View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import org.djunits.unit.LengthUnit;
4   import org.djunits.unit.PositionUnit;
5   
6   /**
7    * Easy access methods for the Position FloatScalar. Instead of:
8    * 
9    * <pre>
10   * FloatScalar.Abs&lt;PositionUnit&gt; value = new FloatScalar.Abs&lt;PositionUnit&gt;(100.0, PositionUnit.SI);
11   * </pre>
12   * 
13   * we can now write:
14   * 
15   * <pre>
16   * FloatPosition value = new FloatPosition(100.0, PositionUnit.SI);
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   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
23   * All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * <p>
26   * $LastChangedDate: 2017-01-30 14:23:11 +0100 (Mon, 30 Jan 2017) $, @version $Revision: 234 $, by $Author: averbraeck $,
27   * initial version Sep 1, 2015 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30   */
31  public class FloatPosition extends AbstractFloatScalarAbs<PositionUnit, FloatPosition, LengthUnit, FloatLength>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** constant with value zero. */
37      public static final FloatPosition ZERO = new FloatPosition(0.0f, PositionUnit.BASE);
38  
39      /**
40       * Construct FloatPosition scalar.
41       * @param value float value
42       * @param unit unit for the float value
43       */
44      public FloatPosition(final float value, final PositionUnit unit)
45      {
46          super(value, unit);
47      }
48  
49      /**
50       * Construct FloatPosition scalar using a double value.
51       * @param value double value
52       * @param unit unit for the resulting float value
53       */
54      public FloatPosition(final double value, final PositionUnit unit)
55      {
56          super((float) value, unit);
57      }
58  
59      /**
60       * Construct FloatPosition scalar.
61       * @param value Scalar from which to construct this instance
62       */
63      public FloatPosition(final FloatPosition value)
64      {
65          super(value);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final FloatPosition instantiateAbs(final float value, final PositionUnit unit)
71      {
72          return new FloatPosition(value, unit);
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final FloatLength instantiateRel(final float value, final LengthUnit unit)
78      {
79          return new FloatLength(value, unit);
80      }
81  
82      /**
83       * Construct FloatPosition scalar.
84       * @param value float value in BASE units
85       * @return the new scalar with the BASE value
86       */
87      public static final FloatPosition createSI(final float value)
88      {
89          return new FloatPosition(value, PositionUnit.BASE);
90      }
91  
92      /**
93       * Interpolate between two values.
94       * @param zero the low value
95       * @param one the high value
96       * @param ratio the ratio between 0 and 1, inclusive
97       * @return a Scalar at the ratio between
98       */
99      public static FloatPosition interpolate(final FloatPosition zero, final FloatPosition one, final float ratio)
100     {
101         return new FloatPosition(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
102     }
103 
104     /**
105      * Return the maximum value of two absolute scalars.
106      * @param a1 the first scalar
107      * @param a2 the second scalar
108      * @return the maximum value of two absolute scalars
109      */
110     public static FloatPosition max(final FloatPosition a1, final FloatPosition a2)
111     {
112         return (a1.gt(a2)) ? a1 : a2;
113     }
114 
115     /**
116      * Return the maximum value of more than two absolute scalars.
117      * @param a1 the first scalar
118      * @param a2 the second scalar
119      * @param an the other scalars
120      * @return the maximum value of more than two absolute scalars
121      */
122     public static FloatPosition max(final FloatPosition a1, final FloatPosition a2, final FloatPosition... an)
123     {
124         FloatPosition maxa = (a1.gt(a2)) ? a1 : a2;
125         for (FloatPosition a : an)
126         {
127             if (a.gt(maxa))
128             {
129                 maxa = a;
130             }
131         }
132         return maxa;
133     }
134 
135     /**
136      * Return the minimum value of two absolute scalars.
137      * @param a1 the first scalar
138      * @param a2 the second scalar
139      * @return the minimum value of two absolute scalars
140      */
141     public static FloatPosition min(final FloatPosition a1, final FloatPosition a2)
142     {
143         return (a1.lt(a2)) ? a1 : a2;
144     }
145 
146     /**
147      * Return the minimum value of more than two absolute scalars.
148      * @param a1 the first scalar
149      * @param a2 the second scalar
150      * @param an the other scalars
151      * @return the minimum value of more than two absolute scalars
152      */
153     public static FloatPosition min(final FloatPosition a1, final FloatPosition a2, final FloatPosition... an)
154     {
155         FloatPosition mina = (a1.lt(a2)) ? a1 : a2;
156         for (FloatPosition a : an)
157         {
158             if (a.lt(mina))
159             {
160                 mina = a;
161             }
162         }
163         return mina;
164     }
165 
166 }