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