1 24 25 package org.objectweb.tribe.common; 26 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 41 public class Group implements Serializable 42 { 43 private GroupIdentifier groupIdentifier; 44 private ArrayList members; 45 46 50 public Group(GroupIdentifier gid) 51 { 52 groupIdentifier = gid; 53 members = new ArrayList (); 54 } 55 56 61 public GroupIdentifier getGroupIdentifier() 62 { 63 return groupIdentifier; 64 } 65 66 73 public ArrayList getMembers() 74 { 75 return members; 76 } 77 78 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 members.add(m); 101 } 102 } 103 104 110 public boolean removeMember(Member member) 111 { 112 synchronized (members) 113 { 114 return members.remove(member); 115 } 116 } 117 118 124 public boolean hasMember(Member member) 125 { 126 synchronized (members) 127 { 128 return members.contains(member); 129 } 130 } 131 132 138 public boolean merge(Group other) 139 { 140 boolean hasBeenModified = false; 141 synchronized (members) 142 { 143 for (Iterator 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 161 public String getStringMembers() 162 { 163 StringBuffer sb = new StringBuffer (); 164 synchronized (members) 165 { 166 for (Iterator 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 |