Coverage Report - com.liquidatom.derbyscore.domain.Duration
 
Classes in this File Line Coverage Branch Coverage Complexity
Duration
68%
20/29
63%
14/22
2.857
 
 1  
 package com.liquidatom.derbyscore.domain;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.concurrent.TimeUnit;
 5  
 import javax.annotation.concurrent.Immutable;
 6  
 import javax.annotation.concurrent.ThreadSafe;
 7  
 
 8  
 /**
 9  
  * A basic immutable data structure which holds a scalar and a unit for measuring the duration of time.
 10  
  *
 11  
  * @author Russell Francis (russ@metro-six.com)
 12  
  */
 13  0
 @Immutable
 14  
 @ThreadSafe
 15  
 public class Duration implements Serializable, Comparable<Duration> {
 16  
 
 17  
     final private long time;
 18  
     final private TimeUnit unit;
 19  
 
 20  
     /**
 21  
      * Construct a new Duration instance with the provided length and unit.
 22  
      *
 23  
      * @param time The number of units of duration this instance will represent.
 24  
      * @param unit The unit of time this duration will represent.
 25  
      */
 26  
     public Duration(final long time, final TimeUnit unit) {
 27  6
         super();
 28  6
         if (unit == null) {
 29  1
             throw new IllegalArgumentException("The parameter unit must be non-null.");
 30  
         }
 31  5
         if (unit.compareTo(TimeUnit.MILLISECONDS) < 0) {
 32  1
             throw new IllegalArgumentException("The parameter unit must be milliseconds or larger.");
 33  
         }
 34  4
         this.time = time;
 35  4
         this.unit = unit;
 36  4
     }
 37  
 
 38  
     /**
 39  
      * Get the length represented by this duration.  This number will be in the units
 40  
      * identified by {@link #getUnit()}.
 41  
      *
 42  
      * @return The length of this duration.
 43  
      */
 44  
     public long getTime() {
 45  1
         return time;
 46  
     }
 47  
 
 48  
     /**
 49  
      * Get the unit that the length of this duration is represented in.
 50  
      *
 51  
      * @return The unit that the length of this duration is represented in.
 52  
      */
 53  
     public TimeUnit getUnit() {
 54  74
         return unit;
 55  
     }
 56  
 
 57  
     /**
 58  
      * Get the length of time represented in milliseconds.
 59  
      *
 60  
      * @return The length of time represented in milliseconds.
 61  
      */
 62  
     public long getTimeInMilliseconds() {
 63  19
         long result = time;
 64  
 
 65  19
         if (TimeUnit.DAYS.equals(getUnit())) {
 66  1
             result *= 24 * 60 * 60 * 1000;
 67  
         }
 68  18
         else if (TimeUnit.HOURS.equals(getUnit())) {
 69  0
             result *= 60 * 60 * 1000;
 70  
         }
 71  18
         else if (TimeUnit.MINUTES.equals(getUnit())) {
 72  0
             result *= 60 * 1000;
 73  
         }
 74  18
         else if (TimeUnit.SECONDS.equals(getUnit())) {
 75  12
             result *= 1000;
 76  
         }
 77  19
         return result;
 78  
     }
 79  
 
 80  
     /**
 81  
      * {@inheritDoc}
 82  
      */
 83  
     public int compareTo(final Duration o) {
 84  9
         long result = getTimeInMilliseconds() - o.getTimeInMilliseconds();
 85  9
         return result > 0 ? 1 : result < 0 ? -1 : 0;
 86  
     }
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      */
 91  
     @Override
 92  
     public boolean equals(Object o) {
 93  0
         if (o != null && o instanceof Duration) {
 94  0
             Duration that = (Duration)o;
 95  0
             return getTimeInMilliseconds() == that.getTimeInMilliseconds();
 96  
         }
 97  0
         return false;
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     @Override
 104  
     public int hashCode() {
 105  0
         long value = getTimeInMilliseconds();
 106  0
         return 413 * (int) (value ^ (value >>> 32));
 107  
     }
 108  
 }