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