001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.lockmanager.api;
006:
007: import org.apache.commons.lang.builder.HashCodeBuilder;
008:
009: import com.tc.io.TCByteBufferInput;
010: import com.tc.io.TCByteBufferOutput;
011: import com.tc.io.TCSerializable;
012:
013: import java.io.IOException;
014:
015: /**
016: * Stores info on a cluster-wide notify / notifyAll.
017: */
018: public class Notify implements TCSerializable {
019:
020: /** Null instance of Notify */
021: public static final Notify NULL = new Notify(true);
022:
023: private LockID lockID;
024: private ThreadID threadID;
025: private boolean all;
026: private boolean initialized;
027: private int hashCode;
028: private final boolean isNull;
029:
030: /**
031: * New initialized
032: *
033: * @param lockID Lock identifier
034: * @param threadID Thread identifier
035: * @param all Whether notify or notifyAll
036: */
037: public Notify(LockID lockID, ThreadID threadID, boolean all) {
038: isNull = false;
039: initialize(lockID, threadID, all);
040: }
041:
042: /**
043: * New uninitialized
044: */
045: public Notify() {
046: isNull = false;
047: }
048:
049: /**
050: * New null instance
051: */
052: private Notify(boolean isNull) {
053: this .isNull = isNull;
054: }
055:
056: /**
057: * @return True if this is the null instance
058: */
059: public boolean isNull() {
060: return this .isNull;
061: }
062:
063: private void initialize(LockID l, ThreadID id, boolean isAll) {
064: if (initialized)
065: throw new AssertionError("Attempt to initialize twice");
066: this .lockID = l;
067: this .threadID = id;
068: this .all = isAll;
069: hashCode = new HashCodeBuilder(5503, 6737).append(lockID)
070: .append(threadID).append(isAll).toHashCode();
071: initialized = true;
072: }
073:
074: /**
075: * Serialize Notify to output
076: *
077: * @param out Output stream
078: */
079: public void serializeTo(TCByteBufferOutput out) {
080: if (!initialized)
081: throw new AssertionError(
082: "Attempt to serialize an uninitialized Notify.");
083: out.writeString(this .lockID.asString());
084: out.writeLong(this .threadID.toLong());
085: out.writeBoolean(this .all);
086: }
087:
088: /**
089: * Deserialize Notify from in
090: *
091: * @param in Input stream
092: * @return This object
093: * @throws IOException If error reading in
094: */
095: public Object deserializeFrom(TCByteBufferInput in)
096: throws IOException {
097: initialize(new LockID(in.readString()), new ThreadID(in
098: .readLong()), in.readBoolean());
099: return this ;
100: }
101:
102: public int hashCode() {
103: if (!initialized)
104: throw new AssertionError(
105: "Called hashCode before initializing.");
106: return hashCode;
107: }
108:
109: public boolean equals(Object o) {
110: if (!(o instanceof Notify))
111: return false;
112: Notify cmp = (Notify) o;
113: return this .lockID.equals(cmp.lockID)
114: && this .threadID.equals(cmp.threadID)
115: && this .all == cmp.all;
116: }
117:
118: public String toString() {
119: return getClass().getName() + "[" + lockID + ", " + threadID
120: + ", " + "all: " + all + "]";
121: }
122:
123: /**
124: * @return Thread identfier
125: */
126: public ThreadID getThreadID() {
127: return this .threadID;
128: }
129:
130: /**
131: * @return Lock identifier
132: */
133: public LockID getLockID() {
134: return this .lockID;
135: }
136:
137: /**
138: * @return flag frorm using notify() vs notifyall().
139: */
140: public boolean getIsAll() {
141: return this.all;
142: }
143: }
|