View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import java.util.regex.Matcher;
4   
5   import javax.annotation.Generated;
6   
7   import org.djunits.Throw;
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.unit.PositionUnit;
10  import org.djunits.value.util.ValueUtil;
11  import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarAbs;
12  
13  /**
14   * Easy access methods for the Absolute Position DoubleScalar.
15   * <p>
16   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
17   * All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
22   */
23  @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2020-01-19T15:21:24.964166400Z")
24  public class Position extends AbstractDoubleScalarAbs<PositionUnit, Position, LengthUnit, Length>
25  {
26      /** */
27      private static final long serialVersionUID = 20150901L;
28  
29      /** Constant with value zero. */
30      public static final Position/scalar/Position.html#Position">Position ZERO = new Position(0.0, PositionUnit.DEFAULT);
31  
32      /**
33       * Construct Position scalar.
34       * @param value double; value
35       * @param unit PositionUnit; unit for the double value
36       */
37      public Position(final double value, final PositionUnit unit)
38      {
39          super(value, unit);
40      }
41  
42      /**
43       * Construct Position scalar.
44       * @param value Position; Scalar from which to construct this instance
45       */
46      public Positionouble/scalar/Position.html#Position">Position(final Position value)
47      {
48          super(value);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public final Position instantiateAbs(final double value, final PositionUnit unit)
54      {
55          return new Position(value, unit);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final Length instantiateRel(final double value, final LengthUnit unit)
61      {
62          return new Length(value, unit);
63      }
64  
65      /**
66       * Construct Position scalar.
67       * @param value double; value in SI units
68       * @return Position; the new scalar with the SI value
69       */
70      public static final Position instantiateSI(final double value)
71      {
72          return new Position(value, PositionUnit.DEFAULT);
73      }
74  
75      /**
76       * Interpolate between two values.
77       * @param zero Position; the low value
78       * @param one Position; the high value
79       * @param ratio double; the ratio between 0 and 1, inclusive
80       * @return Position; a Scalar at the ratio between
81       */
82      public static Positionscalar/Position.html#Position">Position/Position.html#Position">Position interpolate(final Positionscalar/Position.html#Position">Position zero, final Position one, final double ratio)
83      {
84          return new Position(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
85                  zero.getDisplayUnit());
86      }
87  
88      /**
89       * Return the maximum value of two absolute scalars.
90       * @param a1 Position; the first scalar
91       * @param a2 Position; the second scalar
92       * @return Position; the maximum value of two absolute scalars
93       */
94      public static Positione/scalar/Position.html#Position">Positione/scalar/Position.html#Position">Position max(final Positione/scalar/Position.html#Position">Position a1, final Position a2)
95      {
96          return a1.gt(a2) ? a1 : a2;
97      }
98  
99      /**
100      * Return the maximum value of more than two absolute scalars.
101      * @param a1 Position; the first scalar
102      * @param a2 Position; the second scalar
103      * @param an Position...; the other scalars
104      * @return the maximum value of more than two absolute scalars
105      */
106     public static Positione/scalar/Position.html#Position">Positione/scalar/Position.html#Position">Position max(final Positione/scalar/Position.html#Position">Position a1, final Position a2, final Position... an)
107     {
108         Position maxa = a1.gt(a2) ? a1 : a2;
109         for (Position a : an)
110         {
111             if (a.gt(maxa))
112             {
113                 maxa = a;
114             }
115         }
116         return maxa;
117     }
118 
119     /**
120      * Return the minimum value of two absolute scalars.
121      * @param a1 Position; the first scalar
122      * @param a2 Position; the second scalar
123      * @return the minimum value of two absolute scalars
124      */
125     public static Positione/scalar/Position.html#Position">Positione/scalar/Position.html#Position">Position min(final Positione/scalar/Position.html#Position">Position a1, final Position a2)
126     {
127         return a1.lt(a2) ? a1 : a2;
128     }
129 
130     /**
131      * Return the minimum value of more than two absolute scalars.
132      * @param a1 Position; the first scalar
133      * @param a2 Position; the second scalar
134      * @param an Position...; the other scalars
135      * @return the minimum value of more than two absolute scalars
136      */
137     public static Positione/scalar/Position.html#Position">Positione/scalar/Position.html#Position">Position min(final Positione/scalar/Position.html#Position">Position a1, final Position a2, final Position... an)
138     {
139         Position mina = a1.lt(a2) ? a1 : a2;
140         for (Position a : an)
141         {
142             if (a.lt(mina))
143             {
144                 mina = a;
145             }
146         }
147         return mina;
148     }
149 
150     /**
151      * Returns a Position representation of a textual representation of a value with a unit. The String representation that can
152      * be parsed is the double value in the unit, followed by the official abbreviation of the unit. Spaces are allowed, but not
153      * required, between the value and the unit.
154      * @param text String; the textual representation to parse into a Position
155      * @return Position; the Scalar representation of the value in its unit
156      * @throws IllegalArgumentException when the text cannot be parsed
157      * @throws NullPointerException when the text argument is null
158      */
159     public static Position valueOf(final String text)
160     {
161         Throw.whenNull(text, "Error parsing Position: text to parse is null");
162         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Position: empty text to parse");
163         Matcher matcher = ValueUtil.NUMBER_PATTERN.matcher(text);
164         if (matcher.find())
165         {
166             int index = matcher.end();
167             String unitString = text.substring(index).trim();
168             String valueString = text.substring(0, index).trim();
169             PositionUnit unit = PositionUnit.BASE.getUnitByAbbreviation(unitString);
170             if (unit != null)
171             {
172                 double d = Double.parseDouble(valueString);
173                 return new Position(d, unit);
174             }
175         }
176         throw new IllegalArgumentException("Error parsing Position from " + text);
177     }
178 
179     /**
180      * Returns a Position based on a value and the textual representation of the unit.
181      * @param value double; the value to use
182      * @param unitString String; the textual representation of the unit
183      * @return Position; the Scalar representation of the value in its unit
184      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
185      * @throws NullPointerException when the unitString argument is null
186      */
187     public static Position of(final double value, final String unitString)
188     {
189         Throw.whenNull(unitString, "Error parsing Position: unitString is null");
190         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing Position: empty unitString");
191         PositionUnit unit = PositionUnit.BASE.getUnitByAbbreviation(unitString);
192         if (unit != null)
193         {
194             return new Position(value, unit);
195         }
196         throw new IllegalArgumentException("Error parsing Position with unit " + unitString);
197     }
198 
199 }