001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.groups;
006:
007: import com.tc.io.TCByteBufferInput;
008: import com.tc.io.TCByteBufferOutput;
009: import com.tc.util.Assert;
010:
011: import java.io.IOException;
012: import java.io.ObjectInput;
013: import java.io.ObjectOutput;
014: import java.util.Arrays;
015:
016: public class NodeIDImpl implements NodeID {
017:
018: public static final NodeID NULL_ID = new NodeIDImpl("NULL-ID",
019: new byte[0]);
020:
021: private static final String UNINITIALIZED = "Uninitialized";
022:
023: private String name;
024: private byte[] uid;
025:
026: private transient int hash;
027:
028: public NodeIDImpl() {
029: // satisfy serialization
030: this .name = UNINITIALIZED;
031: }
032:
033: public NodeIDImpl(String name, byte[] uid) {
034: this .name = name;
035: this .uid = uid;
036: }
037:
038: public int hashCode() {
039: if (hash != 0)
040: return hash;
041: int lhash = 27;
042: for (int i = uid.length - 1; i >= 0; i--) {
043: lhash = 31 * lhash + uid[i];
044: }
045: hash = lhash;
046:
047: return lhash;
048: }
049:
050: public boolean equals(Object o) {
051: if (o instanceof NodeIDImpl) {
052: NodeIDImpl that = (NodeIDImpl) o;
053: return Arrays.equals(that.uid, this .uid);
054: }
055: return false;
056: }
057:
058: public byte[] getUID() {
059: return uid;
060: }
061:
062: public String getName() {
063: Assert.assertTrue(this .name != UNINITIALIZED);
064: return name;
065: }
066:
067: public String toString() {
068: return "NodeID[" + getName() + "]";
069: }
070:
071: public boolean isNull() {
072: return NULL_ID.equals(this );
073: }
074:
075: /**
076: * FIXME::Two difference serialization mechanisms are implemented since these classes are used with two different
077: * implementation of comms stack.
078: */
079: public void readExternal(ObjectInput in) throws IOException {
080: this .name = in.readUTF();
081: int length = in.readInt();
082: this .uid = new byte[length];
083: int off = 0;
084: while (length > 0) {
085: int read = in.read(this .uid, off, length);
086: off += read;
087: length -= read;
088: }
089: }
090:
091: public void writeExternal(ObjectOutput out) throws IOException {
092: Assert.assertTrue(this .name != UNINITIALIZED);
093: out.writeUTF(this .name);
094: int length = this .uid.length;
095: out.writeInt(length);
096: out.write(this .uid);
097: }
098:
099: /**
100: * FIXME::Two difference serialization mechanisms are implemented since these classes are used with two different
101: * implementation of comms stack.
102: */
103: public Object deserializeFrom(TCByteBufferInput in)
104: throws IOException {
105: this .name = in.readString();
106: int length = in.readInt();
107: this .uid = new byte[length];
108: int off = 0;
109: while (length > 0) {
110: int read = in.read(this .uid, off, length);
111: off += read;
112: length -= read;
113: }
114: return this ;
115: }
116:
117: public void serializeTo(TCByteBufferOutput out) {
118: Assert.assertTrue(this .name != UNINITIALIZED);
119: out.writeString(this .name);
120: int length = this.uid.length;
121: out.writeInt(length);
122: out.write(this.uid);
123: }
124: }
|