1 3 package org.jgroups; 4 5 import org.jgroups.util.Streamable; 6 import org.jgroups.util.Util; 7 8 import java.io.*; 9 10 11 16 public class ViewId implements Externalizable, Comparable , Cloneable , Streamable { 17 Address coord_addr=null; long id=0; 20 21 public ViewId() { } 23 24 25 30 public ViewId(Address coord_addr) { 31 this.coord_addr=coord_addr; 32 } 33 34 40 public ViewId(Address coord_addr, long id) { 41 this.coord_addr=coord_addr; 42 this.id=id; 43 } 44 45 50 public long getId() { 51 return id; 52 } 53 54 55 60 public Address getCoordAddress() { 61 return coord_addr; 62 } 63 64 65 public String toString() { 66 return "[" + coord_addr + '|' + id + ']'; 67 } 68 69 73 public Object clone() { 74 return new ViewId(coord_addr, id); 75 } 76 77 80 public ViewId copy() { 81 return (ViewId)clone(); 82 } 83 84 90 public int compareTo(Object other) { 91 if(other == null) return 1; 93 if(!(other instanceof ViewId)) { 94 throw new ClassCastException ("ViewId.compareTo(): view id is not comparable with different Objects"); 95 } 96 try { 97 return id > ((ViewId)other).id ? 1 : id < ((ViewId)other).id ? -1 : 0; 98 } 99 catch(NullPointerException e) { 100 System.err.println("ViewId.compareTo(): " + e); 101 throw e; 102 } 103 } 104 105 108 public int compare(Object o) { 109 return compareTo(o); 110 } 111 112 113 public boolean equals(Object other_view) { 114 return compareTo(other_view) == 0 ? true : false; 115 } 116 117 118 public int hashCode() { 119 return (int)id; 120 } 121 122 123 public void writeExternal(ObjectOutput out) throws IOException { 124 out.writeObject(coord_addr); 125 out.writeLong(id); 126 } 127 128 129 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 130 coord_addr=(Address)in.readObject(); 131 id=in.readLong(); 132 } 133 134 public void writeTo(DataOutputStream out) throws IOException { 135 Util.writeAddress(coord_addr, out); 136 out.writeLong(id); 137 } 138 139 public void readFrom(DataInputStream in) throws IOException, IllegalAccessException , InstantiationException { 140 coord_addr=Util.readAddress(in); 141 id=in.readLong(); 142 } 143 144 } 145 | Popular Tags |