1 package org.djunits.value.vdouble.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.vdouble.scalar.base.DoubleScalarAbs;
8 import org.djutils.base.NumberParser;
9 import org.djutils.exceptions.Throw;
10
11 import jakarta.annotation.Generated;
12
13
14
15
16
17
18
19
20
21
22
23 @Generated(value = "org.djunits.generator.GenerateDJUNIT", date = "2023-07-23T14:06:38.224104100Z")
24 public class Position extends DoubleScalarAbs<PositionUnit, Position, LengthUnit, Length>
25 {
26
27 private static final long serialVersionUID = 20150901L;
28
29
30 public static final Position ZERO = new Position(0.0, PositionUnit.DEFAULT);
31
32
33
34
35
36
37 public Position(final double value, final PositionUnit unit)
38 {
39 super(value, unit);
40 }
41
42
43
44
45
46 public Position(final Position value)
47 {
48 super(value);
49 }
50
51 @Override
52 public final Position instantiateAbs(final double value, final PositionUnit unit)
53 {
54 return new Position(value, unit);
55 }
56
57 @Override
58 public final Length instantiateRel(final double value, final LengthUnit unit)
59 {
60 return new Length(value, unit);
61 }
62
63
64
65
66
67
68 public static final Position instantiateSI(final double value)
69 {
70 return new Position(value, PositionUnit.DEFAULT);
71 }
72
73
74
75
76
77
78
79
80 public static Position interpolate(final Position zero, final Position one, final double ratio)
81 {
82 return new Position(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getDisplayUnit()) * ratio,
83 zero.getDisplayUnit());
84 }
85
86
87
88
89
90
91
92 public static Position max(final Position a1, final Position a2)
93 {
94 return a1.gt(a2) ? a1 : a2;
95 }
96
97
98
99
100
101
102
103
104 public static Position max(final Position a1, final Position a2, final Position... an)
105 {
106 Position maxa = a1.gt(a2) ? a1 : a2;
107 for (Position a : an)
108 {
109 if (a.gt(maxa))
110 {
111 maxa = a;
112 }
113 }
114 return maxa;
115 }
116
117
118
119
120
121
122
123 public static Position min(final Position a1, final Position a2)
124 {
125 return a1.lt(a2) ? a1 : a2;
126 }
127
128
129
130
131
132
133
134
135 public static Position min(final Position a1, final Position a2, final Position... an)
136 {
137 Position mina = a1.lt(a2) ? a1 : a2;
138 for (Position a : an)
139 {
140 if (a.lt(mina))
141 {
142 mina = a;
143 }
144 }
145 return mina;
146 }
147
148
149
150
151
152
153
154
155
156
157 public static Position valueOf(final String text)
158 {
159 Throw.whenNull(text, "Error parsing Position: text to parse is null");
160 Throw.when(text.length() == 0, IllegalArgumentException.class, "Error parsing Position: empty text to parse");
161 try
162 {
163 NumberParser numberParser = new NumberParser().lenient().trailing();
164 double d = numberParser.parseDouble(text);
165 String unitString = text.substring(numberParser.getTrailingPosition()).trim();
166 PositionUnit unit = PositionUnit.BASE.getUnitByAbbreviation(unitString);
167 if (unit == null)
168 throw new IllegalArgumentException("Unit " + unitString + " not found");
169 return new Position(d, unit);
170 }
171 catch (Exception exception)
172 {
173 throw new IllegalArgumentException(
174 "Error parsing Position from " + text + " using Locale " + Locale.getDefault(Locale.Category.FORMAT),
175 exception);
176 }
177 }
178
179
180
181
182
183
184
185
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 }