net.sf.hajdbc.balancer.simple.SimpleBalancer Maven / Gradle / Ivy
/*
* HA-JDBC: High-Availability JDBC
* Copyright (C) 2012 Paul Ferraro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package net.sf.hajdbc.balancer.simple;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.balancer.AbstractSetBalancer;
import net.sf.hajdbc.state.StateManager;
/**
* Trivial balancer implementation whose {@link #next} implementation always returns the database with the highest weight.
*
* @author Paul Ferraro
* @param either java.sql.Driver or javax.sql.DataSource
*/
public class SimpleBalancer> extends AbstractSetBalancer
{
private volatile D nextDatabase = null;
private Comparator comparator = new Comparator()
{
@Override
public int compare(D database1, D database2)
{
return database1.getWeight() - database2.getWeight();
}
};
/**
* Constructs a new SimpleBalancer
* @param databases
*/
public SimpleBalancer(Set databases)
{
super(databases);
this.reset();
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.balancer.Balancer#next()
*/
@Override
public D next()
{
return this.nextDatabase;
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.balancer.AbstractSetBalancer#added(net.sf.hajdbc.Database)
*/
@Override
protected void added(D database)
{
this.reset();
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.balancer.AbstractSetBalancer#removed(net.sf.hajdbc.Database)
*/
@Override
protected void removed(D database)
{
this.reset();
}
private void reset()
{
Set databaseSet = this.getDatabases();
this.nextDatabase = databaseSet.isEmpty() ? null : Collections.max(databaseSet, this.comparator);
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.balancer.AbstractSetBalancer#cleared()
*/
@Override
protected void cleared()
{
this.nextDatabase = null;
}
}