1 5 package com.tc.net.groups; 6 7 import com.tc.net.protocol.tcm.ChannelID; 8 import com.tc.util.Assert; 9 10 import java.io.Externalizable ; 11 import java.io.IOException ; 12 import java.io.ObjectInput ; 13 import java.io.ObjectOutput ; 14 15 public class NodeID implements Externalizable { 16 17 public static final NodeID NULL_ID = new NodeID("NULL-ID", new byte[0]); 18 19 private static final String UNINITIALIZED = "Uninitialized"; 20 21 private String name; 22 private byte[] uid; 23 24 private transient int hash; 25 26 public NodeID() { 27 this.name = UNINITIALIZED; 29 } 30 31 public NodeID(String name, byte[] uid) { 32 this.name = name; 33 this.uid = uid; 34 } 35 36 public int hashCode() { 37 if (hash != 0) return hash; 38 int lhash = 27; 39 for (int i = uid.length - 1; i >= 0; i--) { 40 lhash = 31 * lhash + uid[i]; 41 } 42 hash = lhash; 43 44 return lhash; 45 } 46 47 public boolean equals(Object o) { 48 if (o instanceof NodeID) { 49 NodeID n = (NodeID) o; 50 if (n.uid.length != uid.length) return false; 51 for (int i = uid.length - 1; i >= 0; i--) { 52 if (uid[i] != n.uid[i]) return false; 53 } 54 return true; 55 } 56 return false; 57 } 58 59 public byte[] getUID() { 60 return uid; 61 } 62 63 public String getName() { 64 Assert.assertTrue(this.name != UNINITIALIZED); 65 return name; 66 } 67 68 public String toString() { 69 return "NodeID[" + getName() + "]"; 70 } 71 72 public boolean isNull() { 73 return NULL_ID.equals(this); 74 } 75 76 public void readExternal(ObjectInput in) throws IOException { 77 this.name = in.readUTF(); 78 int length = in.readInt(); 79 this.uid = new byte[length]; 80 for (int i = length - 1; i >= 0; i--) { 81 uid[i] = in.readByte(); 82 } 83 } 84 85 public void writeExternal(ObjectOutput out) throws IOException { 86 Assert.assertTrue(this.name != UNINITIALIZED); 87 out.writeUTF(this.name); 88 int length = this.uid.length; 89 out.writeInt(length); 90 for (int i = length - 1; i >= 0; i--) { 91 out.writeByte(this.uid[i]); 92 } 93 } 94 95 100 public ChannelID toChannelID() { 101 return NodeIDChannelIDConverter.getChannelIDFor(this); 102 } 103 } 104 | Popular Tags |