1/*
2 * This file is a modified version of
3 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/TimeUnit.java
4 * which contained the following notice:
5 *
6 * Written by Doug Lea with assistance from members of JCP JSR-166
7 * Expert Group and released to the public domain, as explained at
8 * http://creativecommons.org/publicdomain/zero/1.0/
9 */
10
11package java.util.concurrent;
12
13/**
14 * GWT emulation of TimeUnit, created by removing unsupported operations from
15 * Doug Lea's public domain version.
16 */
17public enum TimeUnit {
18  NANOSECONDS {
19    public long toNanos(long d)   { return d; }
20    public long toMicros(long d)  { return d/(C1/C0); }
21    public long toMillis(long d)  { return d/(C2/C0); }
22    public long toSeconds(long d) { return d/(C3/C0); }
23    public long toMinutes(long d) { return d/(C4/C0); }
24    public long toHours(long d)   { return d/(C5/C0); }
25    public long toDays(long d)    { return d/(C6/C0); }
26    public long convert(long d, TimeUnit u) { return u.toNanos(d); }
27    int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
28  },
29  MICROSECONDS {
30    public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
31    public long toMicros(long d)  { return d; }
32    public long toMillis(long d)  { return d/(C2/C1); }
33    public long toSeconds(long d) { return d/(C3/C1); }
34    public long toMinutes(long d) { return d/(C4/C1); }
35    public long toHours(long d)   { return d/(C5/C1); }
36    public long toDays(long d)    { return d/(C6/C1); }
37    public long convert(long d, TimeUnit u) { return u.toMicros(d); }
38    int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
39  },
40  MILLISECONDS {
41    public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
42    public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
43    public long toMillis(long d)  { return d; }
44    public long toSeconds(long d) { return d/(C3/C2); }
45    public long toMinutes(long d) { return d/(C4/C2); }
46    public long toHours(long d)   { return d/(C5/C2); }
47    public long toDays(long d)    { return d/(C6/C2); }
48    public long convert(long d, TimeUnit u) { return u.toMillis(d); }
49    int excessNanos(long d, long m) { return 0; }
50  },
51  SECONDS {
52    public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
53    public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
54    public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
55    public long toSeconds(long d) { return d; }
56    public long toMinutes(long d) { return d/(C4/C3); }
57    public long toHours(long d)   { return d/(C5/C3); }
58    public long toDays(long d)    { return d/(C6/C3); }
59    public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
60    int excessNanos(long d, long m) { return 0; }
61  },
62  MINUTES {
63    public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
64    public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
65    public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
66    public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
67    public long toMinutes(long d) { return d; }
68    public long toHours(long d)   { return d/(C5/C4); }
69    public long toDays(long d)    { return d/(C6/C4); }
70    public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
71    int excessNanos(long d, long m) { return 0; }
72  },
73  HOURS {
74    public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
75    public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
76    public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
77    public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
78    public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
79    public long toHours(long d)   { return d; }
80    public long toDays(long d)    { return d/(C6/C5); }
81    public long convert(long d, TimeUnit u) { return u.toHours(d); }
82    int excessNanos(long d, long m) { return 0; }
83  },
84  DAYS {
85    public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
86    public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
87    public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
88    public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
89    public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
90    public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
91    public long toDays(long d)    { return d; }
92    public long convert(long d, TimeUnit u) { return u.toDays(d); }
93    int excessNanos(long d, long m) { return 0; }
94  };
95
96  // Handy constants for conversion methods
97  static final long C0 = 1L;
98  static final long C1 = C0 * 1000L;
99  static final long C2 = C1 * 1000L;
100  static final long C3 = C2 * 1000L;
101  static final long C4 = C3 * 60L;
102  static final long C5 = C4 * 60L;
103  static final long C6 = C5 * 24L;
104
105  static final long MAX = Long.MAX_VALUE;
106
107  static long x(long d, long m, long over) {
108    if (d >  over) return Long.MAX_VALUE;
109    if (d < -over) return Long.MIN_VALUE;
110    return d * m;
111  }
112
113  // exceptions below changed from AbstractMethodError for GWT
114
115  public long convert(long sourceDuration, TimeUnit sourceUnit) {
116    throw new AssertionError();
117  }
118
119  public long toNanos(long duration) {
120    throw new AssertionError();
121  }
122
123  public long toMicros(long duration) {
124    throw new AssertionError();
125  }
126
127  public long toMillis(long duration) {
128    throw new AssertionError();
129  }
130
131  public long toSeconds(long duration) {
132    throw new AssertionError();
133  }
134
135  public long toMinutes(long duration) {
136    throw new AssertionError();
137  }
138
139  public long toHours(long duration) {
140    throw new AssertionError();
141  }
142
143  public long toDays(long duration) {
144    throw new AssertionError();
145  }
146
147  abstract int excessNanos(long d, long m);
148}
149