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