View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import org.djunits.unit.LengthUnit;
4   import org.djunits.unit.PositionUnit;
5   
6   /**
7    * Easy access methods for the Absolute Position DoubleScalar. Instead of:
8    * 
9    * <pre>
10   * DoubleScalar.Abs&lt;PositionUnit&gt; value = new DoubleScalar.Abs&lt;PositionUnit&gt;(100.0, PositionUnit.SI);
11   * </pre>
12   * 
13   * we can now write:
14   * 
15   * <pre>
16   * Position value = new Position(100.0, PositionUnit.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   * 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: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, 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 Position extends AbstractDoubleScalarAbs<PositionUnit, Position, LengthUnit, Length>
32  {
33      /** */
34      private static final long serialVersionUID = 20150901L;
35  
36      /** constant with value zero. */
37      public static final Position ZERO = new Position(0.0, PositionUnit.BASE);
38  
39      /**
40       * Construct Position scalar.
41       * @param value double value
42       * @param unit unit for the double value
43       */
44      public Position(final double value, final PositionUnit unit)
45      {
46          super(value, unit);
47      }
48  
49      /**
50       * Construct Position scalar.
51       * @param value Scalar from which to construct this instance
52       */
53      public Position(final Position value)
54      {
55          super(value);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final Position instantiateAbs(final double value, final PositionUnit unit)
61      {
62          return new Position(value, unit);
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final Length instantiateRel(final double value, final LengthUnit unit)
68      {
69          return new Length(value, unit);
70      }
71  
72      /**
73       * Construct %TypeAbsl% scalar.
74       * @param value double value in SI units
75       * @return the new scalar with the SI value
76       */
77      public static final Position createSI(final double value)
78      {
79          return new Position(value, PositionUnit.BASE);
80      }
81  
82      /**
83       * Interpolate between two values.
84       * @param zero the low value
85       * @param one the high value
86       * @param ratio the ratio between 0 and 1, inclusive
87       * @return a Scalar at the ratio between
88       */
89      public static Position interpolate(final Position zero, final Position one, final double ratio)
90      {
91          return new Position(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
92      }
93  
94      /**
95       * Return the maximum value of two absolute scalars.
96       * @param a1 the first scalar
97       * @param a2 the second scalar
98       * @return the maximum value of two absolute scalars
99       */
100     public static Position max(final Position a1, final Position a2)
101     {
102         return (a1.gt(a2)) ? a1 : a2;
103     }
104 
105     /**
106      * Return the maximum value of more than two absolute scalars.
107      * @param a1 the first scalar
108      * @param a2 the second scalar
109      * @param an the other scalars
110      * @return the maximum value of more than two absolute scalars
111      */
112     public static Position max(final Position a1, final Position a2, final Position... an)
113     {
114         Position maxa = (a1.gt(a2)) ? a1 : a2;
115         for (Position a : an)
116         {
117             if (a.gt(maxa))
118             {
119                 maxa = a;
120             }
121         }
122         return maxa;
123     }
124 
125     /**
126      * Return the minimum value of two absolute scalars.
127      * @param a1 the first scalar
128      * @param a2 the second scalar
129      * @return the minimum value of two absolute scalars
130      */
131     public static Position min(final Position a1, final Position a2)
132     {
133         return (a1.lt(a2)) ? a1 : a2;
134     }
135 
136     /**
137      * Return the minimum value of more than two absolute scalars.
138      * @param a1 the first scalar
139      * @param a2 the second scalar
140      * @param an the other scalars
141      * @return the minimum value of more than two absolute scalars
142      */
143     public static Position min(final Position a1, final Position a2, final Position... an)
144     {
145         Position mina = (a1.lt(a2)) ? a1 : a2;
146         for (Position a : an)
147         {
148             if (a.lt(mina))
149             {
150                 mina = a;
151             }
152         }
153         return mina;
154     }
155 
156 }