001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.tx;
005:
006: import com.tc.object.msg.LockRequestMessageConsts;
007:
008: /**
009: * Encapsulates an invocation of Object.wait(...)
010: */
011: public final class WaitInvocation {
012:
013: private final Signature signature;
014:
015: private long millis;
016: private int nanos;
017: private long mark = LockRequestMessageConsts.UNITIALIZED_WAIT_TIME;
018:
019: /**
020: * Untimed wait
021: */
022: public WaitInvocation() {
023: this (NO_ARGS, LockRequestMessageConsts.UNITIALIZED_WAIT_TIME,
024: LockRequestMessageConsts.UNITIALIZED_WAIT_TIME);
025: }
026:
027: /**
028: * Wait for millis
029: * @param millis Milliseconds to wait
030: */
031: public WaitInvocation(long millis) {
032: this (LONG, millis,
033: LockRequestMessageConsts.UNITIALIZED_WAIT_TIME);
034: }
035:
036: /**
037: * Wait for millis and nanos
038: * @param millis Milliseconds
039: * @param nanos Nanoseconds
040: */
041: public WaitInvocation(long millis, int nanos) {
042: this (LONG_INT, millis, nanos);
043: }
044:
045: /**
046: * Wait on method signature
047: * @param signature Method signature
048: * @param millis Milliseconds
049: * @param nanos Nanoseconds
050: */
051: private WaitInvocation(Signature signature, long millis, int nanos) {
052: this .signature = signature;
053:
054: if (signature == LONG) {
055: if (millis < 0) {
056: throw new IllegalArgumentException(
057: "Invalid milliseconds argument to wait(long): "
058: + millis);
059: }
060: } else if (signature == LONG_INT) {
061: if (millis < 0) {
062: throw new IllegalArgumentException(
063: "Invalid milliseconds argument to wait(long, int): "
064: + millis);
065: }
066: if (nanos < 0) {
067: throw new IllegalArgumentException(
068: "Invalid nanoseconds argument to wait(long, int): "
069: + nanos);
070: }
071: }
072:
073: this .millis = millis;
074: this .nanos = nanos;
075: }
076:
077: /**
078: * @return True if has timeout
079: */
080: public boolean hasTimeout() {
081: return getSignature() != NO_ARGS;
082: }
083:
084: /**
085: * @return True if timeouts are > 0
086: */
087: public boolean needsToWait() {
088: return millis > 0 || nanos > 0;
089: }
090:
091: /**
092: * @return Get millis timeout
093: */
094: public long getMillis() {
095: return millis;
096: }
097:
098: /**
099: * @return Get nanos timeout
100: */
101: public int getNanos() {
102: return nanos;
103: }
104:
105: /**
106: * @return Get method signature
107: */
108: public Signature getSignature() {
109: return this .signature;
110: }
111:
112: /**
113: * Mark seen at current time
114: */
115: public void mark() {
116: mark = System.currentTimeMillis();
117: }
118:
119: /**
120: * Adjust by removing time to wait by now-last mark.
121: */
122: public void adjust() {
123: if (mark <= LockRequestMessageConsts.UNITIALIZED_WAIT_TIME
124: || signature == NO_ARGS)
125: return;
126: long now = System.currentTimeMillis();
127: millis -= (now - mark);
128:
129: if (millis <= 0) {
130: millis = 1;
131: }
132: }
133:
134: public String toString() {
135: if (this .signature == NO_ARGS) {
136: return this .signature.toString();
137: }
138:
139: StringBuffer rv = new StringBuffer("wait(");
140:
141: if (this .signature == LONG) {
142: rv.append(getMillis());
143: } else if (this .signature == LONG_INT) {
144: rv.append(getMillis()).append(", ").append(getNanos());
145: }
146:
147: rv.append(")");
148:
149: return rv.toString();
150: }
151:
152: /** Signature for untimed wait */
153: public static final Signature NO_ARGS = new Signature("wait()", 0);
154: /** Signature for 1 arg wait() */
155: public static final Signature LONG = new Signature("wait(long)", 1);
156: /** Signature for 2 arg wait() */
157: public static final Signature LONG_INT = new Signature(
158: "wait(long, int)", 2);
159:
160: public final static class Signature {
161: private final String desc;
162: private final int argCount;
163:
164: private Signature(String desc, int numArgs) {
165: this .desc = desc;
166: this .argCount = numArgs;
167: }
168:
169: public int getArgCount() {
170: return this .argCount;
171: }
172:
173: public String toString() {
174: return desc;
175: }
176:
177: }
178:
179: }
|