emul/compact/src/main/java/java/util/concurrent/TimeUnit.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Feb 2013 16:10:10 +0100
branchjdk7-b147
changeset 633 bc6f3be91306
child 635 e5cc7edead25
permissions -rw-r--r--
More classes needed by David
jaroslav@633
     1
/*
jaroslav@633
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@633
     3
 *
jaroslav@633
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@633
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@633
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@633
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@633
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@633
     9
 *
jaroslav@633
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@633
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@633
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@633
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@633
    14
 * accompanied this code).
jaroslav@633
    15
 *
jaroslav@633
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@633
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@633
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@633
    19
 *
jaroslav@633
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@633
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@633
    22
 * questions.
jaroslav@633
    23
 */
jaroslav@633
    24
jaroslav@633
    25
/*
jaroslav@633
    26
 * This file is available under and governed by the GNU General Public
jaroslav@633
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@633
    28
 * However, the following notice accompanied the original version of this
jaroslav@633
    29
 * file:
jaroslav@633
    30
 *
jaroslav@633
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@633
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@633
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@633
    34
 */
jaroslav@633
    35
jaroslav@633
    36
package java.util.concurrent;
jaroslav@633
    37
jaroslav@633
    38
/**
jaroslav@633
    39
 * A <tt>TimeUnit</tt> represents time durations at a given unit of
jaroslav@633
    40
 * granularity and provides utility methods to convert across units,
jaroslav@633
    41
 * and to perform timing and delay operations in these units.  A
jaroslav@633
    42
 * <tt>TimeUnit</tt> does not maintain time information, but only
jaroslav@633
    43
 * helps organize and use time representations that may be maintained
jaroslav@633
    44
 * separately across various contexts.  A nanosecond is defined as one
jaroslav@633
    45
 * thousandth of a microsecond, a microsecond as one thousandth of a
jaroslav@633
    46
 * millisecond, a millisecond as one thousandth of a second, a minute
jaroslav@633
    47
 * as sixty seconds, an hour as sixty minutes, and a day as twenty four
jaroslav@633
    48
 * hours.
jaroslav@633
    49
 *
jaroslav@633
    50
 * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
jaroslav@633
    51
 * how a given timing parameter should be interpreted. For example,
jaroslav@633
    52
 * the following code will timeout in 50 milliseconds if the {@link
jaroslav@633
    53
 * java.util.concurrent.locks.Lock lock} is not available:
jaroslav@633
    54
 *
jaroslav@633
    55
 * <pre>  Lock lock = ...;
jaroslav@633
    56
 *  if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
jaroslav@633
    57
 * </pre>
jaroslav@633
    58
 * while this code will timeout in 50 seconds:
jaroslav@633
    59
 * <pre>
jaroslav@633
    60
 *  Lock lock = ...;
jaroslav@633
    61
 *  if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
jaroslav@633
    62
 * </pre>
jaroslav@633
    63
 *
jaroslav@633
    64
 * Note however, that there is no guarantee that a particular timeout
jaroslav@633
    65
 * implementation will be able to notice the passage of time at the
jaroslav@633
    66
 * same granularity as the given <tt>TimeUnit</tt>.
jaroslav@633
    67
 *
jaroslav@633
    68
 * @since 1.5
jaroslav@633
    69
 * @author Doug Lea
jaroslav@633
    70
 */
jaroslav@633
    71
public enum TimeUnit {
jaroslav@633
    72
    NANOSECONDS {
jaroslav@633
    73
        public long toNanos(long d)   { return d; }
jaroslav@633
    74
        public long toMicros(long d)  { return d/(C1/C0); }
jaroslav@633
    75
        public long toMillis(long d)  { return d/(C2/C0); }
jaroslav@633
    76
        public long toSeconds(long d) { return d/(C3/C0); }
jaroslav@633
    77
        public long toMinutes(long d) { return d/(C4/C0); }
jaroslav@633
    78
        public long toHours(long d)   { return d/(C5/C0); }
jaroslav@633
    79
        public long toDays(long d)    { return d/(C6/C0); }
jaroslav@633
    80
        public long convert(long d, TimeUnit u) { return u.toNanos(d); }
jaroslav@633
    81
        int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
jaroslav@633
    82
    },
jaroslav@633
    83
    MICROSECONDS {
jaroslav@633
    84
        public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
jaroslav@633
    85
        public long toMicros(long d)  { return d; }
jaroslav@633
    86
        public long toMillis(long d)  { return d/(C2/C1); }
jaroslav@633
    87
        public long toSeconds(long d) { return d/(C3/C1); }
jaroslav@633
    88
        public long toMinutes(long d) { return d/(C4/C1); }
jaroslav@633
    89
        public long toHours(long d)   { return d/(C5/C1); }
jaroslav@633
    90
        public long toDays(long d)    { return d/(C6/C1); }
jaroslav@633
    91
        public long convert(long d, TimeUnit u) { return u.toMicros(d); }
jaroslav@633
    92
        int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
jaroslav@633
    93
    },
jaroslav@633
    94
    MILLISECONDS {
jaroslav@633
    95
        public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
jaroslav@633
    96
        public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
jaroslav@633
    97
        public long toMillis(long d)  { return d; }
jaroslav@633
    98
        public long toSeconds(long d) { return d/(C3/C2); }
jaroslav@633
    99
        public long toMinutes(long d) { return d/(C4/C2); }
jaroslav@633
   100
        public long toHours(long d)   { return d/(C5/C2); }
jaroslav@633
   101
        public long toDays(long d)    { return d/(C6/C2); }
jaroslav@633
   102
        public long convert(long d, TimeUnit u) { return u.toMillis(d); }
jaroslav@633
   103
        int excessNanos(long d, long m) { return 0; }
jaroslav@633
   104
    },
jaroslav@633
   105
    SECONDS {
jaroslav@633
   106
        public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
jaroslav@633
   107
        public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
jaroslav@633
   108
        public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
jaroslav@633
   109
        public long toSeconds(long d) { return d; }
jaroslav@633
   110
        public long toMinutes(long d) { return d/(C4/C3); }
jaroslav@633
   111
        public long toHours(long d)   { return d/(C5/C3); }
jaroslav@633
   112
        public long toDays(long d)    { return d/(C6/C3); }
jaroslav@633
   113
        public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
jaroslav@633
   114
        int excessNanos(long d, long m) { return 0; }
jaroslav@633
   115
    },
jaroslav@633
   116
    MINUTES {
jaroslav@633
   117
        public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
jaroslav@633
   118
        public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
jaroslav@633
   119
        public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
jaroslav@633
   120
        public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
jaroslav@633
   121
        public long toMinutes(long d) { return d; }
jaroslav@633
   122
        public long toHours(long d)   { return d/(C5/C4); }
jaroslav@633
   123
        public long toDays(long d)    { return d/(C6/C4); }
jaroslav@633
   124
        public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
jaroslav@633
   125
        int excessNanos(long d, long m) { return 0; }
jaroslav@633
   126
    },
jaroslav@633
   127
    HOURS {
jaroslav@633
   128
        public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
jaroslav@633
   129
        public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
jaroslav@633
   130
        public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
jaroslav@633
   131
        public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
jaroslav@633
   132
        public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
jaroslav@633
   133
        public long toHours(long d)   { return d; }
jaroslav@633
   134
        public long toDays(long d)    { return d/(C6/C5); }
jaroslav@633
   135
        public long convert(long d, TimeUnit u) { return u.toHours(d); }
jaroslav@633
   136
        int excessNanos(long d, long m) { return 0; }
jaroslav@633
   137
    },
jaroslav@633
   138
    DAYS {
jaroslav@633
   139
        public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
jaroslav@633
   140
        public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
jaroslav@633
   141
        public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
jaroslav@633
   142
        public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
jaroslav@633
   143
        public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
jaroslav@633
   144
        public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
jaroslav@633
   145
        public long toDays(long d)    { return d; }
jaroslav@633
   146
        public long convert(long d, TimeUnit u) { return u.toDays(d); }
jaroslav@633
   147
        int excessNanos(long d, long m) { return 0; }
jaroslav@633
   148
    };
jaroslav@633
   149
jaroslav@633
   150
    // Handy constants for conversion methods
jaroslav@633
   151
    static final long C0 = 1L;
jaroslav@633
   152
    static final long C1 = C0 * 1000L;
jaroslav@633
   153
    static final long C2 = C1 * 1000L;
jaroslav@633
   154
    static final long C3 = C2 * 1000L;
jaroslav@633
   155
    static final long C4 = C3 * 60L;
jaroslav@633
   156
    static final long C5 = C4 * 60L;
jaroslav@633
   157
    static final long C6 = C5 * 24L;
jaroslav@633
   158
jaroslav@633
   159
    static final long MAX = Long.MAX_VALUE;
jaroslav@633
   160
jaroslav@633
   161
    /**
jaroslav@633
   162
     * Scale d by m, checking for overflow.
jaroslav@633
   163
     * This has a short name to make above code more readable.
jaroslav@633
   164
     */
jaroslav@633
   165
    static long x(long d, long m, long over) {
jaroslav@633
   166
        if (d >  over) return Long.MAX_VALUE;
jaroslav@633
   167
        if (d < -over) return Long.MIN_VALUE;
jaroslav@633
   168
        return d * m;
jaroslav@633
   169
    }
jaroslav@633
   170
jaroslav@633
   171
    // To maintain full signature compatibility with 1.5, and to improve the
jaroslav@633
   172
    // clarity of the generated javadoc (see 6287639: Abstract methods in
jaroslav@633
   173
    // enum classes should not be listed as abstract), method convert
jaroslav@633
   174
    // etc. are not declared abstract but otherwise act as abstract methods.
jaroslav@633
   175
jaroslav@633
   176
    /**
jaroslav@633
   177
     * Convert the given time duration in the given unit to this
jaroslav@633
   178
     * unit.  Conversions from finer to coarser granularities
jaroslav@633
   179
     * truncate, so lose precision. For example converting
jaroslav@633
   180
     * <tt>999</tt> milliseconds to seconds results in
jaroslav@633
   181
     * <tt>0</tt>. Conversions from coarser to finer granularities
jaroslav@633
   182
     * with arguments that would numerically overflow saturate to
jaroslav@633
   183
     * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
jaroslav@633
   184
     * if positive.
jaroslav@633
   185
     *
jaroslav@633
   186
     * <p>For example, to convert 10 minutes to milliseconds, use:
jaroslav@633
   187
     * <tt>TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)</tt>
jaroslav@633
   188
     *
jaroslav@633
   189
     * @param sourceDuration the time duration in the given <tt>sourceUnit</tt>
jaroslav@633
   190
     * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
jaroslav@633
   191
     * @return the converted duration in this unit,
jaroslav@633
   192
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   193
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   194
     */
jaroslav@633
   195
    public long convert(long sourceDuration, TimeUnit sourceUnit) {
jaroslav@633
   196
        throw new AbstractMethodError();
jaroslav@633
   197
    }
jaroslav@633
   198
jaroslav@633
   199
    /**
jaroslav@633
   200
     * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
jaroslav@633
   201
     * @param duration the duration
jaroslav@633
   202
     * @return the converted duration,
jaroslav@633
   203
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   204
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   205
     * @see #convert
jaroslav@633
   206
     */
jaroslav@633
   207
    public long toNanos(long duration) {
jaroslav@633
   208
        throw new AbstractMethodError();
jaroslav@633
   209
    }
jaroslav@633
   210
jaroslav@633
   211
    /**
jaroslav@633
   212
     * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
jaroslav@633
   213
     * @param duration the duration
jaroslav@633
   214
     * @return the converted duration,
jaroslav@633
   215
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   216
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   217
     * @see #convert
jaroslav@633
   218
     */
jaroslav@633
   219
    public long toMicros(long duration) {
jaroslav@633
   220
        throw new AbstractMethodError();
jaroslav@633
   221
    }
jaroslav@633
   222
jaroslav@633
   223
    /**
jaroslav@633
   224
     * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
jaroslav@633
   225
     * @param duration the duration
jaroslav@633
   226
     * @return the converted duration,
jaroslav@633
   227
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   228
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   229
     * @see #convert
jaroslav@633
   230
     */
jaroslav@633
   231
    public long toMillis(long duration) {
jaroslav@633
   232
        throw new AbstractMethodError();
jaroslav@633
   233
    }
jaroslav@633
   234
jaroslav@633
   235
    /**
jaroslav@633
   236
     * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
jaroslav@633
   237
     * @param duration the duration
jaroslav@633
   238
     * @return the converted duration,
jaroslav@633
   239
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   240
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   241
     * @see #convert
jaroslav@633
   242
     */
jaroslav@633
   243
    public long toSeconds(long duration) {
jaroslav@633
   244
        throw new AbstractMethodError();
jaroslav@633
   245
    }
jaroslav@633
   246
jaroslav@633
   247
    /**
jaroslav@633
   248
     * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
jaroslav@633
   249
     * @param duration the duration
jaroslav@633
   250
     * @return the converted duration,
jaroslav@633
   251
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   252
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   253
     * @see #convert
jaroslav@633
   254
     * @since 1.6
jaroslav@633
   255
     */
jaroslav@633
   256
    public long toMinutes(long duration) {
jaroslav@633
   257
        throw new AbstractMethodError();
jaroslav@633
   258
    }
jaroslav@633
   259
jaroslav@633
   260
    /**
jaroslav@633
   261
     * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
jaroslav@633
   262
     * @param duration the duration
jaroslav@633
   263
     * @return the converted duration,
jaroslav@633
   264
     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
jaroslav@633
   265
     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
jaroslav@633
   266
     * @see #convert
jaroslav@633
   267
     * @since 1.6
jaroslav@633
   268
     */
jaroslav@633
   269
    public long toHours(long duration) {
jaroslav@633
   270
        throw new AbstractMethodError();
jaroslav@633
   271
    }
jaroslav@633
   272
jaroslav@633
   273
    /**
jaroslav@633
   274
     * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
jaroslav@633
   275
     * @param duration the duration
jaroslav@633
   276
     * @return the converted duration
jaroslav@633
   277
     * @see #convert
jaroslav@633
   278
     * @since 1.6
jaroslav@633
   279
     */
jaroslav@633
   280
    public long toDays(long duration) {
jaroslav@633
   281
        throw new AbstractMethodError();
jaroslav@633
   282
    }
jaroslav@633
   283
jaroslav@633
   284
    /**
jaroslav@633
   285
     * Utility to compute the excess-nanosecond argument to wait,
jaroslav@633
   286
     * sleep, join.
jaroslav@633
   287
     * @param d the duration
jaroslav@633
   288
     * @param m the number of milliseconds
jaroslav@633
   289
     * @return the number of nanoseconds
jaroslav@633
   290
     */
jaroslav@633
   291
    abstract int excessNanos(long d, long m);
jaroslav@633
   292
jaroslav@633
   293
    /**
jaroslav@633
   294
     * Performs a timed {@link Object#wait(long, int) Object.wait}
jaroslav@633
   295
     * using this time unit.
jaroslav@633
   296
     * This is a convenience method that converts timeout arguments
jaroslav@633
   297
     * into the form required by the <tt>Object.wait</tt> method.
jaroslav@633
   298
     *
jaroslav@633
   299
     * <p>For example, you could implement a blocking <tt>poll</tt>
jaroslav@633
   300
     * method (see {@link BlockingQueue#poll BlockingQueue.poll})
jaroslav@633
   301
     * using:
jaroslav@633
   302
     *
jaroslav@633
   303
     *  <pre> {@code
jaroslav@633
   304
     * public synchronized Object poll(long timeout, TimeUnit unit)
jaroslav@633
   305
     *     throws InterruptedException {
jaroslav@633
   306
     *   while (empty) {
jaroslav@633
   307
     *     unit.timedWait(this, timeout);
jaroslav@633
   308
     *     ...
jaroslav@633
   309
     *   }
jaroslav@633
   310
     * }}</pre>
jaroslav@633
   311
     *
jaroslav@633
   312
     * @param obj the object to wait on
jaroslav@633
   313
     * @param timeout the maximum time to wait. If less than
jaroslav@633
   314
     * or equal to zero, do not wait at all.
jaroslav@633
   315
     * @throws InterruptedException if interrupted while waiting
jaroslav@633
   316
     */
jaroslav@633
   317
    public void timedWait(Object obj, long timeout)
jaroslav@633
   318
            throws InterruptedException {
jaroslav@633
   319
        if (timeout > 0) {
jaroslav@633
   320
            long ms = toMillis(timeout);
jaroslav@633
   321
            int ns = excessNanos(timeout, ms);
jaroslav@633
   322
            obj.wait(ms, ns);
jaroslav@633
   323
        }
jaroslav@633
   324
    }
jaroslav@633
   325
jaroslav@633
   326
    /**
jaroslav@633
   327
     * Performs a timed {@link Thread#join(long, int) Thread.join}
jaroslav@633
   328
     * using this time unit.
jaroslav@633
   329
     * This is a convenience method that converts time arguments into the
jaroslav@633
   330
     * form required by the <tt>Thread.join</tt> method.
jaroslav@633
   331
     *
jaroslav@633
   332
     * @param thread the thread to wait for
jaroslav@633
   333
     * @param timeout the maximum time to wait. If less than
jaroslav@633
   334
     * or equal to zero, do not wait at all.
jaroslav@633
   335
     * @throws InterruptedException if interrupted while waiting
jaroslav@633
   336
     */
jaroslav@633
   337
    public void timedJoin(Thread thread, long timeout)
jaroslav@633
   338
            throws InterruptedException {
jaroslav@633
   339
        if (timeout > 0) {
jaroslav@633
   340
            long ms = toMillis(timeout);
jaroslav@633
   341
            int ns = excessNanos(timeout, ms);
jaroslav@633
   342
            thread.join(ms, ns);
jaroslav@633
   343
        }
jaroslav@633
   344
    }
jaroslav@633
   345
jaroslav@633
   346
    /**
jaroslav@633
   347
     * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
jaroslav@633
   348
     * this time unit.
jaroslav@633
   349
     * This is a convenience method that converts time arguments into the
jaroslav@633
   350
     * form required by the <tt>Thread.sleep</tt> method.
jaroslav@633
   351
     *
jaroslav@633
   352
     * @param timeout the minimum time to sleep. If less than
jaroslav@633
   353
     * or equal to zero, do not sleep at all.
jaroslav@633
   354
     * @throws InterruptedException if interrupted while sleeping
jaroslav@633
   355
     */
jaroslav@633
   356
    public void sleep(long timeout) throws InterruptedException {
jaroslav@633
   357
        if (timeout > 0) {
jaroslav@633
   358
            long ms = toMillis(timeout);
jaroslav@633
   359
            int ns = excessNanos(timeout, ms);
jaroslav@633
   360
            Thread.sleep(ms, ns);
jaroslav@633
   361
        }
jaroslav@633
   362
    }
jaroslav@633
   363
jaroslav@633
   364
}