TimeUtil.java Source code

Java tutorial

Introduction

Here is the source code for TimeUtil.java

Source

/*
 * @(#)TimeUtil.java   1.0 Mar 7, 2008
 *
 *   The MIT License
 *
 *   Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */
//package org.eoti.util;

import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TimeUtil {
    public static Unit Nanosecond = Unit.Nanosecond;
    public static Unit Microsecond = Unit.Microsecond;
    public static Unit Millisecond = Unit.Millisecond;
    public static Unit Centisecond = Unit.Centisecond;
    public static Unit Second = Unit.Second;
    public static Unit Minute = Unit.Minute;
    public static Unit Hour = Unit.Hour;
    public static Unit Day = Unit.Day;

    public enum Unit {
        Nanosecond("ns", NANOSECONDS, 1), Microsecond('\u00B5' + "s", MICROSECONDS, 1), Millisecond("ms",
                MILLISECONDS, 1), Centisecond("cs", MILLISECONDS, 10), Second("sec", SECONDS, 1), Minute("min",
                        SECONDS, 60), Hour("hr", SECONDS, 60 * 60), Day("day", SECONDS, 60 * 60 * 24);

        Unit(String friendlyName, TimeUnit unit, long multiplier) {
            this.friendlyName = friendlyName;
            this.unit = unit;
            this.multiplier = multiplier;
        }

        protected String friendlyName;
        protected TimeUnit unit;
        protected long multiplier;

        public long convertFrom(long amount, Unit unitOfAmount) {
            if (unit.equals(unitOfAmount.unit))
                return amount * unitOfAmount.multiplier / multiplier;

            return unit.convert(amount * unitOfAmount.multiplier, unitOfAmount.unit) / multiplier;
        }

        public void sleep(long timeout) {
            try {
                unit.sleep(timeout * multiplier);
            } catch (InterruptedException ie) {
                // no-op
            }
        }

        public void timedJoin(Thread thread, long timeout) {
            try {
                unit.timedJoin(thread, timeout * multiplier);
            } catch (InterruptedException ie) {
                // no-op
            }
        }

        public void timedWait(Object obj, long timeout) {
            try {
                unit.timedWait(obj, timeout * multiplier);
            } catch (InterruptedException ie) {
                // no-op
            }
        }

        public String toString(long amount) {
            return String.format("%d %ss", amount, toString());
        }

        public String friendly(long amount) {
            return String.format("%d%s", amount, friendlyName);
        }
    }

    public static String friendly(long amount, Unit unitOfAmount, boolean includeCentiseconds) {
        long nanos = Nanosecond.convertFrom(amount, unitOfAmount);
        List<Unit> units = Arrays.asList(Unit.values());
        Collections.reverse(units);
        StringBuilder sb = new StringBuilder();
        for (Unit unit : units) {
            if ((!includeCentiseconds) && unit.equals(Centisecond))
                continue;

            long unitAmount = unit.convertFrom(nanos, Nanosecond);
            nanos -= Nanosecond.convertFrom(unitAmount, unit);
            if (unitAmount > 0)
                sb.append(String.format("%s ", unit.friendly(unitAmount)));
        }

        return sb.toString().trim();
    }

    public static String unfriendly(long amount, Unit unitOfAmount, boolean includeCentiseconds) {
        long nanos = Nanosecond.convertFrom(amount, unitOfAmount);
        List<Unit> units = Arrays.asList(Unit.values());
        Collections.reverse(units);
        StringBuilder sb = new StringBuilder();
        for (Unit unit : units) {
            if ((!includeCentiseconds) && unit.equals(Centisecond))
                continue;

            long unitAmount = unit.convertFrom(nanos, Nanosecond);
            nanos -= Nanosecond.convertFrom(unitAmount, unit);
            if (unitAmount > 0)
                sb.append(String.format("%s ", unit.toString(unitAmount)));
        }

        return sb.toString().trim();
    }
}