• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 package java.util.concurrent;
12 
13 /**
14  * GWT emulation of TimeUnit, created by removing unsupported operations from
15  * Doug Lea's public domain version.
16  */
17 public enum TimeUnit {
18   NANOSECONDS {
toNanos(long d)19     public long toNanos(long d)   { return d; }
toMicros(long d)20     public long toMicros(long d)  { return d/(C1/C0); }
toMillis(long d)21     public long toMillis(long d)  { return d/(C2/C0); }
toSeconds(long d)22     public long toSeconds(long d) { return d/(C3/C0); }
toMinutes(long d)23     public long toMinutes(long d) { return d/(C4/C0); }
toHours(long d)24     public long toHours(long d)   { return d/(C5/C0); }
toDays(long d)25     public long toDays(long d)    { return d/(C6/C0); }
convert(long d, TimeUnit u)26     public long convert(long d, TimeUnit u) { return u.toNanos(d); }
excessNanos(long d, long m)27     int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
28   },
29   MICROSECONDS {
toNanos(long d)30     public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
toMicros(long d)31     public long toMicros(long d)  { return d; }
toMillis(long d)32     public long toMillis(long d)  { return d/(C2/C1); }
toSeconds(long d)33     public long toSeconds(long d) { return d/(C3/C1); }
toMinutes(long d)34     public long toMinutes(long d) { return d/(C4/C1); }
toHours(long d)35     public long toHours(long d)   { return d/(C5/C1); }
toDays(long d)36     public long toDays(long d)    { return d/(C6/C1); }
convert(long d, TimeUnit u)37     public long convert(long d, TimeUnit u) { return u.toMicros(d); }
excessNanos(long d, long m)38     int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
39   },
40   MILLISECONDS {
toNanos(long d)41     public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
toMicros(long d)42     public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
toMillis(long d)43     public long toMillis(long d)  { return d; }
toSeconds(long d)44     public long toSeconds(long d) { return d/(C3/C2); }
toMinutes(long d)45     public long toMinutes(long d) { return d/(C4/C2); }
toHours(long d)46     public long toHours(long d)   { return d/(C5/C2); }
toDays(long d)47     public long toDays(long d)    { return d/(C6/C2); }
convert(long d, TimeUnit u)48     public long convert(long d, TimeUnit u) { return u.toMillis(d); }
excessNanos(long d, long m)49     int excessNanos(long d, long m) { return 0; }
50   },
51   SECONDS {
toNanos(long d)52     public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
toMicros(long d)53     public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
toMillis(long d)54     public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
toSeconds(long d)55     public long toSeconds(long d) { return d; }
toMinutes(long d)56     public long toMinutes(long d) { return d/(C4/C3); }
toHours(long d)57     public long toHours(long d)   { return d/(C5/C3); }
toDays(long d)58     public long toDays(long d)    { return d/(C6/C3); }
convert(long d, TimeUnit u)59     public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
excessNanos(long d, long m)60     int excessNanos(long d, long m) { return 0; }
61   },
62   MINUTES {
toNanos(long d)63     public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
toMicros(long d)64     public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
toMillis(long d)65     public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
toSeconds(long d)66     public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
toMinutes(long d)67     public long toMinutes(long d) { return d; }
toHours(long d)68     public long toHours(long d)   { return d/(C5/C4); }
toDays(long d)69     public long toDays(long d)    { return d/(C6/C4); }
convert(long d, TimeUnit u)70     public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
excessNanos(long d, long m)71     int excessNanos(long d, long m) { return 0; }
72   },
73   HOURS {
toNanos(long d)74     public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
toMicros(long d)75     public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
toMillis(long d)76     public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
toSeconds(long d)77     public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
toMinutes(long d)78     public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
toHours(long d)79     public long toHours(long d)   { return d; }
toDays(long d)80     public long toDays(long d)    { return d/(C6/C5); }
convert(long d, TimeUnit u)81     public long convert(long d, TimeUnit u) { return u.toHours(d); }
excessNanos(long d, long m)82     int excessNanos(long d, long m) { return 0; }
83   },
84   DAYS {
toNanos(long d)85     public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
toMicros(long d)86     public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
toMillis(long d)87     public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
toSeconds(long d)88     public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
toMinutes(long d)89     public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
toHours(long d)90     public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
toDays(long d)91     public long toDays(long d)    { return d; }
convert(long d, TimeUnit u)92     public long convert(long d, TimeUnit u) { return u.toDays(d); }
excessNanos(long d, long m)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 
x(long d, long m, long over)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 
convert(long sourceDuration, TimeUnit sourceUnit)115   public long convert(long sourceDuration, TimeUnit sourceUnit) {
116     throw new AssertionError();
117   }
118 
toNanos(long duration)119   public long toNanos(long duration) {
120     throw new AssertionError();
121   }
122 
toMicros(long duration)123   public long toMicros(long duration) {
124     throw new AssertionError();
125   }
126 
toMillis(long duration)127   public long toMillis(long duration) {
128     throw new AssertionError();
129   }
130 
toSeconds(long duration)131   public long toSeconds(long duration) {
132     throw new AssertionError();
133   }
134 
toMinutes(long duration)135   public long toMinutes(long duration) {
136     throw new AssertionError();
137   }
138 
toHours(long duration)139   public long toHours(long duration) {
140     throw new AssertionError();
141   }
142 
toDays(long duration)143   public long toDays(long duration) {
144     throw new AssertionError();
145   }
146 
excessNanos(long d, long m)147   abstract int excessNanos(long d, long m);
148 }
149