1 3 package org.jgroups; 4 5 6 import org.jgroups.util.Streamable; 7 import org.jgroups.util.Util; 8 9 import java.io.*; 10 import java.util.Vector ; 11 import java.util.Iterator ; 12 13 14 23 public class View implements Externalizable, Cloneable , Streamable { 24 29 protected ViewId vid=null; 30 31 37 protected Vector members=null; 38 39 40 43 public View() { 44 } 45 46 47 53 public View(ViewId vid, Vector members) { 54 this.vid=vid; 55 this.members=members; 56 } 57 58 59 66 public View(Address creator, long id, Vector members) { 67 this(new ViewId(creator, id), members); 68 } 69 70 71 77 public ViewId getVid() { 78 return vid; 79 } 80 81 87 public Address getCreator() { 88 return vid != null ? vid.getCoordAddress() : null; 89 } 90 91 98 public Vector getMembers() { 99 return members; 100 } 101 102 109 public boolean containsMember(Address mbr) { 110 if(mbr == null || members == null) { 111 return false; 112 } 113 return members.contains(mbr); 114 } 115 116 117 public boolean equals(Object obj) { 118 if(obj == null) 119 return false; 120 if(vid != null) { 121 int rc=vid.compareTo(((View)obj).vid); 122 if(rc != 0) 123 return false; 124 if(members != null && ((View)obj).members != null) { 125 return members.equals(((View)obj).members); 126 } 127 } 128 return false; 129 } 130 131 136 public int size() { 137 return members == null ? 0 : members.size(); 138 } 139 140 141 146 public Object clone() { 147 ViewId vid2=vid != null ? (ViewId)vid.clone() : null; 148 Vector members2=members != null ? (Vector )members.clone() : null; 149 return new View(vid2, members2); 150 } 151 152 153 156 public String printDetails() { 157 StringBuffer ret=new StringBuffer (); 158 ret.append(vid).append("\n\t"); 159 if(members != null) { 160 for(int i=0; i < members.size(); i++) { 161 ret.append(members.elementAt(i)).append("\n\t"); 162 } 163 ret.append('\n'); 164 } 165 return ret.toString(); 166 } 167 168 169 public String toString() { 170 StringBuffer ret=new StringBuffer (64); 171 ret.append(vid + " " + members); 172 return ret.toString(); 173 } 174 175 176 public void writeExternal(ObjectOutput out) throws IOException { 177 out.writeObject(vid); 178 out.writeObject(members); 179 } 180 181 182 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 183 vid=(ViewId)in.readObject(); 184 members=(Vector )in.readObject(); 185 } 186 187 188 public void writeTo(DataOutputStream out) throws IOException { 189 if(vid != null) { 191 out.write(1); 192 vid.writeTo(out); 193 } 194 else 195 out.write(0); 196 197 if(members != null) { 199 out.write(1); 200 out.writeInt(members.size()); 201 for(Iterator it=members.iterator(); it.hasNext();) { 202 Address addr=(Address)it.next(); 203 Util.writeAddress(addr, out); 204 } 205 } 206 else 207 out.write(0); 208 } 209 210 211 public void readFrom(DataInputStream in) throws IOException, IllegalAccessException , InstantiationException { 212 int b; 213 b=in.read(); 215 if(b == 1) { 216 vid=new ViewId(); 217 vid.readFrom(in); 218 } 219 220 b=in.read(); 222 if(b == 1) { 223 b=in.readInt(); 224 members=new Vector (b); 225 Address addr; 226 for(int i=0; i < b; i++) { 227 addr=Util.readAddress(in); 228 members.add(addr); 229 } 230 } 231 } 232 233 234 } 235 | Popular Tags |