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