1 6 7 package org.apache.beehive.netui.util.internal.concurrent; 8 9 42 abstract class TimeUnit implements java.io.Serializable { 43 44 public static final TimeUnit NANOSECONDS = new TimeUnit(0, "NANOSECONDS") { 45 public long toNanos(long d) { return d; } 46 public long toMicros(long d) { return d/(C1/C0); } 47 public long toMillis(long d) { return d/(C2/C0); } 48 public long toSeconds(long d) { return d/(C3/C0); } 49 public long toMinutes(long d) { return d/(C4/C0); } 50 public long toHours(long d) { return d/(C5/C0); } 51 public long toDays(long d) { return d/(C6/C0); } 52 public long convert(long d, TimeUnit u) { return u.toNanos(d); } 53 int excessNanos(long d, long m) { return (int)(d - (m*C2)); } 54 }; 55 public static final TimeUnit MICROSECONDS = new TimeUnit(1, "MICROSECONDS") { 56 public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } 57 public long toMicros(long d) { return d; } 58 public long toMillis(long d) { return d/(C2/C1); } 59 public long toSeconds(long d) { return d/(C3/C1); } 60 public long toMinutes(long d) { return d/(C4/C1); } 61 public long toHours(long d) { return d/(C5/C1); } 62 public long toDays(long d) { return d/(C6/C1); } 63 public long convert(long d, TimeUnit u) { return u.toMicros(d); } 64 int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } 65 }; 66 public static final TimeUnit MILLISECONDS = new TimeUnit(2, "MILLISECONDS") { 67 public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } 68 public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } 69 public long toMillis(long d) { return d; } 70 public long toSeconds(long d) { return d/(C3/C2); } 71 public long toMinutes(long d) { return d/(C4/C2); } 72 public long toHours(long d) { return d/(C5/C2); } 73 public long toDays(long d) { return d/(C6/C2); } 74 public long convert(long d, TimeUnit u) { return u.toMillis(d); } 75 int excessNanos(long d, long m) { return 0; } 76 }; 77 public static final TimeUnit SECONDS = new TimeUnit(3, "SECONDS") { 78 public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } 79 public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } 80 public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } 81 public long toSeconds(long d) { return d; } 82 public long toMinutes(long d) { return d/(C4/C3); } 83 public long toHours(long d) { return d/(C5/C3); } 84 public long toDays(long d) { return d/(C6/C3); } 85 public long convert(long d, TimeUnit u) { return u.toSeconds(d); } 86 int excessNanos(long d, long m) { return 0; } 87 }; 88 public static final TimeUnit MINUTES = new TimeUnit(4, "MINUTES") { 89 public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } 90 public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } 91 public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); } 92 public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); } 93 public long toMinutes(long d) { return d; } 94 public long toHours(long d) { return d/(C5/C4); } 95 public long toDays(long d) { return d/(C6/C4); } 96 public long convert(long d, TimeUnit u) { return u.toMinutes(d); } 97 int excessNanos(long d, long m) { return 0; } 98 }; 99 public static final TimeUnit HOURS = new TimeUnit(5, "HOURS") { 100 public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } 101 public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } 102 public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); } 103 public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); } 104 public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); } 105 public long toHours(long d) { return d; } 106 public long toDays(long d) { return d/(C6/C5); } 107 public long convert(long d, TimeUnit u) { return u.toHours(d); } 108 int excessNanos(long d, long m) { return 0; } 109 }; 110 public static final TimeUnit DAYS = new TimeUnit(6, "DAYS") { 111 public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } 112 public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } 113 public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); } 114 public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); } 115 public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); } 116 public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); } 117 public long toDays(long d) { return d; } 118 public long convert(long d, TimeUnit u) { return u.toDays(d); } 119 int excessNanos(long d, long m) { return 0; } 120 }; 121 122 private static final TimeUnit[] values = new TimeUnit[] 123 { NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS }; 124 125 public static TimeUnit[] values() { 126 return (TimeUnit[])values.clone(); 127 } 128 129 134 private final int index; 135 136 137 private final String name; 138 139 140 TimeUnit(int index, String name) { 141 this.index = index; 142 this.name = name; 143 } 144 145 static final long C0 = 1; 147 static final long C1 = C0 * 1000; 148 static final long C2 = C1 * 1000; 149 static final long C3 = C2 * 1000; 150 static final long C4 = C3 * 60; 151 static final long C5 = C4 * 60; 152 static final long C6 = C5 * 24; 153 154 static final long MAX = Long.MAX_VALUE; 155 156 160 static long x(long d, long m, long over) { 161 if (d > over) return Long.MAX_VALUE; 162 if (d < -over) return Long.MIN_VALUE; 163 return d * m; 164 } 165 166 182 public abstract long convert(long duration, TimeUnit unit); 183 184 192 public abstract long toNanos(long duration); 193 194 202 public abstract long toMicros(long duration); 203 204 212 public abstract long toMillis(long duration); 213 214 222 public abstract long toSeconds(long duration); 223 224 232 public abstract long toMinutes(long duration); 233 234 242 public abstract long toHours(long duration); 243 244 250 public abstract long toDays(long duration); 251 252 259 abstract int excessNanos(long d, long m); 260 261 282 public void timedWait(Object obj, long timeout) 283 throws InterruptedException { 284 if (timeout > 0) { 285 long ms = toMillis(timeout); 286 int ns = excessNanos(timeout, ms); 287 obj.wait(ms, ns); 288 } 289 } 290 291 300 public void timedJoin(Thread thread, long timeout) 301 throws InterruptedException { 302 if (timeout > 0) { 303 long ms = toMillis(timeout); 304 int ns = excessNanos(timeout, ms); 305 thread.join(ms, ns); 306 } 307 } 308 309 317 public void sleep(long timeout) throws InterruptedException { 318 if (timeout > 0) { 319 long ms = toMillis(timeout); 320 int ns = excessNanos(timeout, ms); 321 Thread.sleep(ms, ns); 322 } 323 } 324 325 public String toString() { 326 return name; 327 } 328 } 329 | Popular Tags |