KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > Membership


1 // $Id: Membership.java,v 1.7 2004/09/23 16:30:00 belaban Exp $
2

3 package org.jgroups;
4
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import java.util.*;
10
11
12 /**
13  * Class to keep track of Addresses.
14  * The member ship object holds a vector of Address object that are in the same membership
15  * Each unique address can only exist once, ie, doing Membership.add(existing_address) will be ignored
16  */

17 public class Membership implements Cloneable JavaDoc {
18     /* private vector to hold all the addresses */
19     private final LinkedList members=new LinkedList();
20     protected static final Log log=LogFactory.getLog(Membership.class);
21
22     /**
23      * Public constructor
24      * Creates a member ship object with zero members
25      */

26     public Membership() {
27     }
28
29
30     /**
31      * Creates a member ship object with the initial members.
32      * The Address references are copied out of the vector, so that the
33      * vector passed in as parameters is not the same reference as the vector
34      * that the membership class is using
35      *
36      * @param initial_members - a list of members that belong to this membership
37      */

38     public Membership(Collection initial_members) {
39         if(initial_members != null)
40             add(initial_members);
41     }
42
43
44
45     /**
46      * returns a copy (clone) of the members in this membership.
47      * the vector returned is immutable in reference to this object.
48      * ie, modifying the vector that is being returned in this method
49      * will not modify this membership object.
50      *
51      * @return a list of members,
52      */

53     public Vector getMembers() {
54         /*clone so that this objects members can not be manipulated from the outside*/
55         synchronized(members) {
56             return new Vector(members);
57         }
58     }
59
60
61     /**
62      * Adds a new member to this membership.
63      * If the member already exist (Address.equals(Object) returns true then the member will
64      * not be added to the membership
65      */

66     public void add(Address new_member) {
67         synchronized(members) {
68             if(new_member != null && !members.contains(new_member)) {
69                 members.add(new_member);
70             }
71         }
72     }
73
74
75     /**
76      * Adds a list of members to this membership
77      *
78      * @param v - a vector containing Address objects
79      * @throws ClassCastException if v contains objects that don't implement the Address interface
80      * @see #add
81      */

82     public void add(Collection v) {
83         if(v != null) {
84             for(Iterator it=v.iterator(); it.hasNext();) {
85                 Address addr=(Address)it.next();
86                 add(addr);
87             }
88         }
89     }
90
91
92     /**
93      * removes an member from the membership.
94      * If this member doesn't exist, no action will be performed on the existing membership
95      *
96      * @param old_member - the member to be removed
97      */

98     public void remove(Address old_member) {
99         if(old_member != null) {
100             synchronized(members) {
101                 members.remove(old_member);
102             }
103         }
104     }
105
106
107     /**
108      * removes all the members contained in v from this membership
109      *
110      * @param v - a vector containing all the members to be removed
111      */

112     public void remove(Collection v) {
113         if(v != null) {
114             synchronized(members) {
115                 members.removeAll(v);
116             }
117         }
118     }
119
120
121     /**
122      * removes all the members from this membership
123      */

124     public void clear() {
125         synchronized(members) {
126             members.clear();
127         }
128     }
129
130     /**
131      * Clear the membership and adds all members of v
132      * This method will clear out all the old members of this membership by
133      * invoking the <code>Clear</code> method.
134      * Then it will add all the all members provided in the vector v
135      *
136      * @param v - a vector containing all the members this membership will contain
137      */

138     public void set(Collection v) {
139         clear();
140         if(v != null) {
141             add(v);
142         }
143     }
144
145
146     /**
147      * Clear the membership and adds all members of v
148      * This method will clear out all the old members of this membership by
149      * invoking the <code>Clear</code> method.
150      * Then it will add all the all members provided in the vector v
151      *
152      * @param m - a membership containing all the members this membership will contain
153      */

154     public void set(Membership m) {
155         clear();
156         if(m != null) {
157             add(m.getMembers());
158         }
159     }
160
161
162     /**
163      * merges membership with the new members and removes suspects
164      * The Merge method will remove all the suspects and add in the new members.
165      * It will do it in the order
166      * 1. Remove suspects
167      * 2. Add new members
168      * the order is very important to notice.
169      *
170      * @param new_mems - a vector containing a list of members (Address) to be added to this membership
171      * @param suspects - a vector containing a list of members (Address) to be removed from this membership
172      */

173     public void merge(Collection new_mems, Collection suspects) {
174         remove(suspects);
175         add(new_mems);
176     }
177
178
179     /**
180      * Returns true if the provided member belongs to this membership
181      *
182      * @param member
183      * @return true if the member belongs to this membership
184      */

185     public boolean contains(Address member) {
186         if(member == null) return false;
187         synchronized(members) {
188             return members.contains(member);
189         }
190     }
191
192
193     /* Simple inefficient bubble sort, but not used very often (only when merging) */
194     public void sort() {
195         synchronized(members) {
196             Collections.sort(members);
197         }
198     }
199
200
201
202
203     /**
204      * returns a copy of this membership
205      *
206      * @return an exact copy of this membership
207      */

208     public Membership copy() {
209         return ((Membership)clone());
210     }
211
212
213     /**
214      * @return a clone of this object. The list of members is copied to a new
215      * container
216      */

217     public Object JavaDoc clone() {
218         return new Membership(this.members);
219     }
220
221
222     /**
223      * Returns the number of addresses in this membership
224      *
225      * @return the number of addresses in this membership
226      */

227     public int size() {
228         synchronized(members) {
229             return members.size();
230         }
231     }
232
233     /**
234      * Returns the component at the specified index
235      *
236      * @param index - 0..size()-1
237      * @throws ArrayIndexOutOfBoundsException - if the index is negative or not less than the current size of this Membership object.
238      * @see java.util.Vector#elementAt
239      */

240
241     public Object JavaDoc elementAt(int index) {
242         synchronized(members) {
243             return members.get(index);
244         }
245     }
246
247
248     public String JavaDoc toString() {
249         synchronized(members) {
250             return members.toString();
251         }
252     }
253
254
255 }
256
Popular Tags