KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > common > Group


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Nicolas Modrzyk.
23  */

24
25 package org.objectweb.tribe.common;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * This class defines a Group.
33  * <p>
34  * Note that the member list is always sorted in ascending order of member
35  * addresses.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class Group implements Serializable JavaDoc
42 {
43   private GroupIdentifier groupIdentifier;
44   private ArrayList JavaDoc members;
45
46   /**
47    * Creates a new <code>Group</code> with the given group identifier and no
48    * members.
49    */

50   public Group(GroupIdentifier gid)
51   {
52     groupIdentifier = gid;
53     members = new ArrayList JavaDoc();
54   }
55
56   /**
57    * Get the identifier of this group.
58    *
59    * @return a <code>GroupIdentifier</code>.
60    */

61   public GroupIdentifier getGroupIdentifier()
62   {
63     return groupIdentifier;
64   }
65
66   /**
67    * The list of members in the group. This is not a copy of the member list and
68    * so any modification to the list will implicitely modify the group
69    * composition.
70    *
71    * @return an <code>ArrayList</code> of <code>Member</code>.
72    */

73   public ArrayList JavaDoc getMembers()
74   {
75     return members;
76   }
77
78   /**
79    * Adds a member to the group. This function does not look for duplicate group
80    * members.
81    *
82    * @param m the new group member
83    */

84   public void addMember(Member m)
85   {
86     Address addr = m.getAddress();
87     synchronized (members)
88     {
89       int size = members.size();
90       for (int i = 0; i < size; i++)
91       {
92         Member member = (Member) members.get(i);
93         if (addr.compareTo(member.getAddress()) < 0)
94         {
95           members.add(i, m);
96           return;
97         }
98       }
99       // This is the last element
100
members.add(m);
101     }
102   }
103
104   /**
105    * Remove a member from this group.
106    *
107    * @param member <code>Member</code> object.
108    * @return true if the member was is the group, false otherwise.
109    */

110   public boolean removeMember(Member member)
111   {
112     synchronized (members)
113     {
114       return members.remove(member);
115     }
116   }
117
118   /**
119    * Tests if a member belongs to this group.
120    *
121    * @param member the member to look for
122    * @return true if the member is in the group, false otherwise
123    */

124   public boolean hasMember(Member member)
125   {
126     synchronized (members)
127     {
128       return members.contains(member);
129     }
130   }
131
132   /**
133    * Merge a group definition with another one
134    *
135    * @param other the other group to merge member with.
136    * @return true if the group composition has been modified, false otherwise
137    */

138   public boolean merge(Group other)
139   {
140     boolean hasBeenModified = false;
141     synchronized (members)
142     {
143       for (Iterator JavaDoc iter = other.getMembers().iterator(); iter.hasNext();)
144       {
145         Member member = (Member) iter.next();
146         if (!members.contains(member))
147         {
148           hasBeenModified = true;
149           addMember(member);
150         }
151       }
152     }
153     return hasBeenModified;
154   }
155
156   /**
157    * Return the list of members as a String.
158    *
159    * @return String containing member list
160    */

161   public String JavaDoc getStringMembers()
162   {
163     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164     synchronized (members)
165     {
166       for (Iterator JavaDoc iter = getMembers().iterator(); iter.hasNext();)
167       {
168         Member member = (Member) iter.next();
169         sb.append(member.toString() + " ");
170       }
171     }
172     return sb.toString();
173   }
174
175 }
Popular Tags