src/java.base/share/classes/java/util/concurrent/TimeUnit.java
author dl
Fri, 08 Jun 2018 11:38:40 -0700
changeset 50477 cb0efe0cc20e
parent 47307 6864969a78ad
child 50764 5637aca18f1d
permissions -rw-r--r--
8204375: Add TimeUnit#convert(Duration) Reviewed-by: martin, scolebourne, plevart, rriggs
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
50477
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
    38
import java.time.Duration;
35640
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
    39
import java.time.temporal.ChronoUnit;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
    40
import java.util.Objects;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    43
 * A {@code TimeUnit} represents time durations at a given unit of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * granularity and provides utility methods to convert across units,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and to perform timing and delay operations in these units.  A
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    46
 * {@code TimeUnit} does not maintain time information, but only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * helps organize and use time representations that may be maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * separately across various contexts.  A nanosecond is defined as one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * thousandth of a microsecond, a microsecond as one thousandth of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * millisecond, a millisecond as one thousandth of a second, a minute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * as sixty seconds, an hour as sixty minutes, and a day as twenty four
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    54
 * <p>A {@code TimeUnit} is mainly used to inform time-based methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * how a given timing parameter should be interpreted. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the following code will timeout in 50 milliseconds if the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * java.util.concurrent.locks.Lock lock} is not available:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    59
 * <pre> {@code
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    60
 * Lock lock = ...;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    61
 * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre>
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    62
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * while this code will timeout in 50 seconds:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    64
 * <pre> {@code
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    65
 * Lock lock = ...;
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    66
 * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Note however, that there is no guarantee that a particular timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * implementation will be able to notice the passage of time at the
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
    70
 * same granularity as the given {@code TimeUnit}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public enum TimeUnit {
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    76
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    77
     * Time unit representing one thousandth of a microsecond.
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    78
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
    79
    NANOSECONDS(TimeUnit.NANO_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    80
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    81
     * Time unit representing one thousandth of a millisecond.
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    82
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
    83
    MICROSECONDS(TimeUnit.MICRO_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    84
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    85
     * Time unit representing one thousandth of a second.
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    86
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
    87
    MILLISECONDS(TimeUnit.MILLI_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    88
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    89
     * Time unit representing one second.
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    90
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
    91
    SECONDS(TimeUnit.SECOND_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    92
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    93
     * Time unit representing sixty seconds.
28777
01aad659d989 8072456: @since tags missing from TimeUnit
sla
parents: 25859
diff changeset
    94
     * @since 1.6
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    95
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
    96
    MINUTES(TimeUnit.MINUTE_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
    97
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
    98
     * Time unit representing sixty minutes.
28777
01aad659d989 8072456: @since tags missing from TimeUnit
sla
parents: 25859
diff changeset
    99
     * @since 1.6
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
   100
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   101
    HOURS(TimeUnit.HOUR_SCALE),
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
   102
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
   103
     * Time unit representing twenty four hours.
28777
01aad659d989 8072456: @since tags missing from TimeUnit
sla
parents: 25859
diff changeset
   104
     * @since 1.6
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18768
diff changeset
   105
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   106
    DAYS(TimeUnit.DAY_SCALE);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   107
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   108
    // Scales as constants
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   109
    private static final long NANO_SCALE   = 1L;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   110
    private static final long MICRO_SCALE  = 1000L * NANO_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   111
    private static final long MILLI_SCALE  = 1000L * MICRO_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   112
    private static final long SECOND_SCALE = 1000L * MILLI_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   113
    private static final long MINUTE_SCALE = 60L * SECOND_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   114
    private static final long HOUR_SCALE   = 60L * MINUTE_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   115
    private static final long DAY_SCALE    = 24L * HOUR_SCALE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   116
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   117
    /*
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   118
     * Instances cache conversion ratios and saturation cutoffs for
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   119
     * the units up through SECONDS. Other cases compute them, in
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   120
     * method cvt.
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   121
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   123
    private final long scale;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   124
    private final long maxNanos;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   125
    private final long maxMicros;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   126
    private final long maxMillis;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   127
    private final long maxSecs;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   128
    private final long microRatio;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   129
    private final int milliRatio;   // fits in 32 bits
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   130
    private final int secRatio;     // fits in 32 bits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   132
    private TimeUnit(long s) {
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   133
        this.scale = s;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   134
        this.maxNanos = Long.MAX_VALUE / s;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   135
        long ur = (s >= MICRO_SCALE) ? (s / MICRO_SCALE) : (MICRO_SCALE / s);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   136
        this.microRatio = ur;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   137
        this.maxMicros = Long.MAX_VALUE / ur;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   138
        long mr = (s >= MILLI_SCALE) ? (s / MILLI_SCALE) : (MILLI_SCALE / s);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   139
        this.milliRatio = (int)mr;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   140
        this.maxMillis = Long.MAX_VALUE / mr;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   141
        long sr = (s >= SECOND_SCALE) ? (s / SECOND_SCALE) : (SECOND_SCALE / s);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   142
        this.secRatio = (int)sr;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   143
        this.maxSecs = Long.MAX_VALUE / sr;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   144
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   147
     * General conversion utility.
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   148
     *
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   149
     * @param d duration
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   150
     * @param dst result unit scale
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   151
     * @param src source unit scale
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   153
    private static long cvt(long d, long dst, long src) {
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   154
        long r, m;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   155
        if (src == dst)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   156
            return d;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   157
        else if (src < dst)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   158
            return d / (dst / src);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   159
        else if (d > (m = Long.MAX_VALUE / (r = src / dst)))
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   160
            return Long.MAX_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   161
        else if (d < -m)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   162
            return Long.MIN_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   163
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   164
            return d * r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   168
     * Converts the given time duration in the given unit to this unit.
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   169
     * Conversions from finer to coarser granularities truncate, so
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   170
     * lose precision. For example, converting {@code 999} milliseconds
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   171
     * to seconds results in {@code 0}. Conversions from coarser to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   172
     * finer granularities with arguments that would numerically
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   173
     * overflow saturate to {@code Long.MIN_VALUE} if negative or
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   174
     * {@code Long.MAX_VALUE} if positive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>For example, to convert 10 minutes to milliseconds, use:
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   177
     * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   179
     * @param sourceDuration the time duration in the given {@code sourceUnit}
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   180
     * @param sourceUnit the unit of the {@code sourceDuration} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return the converted duration in this unit,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   182
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   183
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public long convert(long sourceDuration, TimeUnit sourceUnit) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   186
        switch (this) {
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   187
        case NANOSECONDS:  return sourceUnit.toNanos(sourceDuration);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   188
        case MICROSECONDS: return sourceUnit.toMicros(sourceDuration);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   189
        case MILLISECONDS: return sourceUnit.toMillis(sourceDuration);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   190
        case SECONDS:      return sourceUnit.toSeconds(sourceDuration);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   191
        default: return cvt(sourceDuration, scale, sourceUnit.scale);
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
50477
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   196
     * Converts the given time duration to this unit.
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   197
     *
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   198
     * <p>For any TimeUnit {@code unit},
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   199
     * {@code unit.convert(Duration.ofNanos(n))}
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   200
     * is equivalent to
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   201
     * {@code unit.convert(n, NANOSECONDS)}, and
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   202
     * {@code unit.convert(Duration.of(n, unit.toChronoUnit()))}
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   203
     * is equivalent to {@code n} (in the absence of overflow).
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   204
     *
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   205
     * @param duration the time duration
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   206
     * @return the converted duration in this unit,
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   207
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   208
     * or {@code Long.MAX_VALUE} if it would positively overflow.
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   209
     * @throws NullPointerException if {@code duration} is null
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   210
     * @see Duration#of(long,TemporalUnit)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   211
     * @since 11
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   212
     */
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   213
    public long convert(Duration duration) {
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   214
        long secs = duration.getSeconds();
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   215
        int nano = duration.getNano();
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   216
        if (secs < 0 && nano > 0) {
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   217
            // use representation compatible with integer division
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   218
            secs++;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   219
            nano -= SECOND_SCALE;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   220
        }
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   221
        final long s, nanoVal;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   222
        // Optimize for the common case - NANOSECONDS without overflow
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   223
        if (this == NANOSECONDS)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   224
            nanoVal = nano;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   225
        else if ((s = scale) < SECOND_SCALE)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   226
            nanoVal = nano / s;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   227
        else if (this == SECONDS)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   228
            return secs;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   229
        else
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   230
            return secs / secRatio;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   231
        long val = secs * secRatio + nanoVal;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   232
        return ((secs < maxSecs && secs > -maxSecs) ||
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   233
                (secs == maxSecs && val > 0) ||
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   234
                (secs == -maxSecs && val < 0))
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   235
            ? val
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   236
            : (secs > 0) ? Long.MAX_VALUE : Long.MIN_VALUE;
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   237
    }
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   238
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   239
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   240
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   241
     * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   244
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   245
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public long toNanos(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   248
        long s, m;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   249
        if ((s = scale) == NANO_SCALE)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   250
            return duration;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   251
        else if (duration > (m = maxNanos))
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   252
            return Long.MAX_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   253
        else if (duration < -m)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   254
            return Long.MIN_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   255
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   256
            return duration * s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   260
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   261
     * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   264
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   265
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public long toMicros(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   268
        long s, m;
50477
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   269
        if ((s = scale) <= MICRO_SCALE)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   270
            return (s == MICRO_SCALE) ? duration : duration / microRatio;
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   271
        else if (duration > (m = maxMicros))
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   272
            return Long.MAX_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   273
        else if (duration < -m)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   274
            return Long.MIN_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   275
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   276
            return duration * microRatio;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   280
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   281
     * {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   284
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   285
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public long toMillis(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   288
        long s, m;
50477
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   289
        if ((s = scale) <= MILLI_SCALE)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   290
            return (s == MILLI_SCALE) ? duration : duration / milliRatio;
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   291
        else if (duration > (m = maxMillis))
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   292
            return Long.MAX_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   293
        else if (duration < -m)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   294
            return Long.MIN_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   295
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   296
            return duration * milliRatio;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   300
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   301
     * {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   304
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   305
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public long toSeconds(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   308
        long s, m;
50477
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   309
        if ((s = scale) <= SECOND_SCALE)
cb0efe0cc20e 8204375: Add TimeUnit#convert(Duration)
dl
parents: 47307
diff changeset
   310
            return (s == SECOND_SCALE) ? duration : duration / secRatio;
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   311
        else if (duration > (m = maxSecs))
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   312
            return Long.MAX_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   313
        else if (duration < -m)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   314
            return Long.MIN_VALUE;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   315
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   316
            return duration * secRatio;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   320
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   321
     * {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   324
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   325
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public long toMinutes(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   329
        return cvt(duration, MINUTE_SCALE, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   333
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   334
     * {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the converted duration,
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   337
     * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   338
     * or {@code Long.MAX_VALUE} if it would positively overflow.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public long toHours(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   342
        return cvt(duration, HOUR_SCALE, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   346
     * Equivalent to
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   347
     * {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param duration the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return the converted duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public long toDays(long duration) {
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   353
        return cvt(duration, DAY_SCALE, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Utility to compute the excess-nanosecond argument to wait,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * sleep, join.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param d the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param m the number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return the number of nanoseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
36732
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   363
    private int excessNanos(long d, long m) {
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   364
        long s;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   365
        if ((s = scale) == NANO_SCALE)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   366
            return (int)(d - (m * MILLI_SCALE));
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   367
        else if (s == MICRO_SCALE)
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   368
            return (int)((d * 1000L) - (m * MILLI_SCALE));
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   369
        else
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   370
            return 0;
7e6686d3f98a 8152083: Optimize TimeUnit conversions
dl
parents: 35640
diff changeset
   371
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   374
     * Performs a timed {@link Object#wait(long, int) Object.wait}
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   375
     * using this time unit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * This is a convenience method that converts timeout arguments
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   377
     * into the form required by the {@code Object.wait} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 36732
diff changeset
   379
     * <p>For example, you could implement a blocking {@code poll} method
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 36732
diff changeset
   380
     * (see {@link BlockingQueue#poll(long, TimeUnit) BlockingQueue.poll})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * using:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 28777
diff changeset
   383
     * <pre> {@code
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   384
     * public E poll(long timeout, TimeUnit unit)
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   385
     *     throws InterruptedException {
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   386
     *   synchronized (lock) {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   387
     *     while (isEmpty()) {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   388
     *       unit.timedWait(lock, timeout);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   389
     *       ...
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   390
     *     }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   391
     *   }
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   392
     * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param obj the object to wait on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param timeout the maximum time to wait. If less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * or equal to zero, do not wait at all.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   397
     * @throws InterruptedException if interrupted while waiting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public void timedWait(Object obj, long timeout)
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   400
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            long ms = toMillis(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int ns = excessNanos(timeout, ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            obj.wait(ms, ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   409
     * Performs a timed {@link Thread#join(long, int) Thread.join}
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   410
     * using this time unit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This is a convenience method that converts time arguments into the
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   412
     * form required by the {@code Thread.join} method.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   413
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param thread the thread to wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param timeout the maximum time to wait. If less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * or equal to zero, do not wait at all.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   417
     * @throws InterruptedException if interrupted while waiting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public void timedJoin(Thread thread, long timeout)
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   420
            throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            long ms = toMillis(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            int ns = excessNanos(timeout, ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            thread.join(ms, ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   429
     * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   430
     * this time unit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * This is a convenience method that converts time arguments into the
18768
f2638f396c41 8019481: Sync misc j.u.c classes from 166 to tl
psandoz
parents: 14325
diff changeset
   432
     * form required by the {@code Thread.sleep} method.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   433
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param timeout the minimum time to sleep. If less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * or equal to zero, do not sleep at all.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   436
     * @throws InterruptedException if interrupted while sleeping
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public void sleep(long timeout) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            long ms = toMillis(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            int ns = excessNanos(timeout, ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Thread.sleep(ms, ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
35640
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   446
    /**
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   447
     * Converts this {@code TimeUnit} to the equivalent {@code ChronoUnit}.
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   448
     *
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   449
     * @return the converted equivalent ChronoUnit
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   450
     * @since 9
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   451
     */
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   452
    public ChronoUnit toChronoUnit() {
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   453
        switch (this) {
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   454
        case NANOSECONDS:  return ChronoUnit.NANOS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   455
        case MICROSECONDS: return ChronoUnit.MICROS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   456
        case MILLISECONDS: return ChronoUnit.MILLIS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   457
        case SECONDS:      return ChronoUnit.SECONDS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   458
        case MINUTES:      return ChronoUnit.MINUTES;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   459
        case HOURS:        return ChronoUnit.HOURS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   460
        case DAYS:         return ChronoUnit.DAYS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   461
        default: throw new AssertionError();
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   462
        }
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   463
    }
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   464
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   465
    /**
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   466
     * Converts a {@code ChronoUnit} to the equivalent {@code TimeUnit}.
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   467
     *
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   468
     * @param chronoUnit the ChronoUnit to convert
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   469
     * @return the converted equivalent TimeUnit
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   470
     * @throws IllegalArgumentException if {@code chronoUnit} has no
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   471
     *         equivalent TimeUnit
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   472
     * @throws NullPointerException if {@code chronoUnit} is null
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   473
     * @since 9
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   474
     */
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   475
    public static TimeUnit of(ChronoUnit chronoUnit) {
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   476
        switch (Objects.requireNonNull(chronoUnit, "chronoUnit")) {
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   477
        case NANOS:   return TimeUnit.NANOSECONDS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   478
        case MICROS:  return TimeUnit.MICROSECONDS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   479
        case MILLIS:  return TimeUnit.MILLISECONDS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   480
        case SECONDS: return TimeUnit.SECONDS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   481
        case MINUTES: return TimeUnit.MINUTES;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   482
        case HOURS:   return TimeUnit.HOURS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   483
        case DAYS:    return TimeUnit.DAYS;
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   484
        default:
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   485
            throw new IllegalArgumentException(
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   486
                "No TimeUnit equivalent for " + chronoUnit);
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   487
        }
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   488
    }
4adf536f5359 8141452: Convert between TimeUnit and ChronoUnit
dl
parents: 32991
diff changeset
   489
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
}