~brian-thomason/+junk/ha-jdbc

« back to all changes in this revision

Viewing changes to src/net/sf/hajdbc/balancer/SimpleBalancer.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:34:21 UTC
  • Revision ID: brian.thomason@canonical.com-20111220173421-p9jg95iq91jgdihh
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * HA-JDBC: High-Availability JDBC
 
3
 * Copyright (c) 2004-2007 Paul Ferraro
 
4
 * 
 
5
 * This library is free software; you can redistribute it and/or modify it 
 
6
 * under the terms of the GNU Lesser General Public License as published by the 
 
7
 * Free Software Foundation; either version 2.1 of the License, or (at your 
 
8
 * option) any later version.
 
9
 * 
 
10
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 
12
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 
13
 * for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with this library; if not, write to the Free Software Foundation, 
 
17
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 * 
 
19
 * Contact: ferraro@users.sourceforge.net
 
20
 */
 
21
package net.sf.hajdbc.balancer;
 
22
 
 
23
import java.util.Collections;
 
24
import java.util.Comparator;
 
25
import java.util.NoSuchElementException;
 
26
 
 
27
import net.sf.hajdbc.Database;
 
28
 
 
29
/**
 
30
 * Trivial balancer implementation whose {@link #next} implementation always returns the database with the highest weight.
 
31
 * 
 
32
 * @author  Paul Ferraro
 
33
 * @param <D> either java.sql.Driver or javax.sql.DataSource
 
34
 */
 
35
public class SimpleBalancer<D> extends AbstractBalancer<D>
 
36
{
 
37
        private volatile Database<D> nextDatabase = null;
 
38
        
 
39
        private Comparator<Database<D>> comparator = new Comparator<Database<D>>()
 
40
        {
 
41
                @Override
 
42
                public int compare(Database<D> database1, Database<D> database2)
 
43
                {
 
44
                        return database1.getWeight() - database2.getWeight();
 
45
                }
 
46
        };
 
47
 
 
48
        /**
 
49
         * @see net.sf.hajdbc.Balancer#next()
 
50
         */
 
51
        @Override
 
52
        public Database<D> next()
 
53
        {
 
54
                Database<D> next = this.nextDatabase;
 
55
                
 
56
                if (next == null)
 
57
                {
 
58
                        throw new NoSuchElementException();
 
59
                }
 
60
                
 
61
                return next;
 
62
        }
 
63
 
 
64
        /**
 
65
         * @see net.sf.hajdbc.balancer.AbstractBalancer#added(net.sf.hajdbc.Database)
 
66
         */
 
67
        @Override
 
68
        protected void added(Database<D> database)
 
69
        {
 
70
                this.reset();
 
71
        }
 
72
 
 
73
        /**
 
74
         * @see net.sf.hajdbc.balancer.AbstractBalancer#removed(net.sf.hajdbc.Database)
 
75
         */
 
76
        @Override
 
77
        protected void removed(Database<D> database)
 
78
        {
 
79
                this.reset();
 
80
        }
 
81
        
 
82
        private void reset()
 
83
        {
 
84
                this.nextDatabase = this.databaseSet.isEmpty() ? null : Collections.max(this.databaseSet, this.comparator);
 
85
        }
 
86
 
 
87
        /**
 
88
         * @see net.sf.hajdbc.balancer.AbstractBalancer#cleared()
 
89
         */
 
90
        @Override
 
91
        protected void cleared()
 
92
        {
 
93
                this.nextDatabase = null;
 
94
        }
 
95
}