ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/TimeUnit.java (file contents):
Revision 1.13 by dl, Fri Sep 12 15:40:10 2003 UTC vs.
Revision 1.14 by dl, Fri Sep 12 20:17:50 2003 UTC

# Line 41 | Line 41 | package java.util.concurrent;
41   * @author Doug Lea
42   */
43   public final class TimeUnit implements java.io.Serializable {
44 +    /* ordered indices for each time unit */
45 +    private static final int NS = 0;
46 +    private static final int US = 1;
47 +    private static final int MS = 2;
48 +    private static final int S  = 3;
49 +
50 +    /** quick lookup table for conversion factors */
51 +    private static final int[] multipliers = { 1,
52 +                                       1000,
53 +                                       1000*1000,
54 +                                       1000*1000*1000 };
55 +
56 +    /** lookup table to check saturation */
57 +    private static final long[] overflows = {
58 +        // Note that because we are dividing these down anyway,
59 +        // we don't have to deal with asymmetry of MIN/MAX values.
60 +        0, // unused
61 +        Long.MAX_VALUE / 1000,
62 +        Long.MAX_VALUE / (1000 * 1000),
63 +        Long.MAX_VALUE / (1000 * 1000 * 1000) };
64 +
65 +    /** the index of this unit */
66 +    private int index;
67 +    /** Common name for unit */
68 +    private final String unitName;
69 +
70 +    /** private constructor */
71 +    TimeUnit(int index, String name) {
72 +        this.index = index;
73 +        this.unitName = name;
74 +    }
75 +
76 +    /** Unit for one-second granularities. */
77 +    public static final TimeUnit SECONDS = new TimeUnit(S, "seconds");
78 +    /** Unit for one-millisecond granularities. */
79 +    public static final TimeUnit MILLISECONDS = new TimeUnit(MS, "milliseconds");
80 +    /** Unit for one-microsecond granularities. */
81 +    public static final TimeUnit MICROSECONDS = new TimeUnit(US, "microseconds");
82 +    /** Unit for one-nanosecond granularities. */
83 +    public static final TimeUnit NANOSECONDS = new TimeUnit(NS, "nanoseconds");
84 +
85 +    /**
86 +     * Utility method to compute the excess-nanosecond argument to
87 +     * wait, sleep, join. The results may overflow, so public methods
88 +     * invoking this should document possible overflow unless
89 +     * overflow is known not to be possible for the given arguments.
90 +     */
91 +    private int excessNanos(long time, long ms) {
92 +        if (index == NS)
93 +            return (int) (time  - (ms * multipliers[MS-NS]));
94 +        else if (index == US)
95 +            return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS]));
96 +        else
97 +            return 0;
98 +    }
99  
100      /**
101 <     * Perform the conversion based on given index representing the
101 >     * Perform conversion based on given delta representing the
102       * difference between units
103       * @param delta the difference in index values of source and target units
104       * @param duration the duration
# Line 194 | Line 249 | public final class TimeUnit implements j
249          return unitName;
250      }
251  
197
198    /**
199     * Returns true if the given object is a TimeUnit representing
200     * the same unit as this TimeUnit.
201     * @param x the object to compare
202     * @return true if equal
203     */
204    public boolean equals(Object x) {
205        if (!(x instanceof TimeUnit))
206            return false;
207        return ((TimeUnit)x).index == index;
208    }
209
210    /**
211     * Returns a hash code for this TimeUnit.
212     * @return the hash code
213     */
214    public int hashCode() {
215        return unitName.hashCode();
216    }
217
218    private final String unitName;
219
220    /* ordered indices for each time unit */
221    private static final int NS = 0;
222    private static final int US = 1;
223    private static final int MS = 2;
224    private static final int S  = 3;
225
226    /** quick lookup table for conversion factors */
227    static final int[] multipliers = { 1,
228                                       1000,
229                                       1000*1000,
230                                       1000*1000*1000 };
231
232    /** lookup table to check saturation */
233    static final long[] overflows = {
234        // Note that because we are dividing these down anyway,
235        // we don't have to deal with asymmetry of MIN/MAX values.
236        0, // unused
237        Long.MAX_VALUE / 1000,
238        Long.MAX_VALUE / (1000 * 1000),
239        Long.MAX_VALUE / (1000 * 1000 * 1000) };
240
241    /** the index of this unit */
242    int index;
243
244    /** private constructor */
245    TimeUnit(int index, String name) {
246        this.index = index;
247        this.unitName = name;
248    }
249
252      /**
253 <     * Utility method to compute the excess-nanosecond argument to
254 <     * wait, sleep, join. The results may overflow, so public methods
253 <     * invoking this should document possible overflow unless
254 <     * overflow is known not to be possible for the given arguments.
253 >     * Resolves instances being deserialized to a single instance per
254 >     * unit.
255       */
256 <    private int excessNanos(long time, long ms) {
257 <        if (index == NS)
258 <            return (int) (time  - (ms * multipliers[MS-NS]));
259 <        else if (index == US)
260 <            return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS]));
261 <        else
262 <            return 0;
256 >    private Object readResolve() {
257 >        switch(index) {
258 >        case NS: return NANOSECONDS;
259 >        case US: return MICROSECONDS;
260 >        case MS: return MILLISECONDS;
261 >        case  S: return SECONDS;
262 >        default: assert(false); return null;
263 >        }
264      }
264
265    /** Unit for one-second granularities. */
266    public static final TimeUnit SECONDS = new TimeUnit(S, "seconds");
267
268    /** Unit for one-millisecond granularities. */
269    public static final TimeUnit MILLISECONDS = new TimeUnit(MS, "milliseconds");
270
271    /** Unit for one-microsecond granularities. */
272    public static final TimeUnit MICROSECONDS = new TimeUnit(US, "microseconds");
273
274    /** Unit for one-nanosecond granularities. */
275    public static final TimeUnit NANOSECONDS = new TimeUnit(NS, "nanoseconds");
276
265   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines