KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > groups > NodeID


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectOutput JavaDoc;
14
15 public class NodeID implements Externalizable JavaDoc {
16
17   public static final NodeID NULL_ID = new NodeID("NULL-ID", new byte[0]);
18
19   private static final String JavaDoc UNINITIALIZED = "Uninitialized";
20
21   private String JavaDoc name;
22   private byte[] uid;
23
24   private transient int hash;
25
26   public NodeID() {
27     // satisfy serialization
28
this.name = UNINITIALIZED;
29   }
30
31   public NodeID(String JavaDoc 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 JavaDoc 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 JavaDoc getName() {
64     Assert.assertTrue(this.name != UNINITIALIZED);
65     return name;
66   }
67
68   public String JavaDoc toString() {
69     return "NodeID[" + getName() + "]";
70   }
71
72   public boolean isNull() {
73     return NULL_ID.equals(this);
74   }
75
76   public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc {
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 JavaDoc out) throws IOException JavaDoc {
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   /**
96    * HACK::FIXME::TODO This method is a quick hack to brick NodeIDs to ChannelIDs. This mapping is only valid for the
97    * current VM. The ChannelIDs are given out in the range -100 to Integer.MIN_VALUE to not clash with the regular
98    * client channelID. This definitely needs some cleanup
99    */

100   public ChannelID toChannelID() {
101     return NodeIDChannelIDConverter.getChannelIDFor(this);
102   }
103 }
104
Popular Tags