001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.balancer.simple;
019
020import java.util.Collections;
021import java.util.Comparator;
022import java.util.Set;
023
024import net.sf.hajdbc.Database;
025import net.sf.hajdbc.balancer.AbstractSetBalancer;
026
027/**
028 * Trivial balancer implementation whose {@link #next} implementation always returns the database with the highest weight.
029 * 
030 * @author  Paul Ferraro
031 * @param <D> either java.sql.Driver or javax.sql.DataSource
032 */
033public class SimpleBalancer<Z, D extends Database<Z>> extends AbstractSetBalancer<Z, D>
034{
035        private volatile D nextDatabase = null;
036        
037        private Comparator<D> comparator = new Comparator<D>()
038        {
039                @Override
040                public int compare(D database1, D database2)
041                {
042                        return database1.getWeight() - database2.getWeight();
043                }
044        };
045
046        /**
047         * Constructs a new SimpleBalancer
048         * @param databases
049         */
050        public SimpleBalancer(Set<D> databases)
051        {
052                super(databases);
053                
054                this.reset();
055        }
056        
057        /**
058         * {@inheritDoc}
059         * @see net.sf.hajdbc.balancer.Balancer#next()
060         */
061        @Override
062        public D next()
063        {
064                return this.nextDatabase;
065        }
066
067        /**
068         * {@inheritDoc}
069         * @see net.sf.hajdbc.balancer.AbstractSetBalancer#added(net.sf.hajdbc.Database)
070         */
071        @Override
072        protected void added(D database)
073        {
074                this.reset();
075        }
076
077        /**
078         * {@inheritDoc}
079         * @see net.sf.hajdbc.balancer.AbstractSetBalancer#removed(net.sf.hajdbc.Database)
080         */
081        @Override
082        protected void removed(D database)
083        {
084                this.reset();
085        }
086        
087        private void reset()
088        {
089                Set<D> databaseSet = this.getDatabases();
090                
091                this.nextDatabase = databaseSet.isEmpty() ? null : Collections.max(databaseSet, this.comparator);
092        }
093
094        /**
095         * {@inheritDoc}
096         * @see net.sf.hajdbc.balancer.AbstractSetBalancer#cleared()
097         */
098        @Override
099        protected void cleared()
100        {
101                this.nextDatabase = null;
102        }
103}