001    // =================================================================================================
002    // Copyright 2011 Twitter, Inc.
003    // -------------------------------------------------------------------------------------------------
004    // Licensed under the Apache License, Version 2.0 (the "License");
005    // you may not use this work except in compliance with the License.
006    // You may obtain a copy of the License in the LICENSE file, or at:
007    //
008    //  http://www.apache.org/licenses/LICENSE-2.0
009    //
010    // Unless required by applicable law or agreed to in writing, software
011    // distributed under the License is distributed on an "AS IS" BASIS,
012    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    // See the License for the specific language governing permissions and
014    // limitations under the License.
015    // =================================================================================================
016    
017    package com.twitter.common.quantity;
018    
019    import java.util.concurrent.TimeUnit;
020    
021    /**
022     * Provides a unit to allow conversions and unambiguous passing around of time {@link Amount}s.
023     *
024     * @author John Sirois
025     */
026    public enum Time implements Unit<Time> {
027      NANOSECONDS(1, TimeUnit.NANOSECONDS, "ns"),
028      MICROSECONDS(1000, NANOSECONDS, TimeUnit.MICROSECONDS, "us"),
029      MILLISECONDS(1000, MICROSECONDS, TimeUnit.MILLISECONDS, "ms"),
030      SECONDS(1000, MILLISECONDS, TimeUnit.SECONDS, "secs"),
031      MINUTES(60, SECONDS, TimeUnit.MINUTES, "mins"),
032      HOURS(60, MINUTES, TimeUnit.HOURS, "hrs"),
033      DAYS(24, HOURS, TimeUnit.DAYS, "days");
034    
035      private final double multiplier;
036      private final TimeUnit timeUnit;
037      private final String display;
038    
039      private Time(double multiplier, TimeUnit timeUnit, String display) {
040        this.multiplier = multiplier;
041        this.timeUnit = timeUnit;
042        this.display = display;
043      }
044    
045      private Time(double multiplier, Time base, TimeUnit timeUnit, String display) {
046        this(multiplier * base.multiplier, timeUnit, display);
047      }
048    
049      @Override
050      public double multiplier() {
051        return multiplier;
052      }
053    
054      /**
055       * Returns the equivalent {@code TimeUnit}.
056       */
057      public TimeUnit getTimeUnit() {
058        return timeUnit;
059      }
060    
061      @Override
062      public String toString() {
063        return display;
064      }
065    }