View Javadoc
1   package org.djutils.cli;
2   
3   import org.djunits.value.vdouble.scalar.AbsoluteTemperature;
4   import org.djunits.value.vdouble.scalar.Acceleration;
5   import org.djunits.value.vdouble.scalar.Angle;
6   import org.djunits.value.vdouble.scalar.AngleSolid;
7   import org.djunits.value.vdouble.scalar.Area;
8   import org.djunits.value.vdouble.scalar.Density;
9   import org.djunits.value.vdouble.scalar.Dimensionless;
10  import org.djunits.value.vdouble.scalar.Direction;
11  import org.djunits.value.vdouble.scalar.Duration;
12  import org.djunits.value.vdouble.scalar.ElectricalCharge;
13  import org.djunits.value.vdouble.scalar.ElectricalCurrent;
14  import org.djunits.value.vdouble.scalar.ElectricalPotential;
15  import org.djunits.value.vdouble.scalar.ElectricalResistance;
16  import org.djunits.value.vdouble.scalar.Energy;
17  import org.djunits.value.vdouble.scalar.FlowMass;
18  import org.djunits.value.vdouble.scalar.FlowVolume;
19  import org.djunits.value.vdouble.scalar.Force;
20  import org.djunits.value.vdouble.scalar.Frequency;
21  import org.djunits.value.vdouble.scalar.Length;
22  import org.djunits.value.vdouble.scalar.LinearDensity;
23  import org.djunits.value.vdouble.scalar.Mass;
24  import org.djunits.value.vdouble.scalar.Position;
25  import org.djunits.value.vdouble.scalar.Power;
26  import org.djunits.value.vdouble.scalar.Pressure;
27  import org.djunits.value.vdouble.scalar.Speed;
28  import org.djunits.value.vdouble.scalar.Temperature;
29  import org.djunits.value.vdouble.scalar.Time;
30  import org.djunits.value.vdouble.scalar.Torque;
31  import org.djunits.value.vdouble.scalar.Volume;
32  
33  import picocli.CommandLine;
34  import picocli.CommandLine.ITypeConverter;
35  
36  /**
37   * CliUnitConverters offers conversion methods for DJUNITS scalars so these can be used on the command line, e.g.:
38   * 
39   * <pre>
40   * java -jar ProgramApp.jar --timeout=5min
41   * </pre>
42   * 
43   * <br>
44   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
45   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
46   * source code and binary code of this software is proprietary information of Delft University of Technology.
47   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
48   */
49  public class CliUnitConverters
50  {
51      /**
52       * Register all DJUNITS converters for a CommandLine.
53       * @param cmd String; the CommandLine for which the DJUNITS converters should be registered
54       */
55      public static void registerAll(final CommandLine cmd)
56      {
57          cmd.registerConverter(AbsoluteTemperature.class, new ABSOLUTETEMPERATURE());
58          cmd.registerConverter(Acceleration.class, new ACCELERATION());
59          cmd.registerConverter(Angle.class, new ANGLE());
60          cmd.registerConverter(AngleSolid.class, new ANGLESOLID());
61          cmd.registerConverter(Area.class, new AREA());
62          cmd.registerConverter(Density.class, new DENSITY());
63          cmd.registerConverter(Dimensionless.class, new DIMENSIONLESS());
64          cmd.registerConverter(Direction.class, new DIRECTION());
65          cmd.registerConverter(Duration.class, new DURATION());
66          cmd.registerConverter(ElectricalCharge.class, new ELECTRICALCHARGE());
67          cmd.registerConverter(ElectricalCurrent.class, new ELECTRICALCURRENT());
68          cmd.registerConverter(ElectricalPotential.class, new ELECTRICALPOTENTIAL());
69          cmd.registerConverter(ElectricalResistance.class, new ELECTRICALRESISTANCE());
70          cmd.registerConverter(Energy.class, new ENERGY());
71          cmd.registerConverter(FlowMass.class, new FLOWMASS());
72          cmd.registerConverter(FlowVolume.class, new FLOWVOLUME());
73          cmd.registerConverter(Force.class, new FORCE());
74          cmd.registerConverter(Frequency.class, new FREQUENCY());
75          cmd.registerConverter(Length.class, new LENGTH());
76          cmd.registerConverter(LinearDensity.class, new LINEARDENSITY());
77          cmd.registerConverter(Mass.class, new MASS());
78          cmd.registerConverter(Position.class, new POSITION());
79          cmd.registerConverter(Power.class, new POWER());
80          cmd.registerConverter(Pressure.class, new PRESSURE());
81          cmd.registerConverter(Speed.class, new SPEED());
82          cmd.registerConverter(Temperature.class, new TEMPERATURE());
83          cmd.registerConverter(Time.class, new TIME());
84          cmd.registerConverter(Torque.class, new TORQUE());
85          cmd.registerConverter(Volume.class, new VOLUME());
86      }
87  
88      /**
89       * Convert an absolute temperature String with unit on the command line to an AbsoluteTemperature scalar.
90       */
91      public static class ABSOLUTETEMPERATURE implements ITypeConverter<AbsoluteTemperature>
92      {
93          /** {@inheritDoc} */
94          @Override
95          public AbsoluteTemperature convert(final String value) throws Exception
96          {
97              return AbsoluteTemperature.valueOf(value);
98          }
99      }
100 
101     /**
102      * Convert an acceleration String with unit on the command line to an Acceleration scalar.
103      */
104     public static class ACCELERATION implements ITypeConverter<Acceleration>
105     {
106         /** {@inheritDoc} */
107         @Override
108         public Acceleration convert(final String value) throws Exception
109         {
110             return Acceleration.valueOf(value);
111         }
112     }
113 
114     /**
115      * Convert an angle String with unit on the command line to an Angle scalar.
116      */
117     public static class ANGLE implements ITypeConverter<Angle>
118     {
119         /** {@inheritDoc} */
120         @Override
121         public Angle convert(final String value) throws Exception
122         {
123             return Angle.valueOf(value);
124         }
125     }
126 
127     /**
128      * Convert a solid angle String with unit on the command line to an AngleSolid scalar.
129      */
130     public static class ANGLESOLID implements ITypeConverter<AngleSolid>
131     {
132         /** {@inheritDoc} */
133         @Override
134         public AngleSolid convert(final String value) throws Exception
135         {
136             return AngleSolid.valueOf(value);
137         }
138     }
139 
140     /**
141      * Convert an area String with unit on the command line to an Area scalar.
142      */
143     public static class AREA implements ITypeConverter<Area>
144     {
145         /** {@inheritDoc} */
146         @Override
147         public Area convert(final String value) throws Exception
148         {
149             return Area.valueOf(value);
150         }
151     }
152 
153     /**
154      * Convert a density String with unit on the command line to a Density scalar.
155      */
156     public static class DENSITY implements ITypeConverter<Density>
157     {
158         /** {@inheritDoc} */
159         @Override
160         public Density convert(final String value) throws Exception
161         {
162             return Density.valueOf(value);
163         }
164     }
165 
166     /**
167      * Convert a dimensionless String with unit on the command line to a Dimensionless scalar.
168      */
169     public static class DIMENSIONLESS implements ITypeConverter<Dimensionless>
170     {
171         /** {@inheritDoc} */
172         @Override
173         public Dimensionless convert(final String value) throws Exception
174         {
175             try
176             {
177                 return Dimensionless.valueOf(value);
178             }
179             catch (Exception e)
180             {
181                 // try to return the number as a Dimensionless value
182                 return Dimensionless.createSI(Double.parseDouble(value));
183             }
184         }
185     }
186 
187     /**
188      * Convert a direction String with unit on the command line to a Direction scalar.
189      */
190     public static class DIRECTION implements ITypeConverter<Direction>
191     {
192         /** {@inheritDoc} */
193         @Override
194         public Direction convert(final String value) throws Exception
195         {
196             return Direction.valueOf(value);
197         }
198     }
199 
200     /**
201      * Convert a duration String with unit on the command line to a Duration scalar.
202      */
203     public static class DURATION implements ITypeConverter<Duration>
204     {
205         /** {@inheritDoc} */
206         @Override
207         public Duration convert(final String value) throws Exception
208         {
209             return Duration.valueOf(value);
210         }
211     }
212 
213     /**
214      * Convert an electrical charge String with unit on the command line to an ElectricalCharge scalar.
215      */
216     public static class ELECTRICALCHARGE implements ITypeConverter<ElectricalCharge>
217     {
218         /** {@inheritDoc} */
219         @Override
220         public ElectricalCharge convert(final String value) throws Exception
221         {
222             return ElectricalCharge.valueOf(value);
223         }
224     }
225 
226     /**
227      * Convert an electrical current String with unit on the command line to an ElectricalCurrent scalar.
228      */
229     public static class ELECTRICALCURRENT implements ITypeConverter<ElectricalCurrent>
230     {
231         /** {@inheritDoc} */
232         @Override
233         public ElectricalCurrent convert(final String value) throws Exception
234         {
235             return ElectricalCurrent.valueOf(value);
236         }
237     }
238 
239     /**
240      * Convert an electrical potential String with unit on the command line to an ElectricalPotential scalar.
241      */
242     public static class ELECTRICALPOTENTIAL implements ITypeConverter<ElectricalPotential>
243     {
244         /** {@inheritDoc} */
245         @Override
246         public ElectricalPotential convert(final String value) throws Exception
247         {
248             return ElectricalPotential.valueOf(value);
249         }
250     }
251 
252     /**
253      * Convert an electrical resistance String with unit on the command line to an ElectricalResistance scalar.
254      */
255     public static class ELECTRICALRESISTANCE implements ITypeConverter<ElectricalResistance>
256     {
257         /** {@inheritDoc} */
258         @Override
259         public ElectricalResistance convert(final String value) throws Exception
260         {
261             return ElectricalResistance.valueOf(value);
262         }
263     }
264 
265     /**
266      * Convert an energy String with unit on the command line to an Energy scalar.
267      */
268     public static class ENERGY implements ITypeConverter<Energy>
269     {
270         /** {@inheritDoc} */
271         @Override
272         public Energy convert(final String value) throws Exception
273         {
274             return Energy.valueOf(value);
275         }
276     }
277 
278     /**
279      * Convert a flow mass String with unit on the command line to a FlowMass scalar.
280      */
281     public static class FLOWMASS implements ITypeConverter<FlowMass>
282     {
283         /** {@inheritDoc} */
284         @Override
285         public FlowMass convert(final String value) throws Exception
286         {
287             return FlowMass.valueOf(value);
288         }
289     }
290 
291     /**
292      * Convert a flow volume String with unit on the command line to a FlowVolume scalar.
293      */
294     public static class FLOWVOLUME implements ITypeConverter<FlowVolume>
295     {
296         /** {@inheritDoc} */
297         @Override
298         public FlowVolume convert(final String value) throws Exception
299         {
300             return FlowVolume.valueOf(value);
301         }
302     }
303 
304     /**
305      * Convert a force String with unit on the command line to a Force scalar.
306      */
307     public static class FORCE implements ITypeConverter<Force>
308     {
309         /** {@inheritDoc} */
310         @Override
311         public Force convert(final String value) throws Exception
312         {
313             return Force.valueOf(value);
314         }
315     }
316 
317     /**
318      * Convert a frequency String with unit on the command line to a Frequency scalar.
319      */
320     public static class FREQUENCY implements ITypeConverter<Frequency>
321     {
322         /** {@inheritDoc} */
323         @Override
324         public Frequency convert(final String value) throws Exception
325         {
326             return Frequency.valueOf(value);
327         }
328     }
329 
330     /**
331      * Convert a length String with unit on the command line to a Length scalar.
332      */
333     public static class LENGTH implements ITypeConverter<Length>
334     {
335         /** {@inheritDoc} */
336         @Override
337         public Length convert(final String value) throws Exception
338         {
339             return Length.valueOf(value);
340         }
341     }
342 
343     /**
344      * Convert a linear density String with unit on the command line to a LinearDensity scalar.
345      */
346     public static class LINEARDENSITY implements ITypeConverter<LinearDensity>
347     {
348         /** {@inheritDoc} */
349         @Override
350         public LinearDensity convert(final String value) throws Exception
351         {
352             return LinearDensity.valueOf(value);
353         }
354     }
355 
356     /**
357      * Convert a mass String with unit on the command line to a Mass scalar.
358      */
359     public static class MASS implements ITypeConverter<Mass>
360     {
361         /** {@inheritDoc} */
362         @Override
363         public Mass convert(final String value) throws Exception
364         {
365             return Mass.valueOf(value);
366         }
367     }
368 
369     /**
370      * Convert a position String with unit on the command line to a Position scalar.
371      */
372     public static class POSITION implements ITypeConverter<Position>
373     {
374         /** {@inheritDoc} */
375         @Override
376         public Position convert(final String value) throws Exception
377         {
378             return Position.valueOf(value);
379         }
380     }
381 
382     /**
383      * Convert a power String with unit on the command line to a Power scalar.
384      */
385     public static class POWER implements ITypeConverter<Power>
386     {
387         /** {@inheritDoc} */
388         @Override
389         public Power convert(final String value) throws Exception
390         {
391             return Power.valueOf(value);
392         }
393     }
394 
395     /**
396      * Convert a pressure String with unit on the command line to a Pressure scalar.
397      */
398     public static class PRESSURE implements ITypeConverter<Pressure>
399     {
400         /** {@inheritDoc} */
401         @Override
402         public Pressure convert(final String value) throws Exception
403         {
404             return Pressure.valueOf(value);
405         }
406     }
407 
408     /**
409      * Convert a speed String with unit on the command line to a Speed scalar.
410      */
411     public static class SPEED implements ITypeConverter<Speed>
412     {
413         /** {@inheritDoc} */
414         @Override
415         public Speed convert(final String value) throws Exception
416         {
417             return Speed.valueOf(value);
418         }
419     }
420 
421     /**
422      * Convert a temperature String with unit on the command line to a Temperature scalar.
423      */
424     public static class TEMPERATURE implements ITypeConverter<Temperature>
425     {
426         /** {@inheritDoc} */
427         @Override
428         public Temperature convert(final String value) throws Exception
429         {
430             return Temperature.valueOf(value);
431         }
432     }
433 
434     /**
435      * Convert a time String with unit on the command line to a Time scalar.
436      */
437     public static class TIME implements ITypeConverter<Time>
438     {
439         /** {@inheritDoc} */
440         @Override
441         public Time convert(final String value) throws Exception
442         {
443             return Time.valueOf(value);
444         }
445     }
446 
447     /**
448      * Convert a torque String with unit on the command line to a Torque scalar.
449      */
450     public static class TORQUE implements ITypeConverter<Torque>
451     {
452         /** {@inheritDoc} */
453         @Override
454         public Torque convert(final String value) throws Exception
455         {
456             return Torque.valueOf(value);
457         }
458     }
459 
460     /**
461      * Convert a volume String with unit on the command line to a Volume scalar.
462      */
463     public static class VOLUME implements ITypeConverter<Volume>
464     {
465         /** {@inheritDoc} */
466         @Override
467         public Volume convert(final String value) throws Exception
468         {
469             return Volume.valueOf(value);
470         }
471     }
472 
473 }