KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > WaitInvocation


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

4 package com.tc.object.tx;
5
6 import com.tc.object.msg.LockRequestMessage;
7
8 /**
9  * Encapsulates an invocation of Object.wait(...)
10  */

11 public final class WaitInvocation {
12
13   private final Signature signature;
14
15   private long millis;
16   private int nanos;
17   private long mark = LockRequestMessage.UNITIALIZED_WAIT_TIME;
18
19   public WaitInvocation() {
20     this(NO_ARGS, LockRequestMessage.UNITIALIZED_WAIT_TIME, LockRequestMessage.UNITIALIZED_WAIT_TIME);
21   }
22
23   public WaitInvocation(long millis) {
24     this(LONG, millis, LockRequestMessage.UNITIALIZED_WAIT_TIME);
25   }
26
27   public WaitInvocation(long millis, int nanos) {
28     this(LONG_INT, millis, nanos);
29   }
30
31   private WaitInvocation(Signature signature, long millis, int nanos) {
32     this.signature = signature;
33
34     if (signature == LONG) {
35       if (millis < 0) { throw new IllegalArgumentException JavaDoc("Invalid milliseconds argument to wait(long): " + millis); }
36     } else if (signature == LONG_INT) {
37       if (millis < 0) { throw new IllegalArgumentException JavaDoc("Invalid milliseconds argument to wait(long, int): "
38                                                            + millis); }
39       if (nanos < 0) { throw new IllegalArgumentException JavaDoc("Invalid nanoseconds argument to wait(long, int): " + nanos); }
40     }
41
42     this.millis = millis;
43     this.nanos = nanos;
44   }
45
46   public boolean hasTimeout() {
47     return getSignature() != NO_ARGS;
48   }
49   
50   public long getMillis() {
51     return millis;
52   }
53
54   public int getNanos() {
55     return nanos;
56   }
57
58   public Signature getSignature() {
59     return this.signature;
60   }
61
62   public void mark() {
63     mark = System.currentTimeMillis();
64   }
65
66   public void adjust() {
67     if (mark <= LockRequestMessage.UNITIALIZED_WAIT_TIME || signature == NO_ARGS) return;
68     long now = System.currentTimeMillis();
69     millis -= (now - mark);
70
71     if (millis <= 0) {
72       millis = 1;
73     }
74   }
75
76   public String JavaDoc toString() {
77     if (this.signature == NO_ARGS) { return this.signature.toString(); }
78
79     StringBuffer JavaDoc rv = new StringBuffer JavaDoc("wait(");
80
81     if (this.signature == LONG) {
82       rv.append(getMillis());
83     } else if (this.signature == LONG_INT) {
84       rv.append(getMillis()).append(", ").append(getNanos());
85     }
86
87     rv.append(")");
88
89     return rv.toString();
90   }
91
92   public static final Signature NO_ARGS = new Signature("wait()", 0);
93   public static final Signature LONG = new Signature("wait(long)", 1);
94   public static final Signature LONG_INT = new Signature("wait(long, int)", 2);
95
96   public final static class Signature {
97     private final String JavaDoc desc;
98     private final int argCount;
99
100     private Signature(String JavaDoc desc, int numArgs) {
101       this.desc = desc;
102       this.argCount = numArgs;
103     }
104
105     public int getArgCount() {
106       return this.argCount;
107     }
108
109     public String JavaDoc toString() {
110       return desc;
111     }
112
113   }
114
115 }
Popular Tags