FloatTime.java
package org.djunits.value.vfloat.scalar;
import org.djunits.unit.DurationUnit;
import org.djunits.unit.TimeUnit;
/**
* Easy access methods for the Time FloatScalar. Instead of:
*
* <pre>
* FloatScalar.Abs<TimeUnit> value = new FloatScalar.Abs<TimeUnit>(100.0, TimeUnit.SI);
* </pre>
*
* we can now write:
*
* <pre>
* FloatTime value = new FloatTime(100.0, TimeUnit.SI);
* </pre>
*
* The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
* used are compatible.
* <p>
* Note that when the offset of a stored absolute Time becomes large, precision of a float might not be enough for the required
* resolution of a Time. A float has around 7 significant digits (23 bit mantissa). This means that when we need to have a float
* time that is precise to microseconds, the Time value should not go above 2^22 = 4.0E6. This is <b>not</b> enough to store
* Epoch values that are in the order of magnitude of 2E12 ms! So feeding System.TimeInMillis() to a FloatTime with
* TimeUnit.BASE as its unit is not having the required precision. At best, a FloatTime can store TimeUnit.BASE or
* TimeUnit.EPOCH values with real calendar values with a precision of several minutes.
* <p>
* Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
* All rights reserved. <br>
* BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
* <p>
* $LastChangedDate: 2019-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, by $Author: averbraeck $,
* initial version Sep 1, 2015 <br>
* @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
* @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
*/
public class FloatTime extends AbstractFloatScalarAbs<TimeUnit, FloatTime, DurationUnit, FloatDuration>
{
/** */
private static final long serialVersionUID = 20150901L;
/** constant with value zero. */
public static final FloatTime ZERO = new FloatTime(0.0f, TimeUnit.BASE);
/**
* Construct FloatTime scalar.
* @param value float; float value
* @param unit TimeUnit; unit for the float value
*/
public FloatTime(final float value, final TimeUnit unit)
{
super(value, unit);
}
/**
* Construct FloatTime scalar using a double value.
* @param value double; double value
* @param unit TimeUnit; unit for the resulting float value
*/
public FloatTime(final double value, final TimeUnit unit)
{
super((float) value, unit);
}
/**
* Construct FloatTime scalar.
* @param value FloatTime; Scalar from which to construct this instance
*/
public FloatTime(final FloatTime value)
{
super(value);
}
/** {@inheritDoc} */
@Override
public final FloatTime instantiateAbs(final float value, final TimeUnit unit)
{
return new FloatTime(value, unit);
}
/** {@inheritDoc} */
@Override
public final FloatDuration instantiateRel(final float value, final DurationUnit unit)
{
return new FloatDuration(value, unit);
}
/**
* Construct FloatTime scalar.
* @param value float; float value in BASE units
* @return the new scalar with the BASE value
*/
public static final FloatTime createSI(final float value)
{
return new FloatTime(value, TimeUnit.BASE);
}
/**
* Interpolate between two values.
* @param zero FloatTime; the low value
* @param one FloatTime; the high value
* @param ratio float; the ratio between 0 and 1, inclusive
* @return a Scalar at the ratio between
*/
public static FloatTime interpolate(final FloatTime zero, final FloatTime one, final float ratio)
{
return new FloatTime(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
}
/**
* Return the maximum value of two absolute scalars.
* @param a1 FloatTime; the first scalar
* @param a2 FloatTime; the second scalar
* @return the maximum value of two absolute scalars
*/
public static FloatTime max(final FloatTime a1, final FloatTime a2)
{
return (a1.gt(a2)) ? a1 : a2;
}
/**
* Return the maximum value of more than two absolute scalars.
* @param a1 FloatTime; the first scalar
* @param a2 FloatTime; the second scalar
* @param an FloatTime...; the other scalars
* @return the maximum value of more than two absolute scalars
*/
public static FloatTime max(final FloatTime a1, final FloatTime a2, final FloatTime... an)
{
FloatTime maxa = (a1.gt(a2)) ? a1 : a2;
for (FloatTime a : an)
{
if (a.gt(maxa))
{
maxa = a;
}
}
return maxa;
}
/**
* Return the minimum value of two absolute scalars.
* @param a1 FloatTime; the first scalar
* @param a2 FloatTime; the second scalar
* @return the minimum value of two absolute scalars
*/
public static FloatTime min(final FloatTime a1, final FloatTime a2)
{
return (a1.lt(a2)) ? a1 : a2;
}
/**
* Return the minimum value of more than two absolute scalars.
* @param a1 FloatTime; the first scalar
* @param a2 FloatTime; the second scalar
* @param an FloatTime...; the other scalars
* @return the minimum value of more than two absolute scalars
*/
public static FloatTime min(final FloatTime a1, final FloatTime a2, final FloatTime... an)
{
FloatTime mina = (a1.lt(a2)) ? a1 : a2;
for (FloatTime a : an)
{
if (a.lt(mina))
{
mina = a;
}
}
return mina;
}
}