1 package org.djunits.value.vdouble.scalar; 2 3 import org.djunits.unit.DurationUnit; 4 import org.djunits.unit.TimeUnit; 5 6 /** 7 * Easy access methods for the Absolute Time DoubleScalar. Instead of: 8 * 9 * <pre> 10 * DoubleScalar.Abs<TimeUnit> value = new DoubleScalar.Abs<TimeUnit>(100.0, TimeUnit.SI); 11 * </pre> 12 * 13 * we can now write: 14 * 15 * <pre> 16 * Time value = new Time(100.0, TimeUnit.BASE); 17 * </pre> 18 * 19 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit 20 * used are compatible. 21 * <p> 22 * Note that when the offset of a stored absolute Time becomes large, precision of a double might not be enough for the required 23 * resolution of a Time. A double has around 16 significant digits (52 bit mantissa). This means that when we need to have a 24 * double Time with TimeUnit.BASE as its unit, the largest value where the ms precision is reached is 2^51 = 2.3E15, which is 25 * around 71000 years. This is sufficient to store a date in the 21st Century with a BASE or an Epoch offset precise to a 26 * microsecond. 27 * <p> 28 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br> 29 * All rights reserved. <br> 30 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 31 * <p> 32 * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $, 33 * initial version Sep 1, 2015 <br> 34 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 35 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 36 */ 37 public class Time extends AbstractDoubleScalarAbs<TimeUnit, Time, DurationUnit, Duration> 38 { 39 /** */ 40 private static final long serialVersionUID = 20150901L; 41 42 /** constant with value zero. */ 43 public static final Time ZERO = new Time(0.0, TimeUnit.BASE); 44 45 /** 46 * Construct Time scalar. 47 * @param value double value 48 * @param unit unit for the double value 49 */ 50 public Time(final double value, final TimeUnit unit) 51 { 52 super(value, unit); 53 } 54 55 /** 56 * Construct Time scalar. 57 * @param value Scalar from which to construct this instance 58 */ 59 public Time(final Time value) 60 { 61 super(value); 62 } 63 64 /** {@inheritDoc} */ 65 @Override 66 public final Time instantiateAbs(final double value, final TimeUnit unit) 67 { 68 return new Time(value, unit); 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 public final Duration instantiateRel(final double value, final DurationUnit unit) 74 { 75 return new Duration(value, unit); 76 } 77 78 /** 79 * Construct %TypeAbsl% scalar. 80 * @param value double value in SI units 81 * @return the new scalar with the SI value 82 */ 83 public static final Time createSI(final double value) 84 { 85 return new Time(value, TimeUnit.BASE); 86 } 87 88 /** 89 * Interpolate between two values. 90 * @param zero the low value 91 * @param one the high value 92 * @param ratio the ratio between 0 and 1, inclusive 93 * @return a Scalar at the ratio between 94 */ 95 public static Time interpolate(final Time zero, final Time one, final double ratio) 96 { 97 return new Time(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit()); 98 } 99 100 /** 101 * Return the maximum value of two absolute scalars. 102 * @param a1 the first scalar 103 * @param a2 the second scalar 104 * @return the maximum value of two absolute scalars 105 */ 106 public static Time max(final Time a1, final Time a2) 107 { 108 return (a1.gt(a2)) ? a1 : a2; 109 } 110 111 /** 112 * Return the maximum value of more than two absolute scalars. 113 * @param a1 the first scalar 114 * @param a2 the second scalar 115 * @param an the other scalars 116 * @return the maximum value of more than two absolute scalars 117 */ 118 public static Time max(final Time a1, final Time a2, final Time... an) 119 { 120 Time maxa = (a1.gt(a2)) ? a1 : a2; 121 for (Time a : an) 122 { 123 if (a.gt(maxa)) 124 { 125 maxa = a; 126 } 127 } 128 return maxa; 129 } 130 131 /** 132 * Return the minimum value of two absolute scalars. 133 * @param a1 the first scalar 134 * @param a2 the second scalar 135 * @return the minimum value of two absolute scalars 136 */ 137 public static Time min(final Time a1, final Time a2) 138 { 139 return (a1.lt(a2)) ? a1 : a2; 140 } 141 142 /** 143 * Return the minimum value of more than two absolute scalars. 144 * @param a1 the first scalar 145 * @param a2 the second scalar 146 * @param an the other scalars 147 * @return the minimum value of more than two absolute scalars 148 */ 149 public static Time min(final Time a1, final Time a2, final Time... an) 150 { 151 Time mina = (a1.lt(a2)) ? a1 : a2; 152 for (Time a : an) 153 { 154 if (a.lt(mina)) 155 { 156 mina = a; 157 } 158 } 159 return mina; 160 } 161 162 }