View Javadoc
1   package org.djunits.value.vfloat.scalar;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.LengthUnit;
6   import org.djunits.unit.PositionUnit;
7   import org.djunits.value.vfloat.scalar.base.FloatScalarAbs;
8   import org.djutils.base.NumberParser;
9   import org.djutils.exceptions.Throw;
10  
11  import jakarta.annotation.Generated;
12  
13  /**
14   * Easy access methods for the FloatPosition FloatScalar.
15   * <p>
16   * Copyright (c) 2013-2024 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://djunits.org/docs/license.html">DJUNITS 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 = "2023-07-23T14:06:38.224104100Z")
24  public class FloatPosition extends FloatScalarAbs<PositionUnit, FloatPosition, LengthUnit, FloatLength>
25  {
26      /** */
27      private static final long serialVersionUID = 20150901L;
28  
29      /** Constant with value zero. */
30      public static final FloatPosition ZERO = new FloatPosition(0.0f, PositionUnit.DEFAULT);
31  
32      /**
33       * Construct FloatPosition scalar.
34       * @param value float; the float value
35       * @param unit PositionUnit; unit for the float value
36       */
37      public FloatPosition(final float value, final PositionUnit unit)
38      {
39          super(value, unit);
40      }
41  
42      /**
43       * Construct FloatPosition scalar using a double value.
44       * @param value double; the double value
45       * @param unit PositionUnit; unit for the resulting float value
46       */
47      public FloatPosition(final double value, final PositionUnit unit)
48      {
49          super((float) value, unit);
50      }
51  
52      /**
53       * Construct FloatPosition scalar.
54       * @param value FloatPosition; Scalar from which to construct this instance
55       */
56      public FloatPosition(final FloatPosition value)
57      {
58          super(value);
59      }
60  
61      @Override
62      public final FloatPosition instantiateAbs(final float value, final PositionUnit unit)
63      {
64          return new FloatPosition(value, unit);
65      }
66  
67      @Override
68      public final FloatLength instantiateRel(final float value, final LengthUnit unit)
69      {
70          return new FloatLength(value, unit);
71      }
72  
73      /**
74       * Construct FloatPosition scalar.
75       * @param value float; the float value in BASE units
76       * @return FloatPosition; the new scalar with the BASE value
77       */
78      public static final FloatPosition instantiateSI(final float value)
79      {
80          return new FloatPosition(value, PositionUnit.DEFAULT);
81      }
82  
83      /**
84       * Interpolate between two values.
85       * @param zero FloatPosition; the low value
86       * @param one FloatPosition; the high value
87       * @param ratio float; the ratio between 0 and 1, inclusive
88       * @return FloatPosition; a Scalar at the ratio between
89       */
90      public static FloatPosition interpolate(final FloatPosition zero, final FloatPosition one, final float ratio)
91      {
92          return new FloatPosition(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
93                  zero.getDisplayUnit());
94      }
95  
96      /**
97       * Return the maximum value of two absolute scalars.
98       * @param a1 FloatPosition; the first scalar
99       * @param a2 FloatPosition; the second scalar
100      * @return FloatPosition; the maximum value of two absolute scalars
101      */
102     public static FloatPosition max(final FloatPosition a1, final FloatPosition a2)
103     {
104         return a1.gt(a2) ? a1 : a2;
105     }
106 
107     /**
108      * Return the maximum value of more than two absolute scalars.
109      * @param a1 FloatPosition; the first scalar
110      * @param a2 FloatPosition; the second scalar
111      * @param an FloatPosition...; the other scalars
112      * @return FloatPosition; the maximum value of more than two absolute scalars
113      */
114     public static FloatPosition max(final FloatPosition a1, final FloatPosition a2, final FloatPosition... an)
115     {
116         FloatPosition maxa = a1.gt(a2) ? a1 : a2;
117         for (FloatPosition a : an)
118         {
119             if (a.gt(maxa))
120             {
121                 maxa = a;
122             }
123         }
124         return maxa;
125     }
126 
127     /**
128      * Return the minimum value of two absolute scalars.
129      * @param a1 FloatPosition; the first scalar
130      * @param a2 FloatPosition; the second scalar
131      * @return FloatPosition; the minimum value of two absolute scalars
132      */
133     public static FloatPosition min(final FloatPosition a1, final FloatPosition a2)
134     {
135         return a1.lt(a2) ? a1 : a2;
136     }
137 
138     /**
139      * Return the minimum value of more than two absolute scalars.
140      * @param a1 FloatPosition; the first scalar
141      * @param a2 FloatPosition; the second scalar
142      * @param an FloatPosition...; the other scalars
143      * @return FloatPosition; the minimum value of more than two absolute scalars
144      */
145     public static FloatPosition min(final FloatPosition a1, final FloatPosition a2, final FloatPosition... an)
146     {
147         FloatPosition mina = a1.lt(a2) ? a1 : a2;
148         for (FloatPosition a : an)
149         {
150             if (a.lt(mina))
151             {
152                 mina = a;
153             }
154         }
155         return mina;
156     }
157 
158     /**
159      * Returns a FloatPosition representation of a textual representation of a value with a unit. The String representation that
160      * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
161      * allowed, but not required, between the value and the unit.
162      * @param text String; the textual representation to parse into a FloatPosition
163      * @return FloatPosition; the Scalar representation of the value in its unit
164      * @throws IllegalArgumentException when the text cannot be parsed
165      * @throws NullPointerException when the text argument is null
166      */
167     public static FloatPosition valueOf(final String text)
168     {
169         Throw.whenNull(text, "Error parsing FloatPosition: text to parse is null");
170         Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing FloatPosition: empty text to parse");
171         try
172         {
173             NumberParser numberParser = new NumberParser().lenient().trailing();
174             float f = numberParser.parseFloat(text);
175             String unitString = text.substring(numberParser.getTrailingPosition()).trim();
176             PositionUnit unit = PositionUnit.BASE.getUnitByAbbreviation(unitString);
177             if (unit == null)
178                 throw new IllegalArgumentException("Unit " + unitString + " not found");
179             return new FloatPosition(f, unit);
180         }
181         catch (Exception exception)
182         {
183             throw new IllegalArgumentException(
184                     "Error parsing FloatPosition from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
185                     exception);
186         }
187     }
188 
189     /**
190      * Returns a FloatPosition based on a value and the textual representation of the unit, which can be localized.
191      * @param value double; the value to use
192      * @param unitString String; the textual representation of the unit
193      * @return FloatPosition; the Scalar representation of the value in its unit
194      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
195      * @throws NullPointerException when the unitString argument is null
196      */
197     public static FloatPosition of(final float value, final String unitString)
198     {
199         Throw.whenNull(unitString, "Error parsing FloatPosition: unitString is null");
200         Throw.when(unitString.length() == 0, IllegalArgumentException.class, "Error parsing FloatPosition: empty unitString");
201         PositionUnit unit = PositionUnit.BASE.getUnitByAbbreviation(unitString);
202         if (unit != null)
203         {
204             return new FloatPosition(value, unit);
205         }
206         throw new IllegalArgumentException("Error parsing FloatPosition with unit " + unitString);
207     }
208 
209 }