KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > AbstractBalancer


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 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.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 import net.sf.hajdbc.Balancer;
29 import net.sf.hajdbc.Database;
30
31 /**
32  * Abstract balancer implementation that implements most of the Balancer interface.
33  *
34  * @author Paul Ferraro
35  * @since 1.0
36  */

37 public abstract class AbstractBalancer implements Balancer
38 {
39     /**
40      * @see net.sf.hajdbc.Balancer#beforeOperation(net.sf.hajdbc.Database)
41      */

42     public void beforeOperation(Database database)
43     {
44         // Do nothing
45
}
46     
47     /**
48      * @see net.sf.hajdbc.Balancer#afterOperation(net.sf.hajdbc.Database)
49      */

50     public void afterOperation(Database database)
51     {
52         // Do nothing
53
}
54     
55     /**
56      * @see net.sf.hajdbc.Balancer#remove(net.sf.hajdbc.Database)
57      */

58     public synchronized boolean remove(Database database)
59     {
60         return this.getDatabases().remove(database);
61     }
62     
63     /**
64      * @see net.sf.hajdbc.Balancer#add(net.sf.hajdbc.Database)
65      */

66     public synchronized boolean add(Database database)
67     {
68         return this.getDatabases().contains(database) ? false : this.getDatabases().add(database);
69     }
70     
71     /**
72      * @see net.sf.hajdbc.Balancer#contains(net.sf.hajdbc.Database)
73      */

74     public synchronized boolean contains(Database database)
75     {
76         return this.getDatabases().contains(database);
77     }
78     
79     /**
80      * @see net.sf.hajdbc.Balancer#first()
81      */

82     public synchronized Database first()
83     {
84         return this.getDatabases().iterator().next();
85     }
86     
87     /**
88      * @see net.sf.hajdbc.Balancer#list()
89      */

90     public synchronized List JavaDoc<Database> list()
91     {
92         List JavaDoc<Database> list = new ArrayList JavaDoc<Database>(this.getDatabases());
93         
94         Collections.sort(list);
95         
96         return Collections.unmodifiableList(list);
97     }
98     
99     /**
100      * Exposes a view of the underlying collection of Databases.
101      * @return a Collection of databases
102      */

103     protected abstract Collection JavaDoc<Database> getDatabases();
104 }
105
Popular Tags