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