    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3.time;
    019
    020import java.time.Duration;
    021import java.time.temporal.ChronoUnit;
    022import java.util.Objects;
    023import java.util.concurrent.TimeUnit;
    024
    025import org.apache.commons.lang3.ObjectUtils;
    026import org.apache.commons.lang3.Range;
    027import org.apache.commons.lang3.function.FailableBiConsumer;
    028import org.apache.commons.lang3.math.NumberUtils;
    029
    030/**
    031 * Utilities for {@link Duration}.
    032 *
    033 * @since 3.12.0
    034 */
    035public class DurationUtils {
    036
    037    /**
    038     * An Integer Range that accepts Longs.
    039     */
    040    static final Range<Long> LONG_TO_INT_RANGE = Range.between(NumberUtils.LONG_INT_MIN_VALUE,
    041            NumberUtils.LONG_INT_MAX_VALUE);
    042
    043    /**
    044     * Accepts the function with the duration as a long milliseconds and int nanoseconds.
    045     *
    046     * @param <T> The function exception.
    047     * @param consumer Accepting function.
    048     * @param duration The duration to pick apart.
    049     * @throws T See the function signature.
    050     */
    051    public static <T extends Throwable> void accept(final FailableBiConsumer<Long, Integer, T> consumer, final Duration duration)
    052            throws T {
    053        if (consumer != null && duration != null) {
    054            consumer.accept(duration.toMillis(), getNanosOfMiili(duration));
    055        }
    056    }
    057
    058    /**
    059     * Gets the nanosecond part of a Duration converted to milliseconds.
    060     * <p>
    061     * Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example,
    062     * {@link Object#wait(long, int)} and {@link Thread#sleep(long, int)}.
    063     * </p>
    064     * <p>
    065     * Note that is this different from {@link Duration#getNano()} because a duration are seconds and nanoseconds.
    066     * </p>
    067     *
    068     * @param duration The duration to query.
    069     * @return nanoseconds between 0 and 999,999.
    070     */
    071    public static int getNanosOfMiili(final Duration duration) {
    072        return duration.getNano() % 1_000_000;
    073    }
    074
    075    /**
    076     * Tests whether the given Duration is positive (&gt;0).
    077     *
    078     * @param duration the value to test
    079     * @return whether the given Duration is positive (&gt;0).
    080     */
    081    public static boolean isPositive(final Duration duration) {
    082        return !duration.isNegative() && !duration.isZero();
    083    }
    084
    085    /**
    086     * Converts a {@link TimeUnit} to a {@link ChronoUnit}.
    087     *
    088     * @param timeUnit A non-null TimeUnit.
    089     * @return The corresponding ChronoUnit.
    090     */
    091    static ChronoUnit toChronoUnit(final TimeUnit timeUnit) {
    092        // TODO when using Java >= 9: Use TimeUnit.toChronoUnit().
    093        switch (Objects.requireNonNull(timeUnit)) {
    094        case NANOSECONDS:
    095            return ChronoUnit.NANOS;
    096        case MICROSECONDS:
    097            return ChronoUnit.MICROS;
    098        case MILLISECONDS:
    099            return ChronoUnit.MILLIS;
    100        case SECONDS:
    101            return ChronoUnit.SECONDS;
    102        case MINUTES:
    103            return ChronoUnit.MINUTES;
    104        case HOURS:
    105            return ChronoUnit.HOURS;
    106        case DAYS:
    107            return ChronoUnit.DAYS;
    108        default:
    109            throw new IllegalArgumentException(timeUnit.toString());
    110        }
    111    }
    112
    113    /**
    114     * Converts an amount and TimeUnit into a Duration.
    115     *
    116     * @param amount   the amount of the duration, measured in terms of the unit, positive or negative
    117     * @param timeUnit the unit that the duration is measured in, must have an exact duration, not null
    118     * @return a Duration.
    119     */
    120    public static Duration toDuration(final long amount, final TimeUnit timeUnit) {
    121        return Duration.of(amount, toChronoUnit(timeUnit));
    122    }
    123
    124    /**
    125     * Converts a Duration to milliseconds bound to an int (instead of a long).
    126     * <p>
    127     * Handy for low-level APIs that take millisecond timeouts in ints rather than longs.
    128     * </p>
    129     * <ul>
    130     * <li>If the duration milliseconds are greater than {@link Integer#MAX_VALUE}, then return
    131     * {@link Integer#MAX_VALUE}.</li>
    132     * <li>If the duration milliseconds are lesser than {@link Integer#MIN_VALUE}, then return
    133     * {@link Integer#MIN_VALUE}.</li>
    134     * </ul>
    135     *
    136     * @param duration The duration to convert, not null.
    137     * @return int milliseconds.
    138     */
    139    public static int toMillisInt(final Duration duration) {
    140        Objects.requireNonNull(duration, "duration");
    141        // intValue() does not do a narrowing conversion here
    142        return LONG_TO_INT_RANGE.fit(Long.valueOf(duration.toMillis())).intValue();
    143    }
    144
    145    /**
    146     * Returns the given non-null value or {@link Duration#ZERO} if null.
    147     *
    148     * @param duration The duration to test.
    149     * @return The given duration or {@link Duration#ZERO}.
    150     */
    151    public static Duration zeroIfNull(final Duration duration) {
    152        return ObjectUtils.defaultIfNull(duration, Duration.ZERO);
    153    }
    154
    155}