View Javadoc
1   package org.opentrafficsim.core.value.vfloat.scalar;
2   
3   import org.opentrafficsim.core.unit.SICoefficients;
4   import org.opentrafficsim.core.unit.SIUnit;
5   import org.opentrafficsim.core.unit.Unit;
6   import org.opentrafficsim.core.value.Absolute;
7   import org.opentrafficsim.core.value.Format;
8   import org.opentrafficsim.core.value.Relative;
9   import org.opentrafficsim.core.value.Scalar;
10  import org.opentrafficsim.core.value.ValueUtil;
11  
12  /**
13   * Immutable FloatScalar.
14   * <p>
15   * This file was generated by the OpenTrafficSim value classes generator, 09 mrt, 2015
16   * <p>
17   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
19   * <p>
20   * @version 09 mrt, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @param <U> Unit; the unit of this FloatScalar
24   */
25  public abstract class FloatScalar<U extends Unit<U>> extends Scalar<U>
26  {
27      /**  */
28      private static final long serialVersionUID = 20150309L;
29  
30      /** The value, stored in the standard SI unit. */
31      private float valueSI;
32  
33      /**
34       * Construct a new Immutable FloatScalar.
35       * @param unit U; the unit of the new FloatScalar
36       */
37      protected  FloatScalar(final U unit)
38      {
39          super(unit);
40          // System.out.println("Created FloatScalar");
41      }
42  
43      /**
44       * @param <U> Unit
45       */
46      public static class Abs<U extends Unit<U>> extends FloatScalar<U> implements Absolute, Comparable<Abs<U>>
47      {
48          /**  */
49          private static final long serialVersionUID = 20150309L;
50  
51          /**
52           * Construct a new Absolute Immutable FloatScalar.
53           * @param value float; the value of the new Absolute Immutable FloatScalar
54           * @param unit U; the unit of the new Absolute Immutable FloatScalar
55           */
56          public Abs(final float value, final U unit)
57          {
58              super(unit);
59              // System.out.println("Created Abs");
60              initialize(value);
61          }
62  
63          /**
64           * Construct a new Absolute Immutable FloatScalar from an existing Absolute Immutable FloatScalar.
65           * @param value FloatScalar.Abs&lt;U&gt;; the reference
66           */
67          public Abs(final FloatScalar.Abs<U> value)
68          {
69              super(value.getUnit());
70              // System.out.println("Created Abs");
71              initialize(value);
72          }
73  
74          /**
75           * Construct a new Absolute Immutable FloatScalar from an existing Absolute MutableFloatScalar.
76           * @param value MutableFloatScalar.Abs&lt;U&gt;; the reference
77           */
78          public Abs(final MutableFloatScalar.Abs<U> value)
79          {
80              super(value.getUnit());
81              // System.out.println("Created Abs");
82              initialize(value);
83          }
84  
85          /** {@inheritDoc} */
86          @Override
87          public final MutableFloatScalar.Abs<U> mutable()
88          {
89              return new MutableFloatScalar.Abs<U>(this);
90          }
91  
92          /** {@inheritDoc} */
93          @Override
94          public final int compareTo(final Abs<U> o)
95          {
96              return new Float(getSI()).compareTo(o.getSI());
97          }
98  
99          /** {@inheritDoc} */
100         @Override
101         public final FloatScalar.Abs<U> copy()
102         {
103             return this;
104         }
105 
106     }
107 
108     /**
109      * @param <U> Unit
110      */
111     public static class Rel<U extends Unit<U>> extends FloatScalar<U> implements Relative, Comparable<Rel<U>>
112     {
113         /**  */
114         private static final long serialVersionUID = 20150309L;
115 
116         /**
117          * Construct a new Relative Immutable FloatScalar.
118          * @param value float; the value of the new Relative Immutable FloatScalar
119          * @param unit U; the unit of the new Relative Immutable FloatScalar
120          */
121         public Rel(final float value, final U unit)
122         {
123             super(unit);
124             // System.out.println("Created Rel");
125             initialize(value);
126         }
127 
128         /**
129          * Construct a new Relative Immutable FloatScalar from an existing Relative Immutable FloatScalar.
130          * @param value FloatScalar.Rel&lt;U&gt;; the reference
131          */
132         public Rel(final FloatScalar.Rel<U> value)
133         {
134             super(value.getUnit());
135             // System.out.println("Created Rel");
136             initialize(value);
137         }
138 
139         /**
140          * Construct a new Relative Immutable FloatScalar from an existing Relative MutableFloatScalar.
141          * @param value MutableFloatScalar.Rel&lt;U&gt;; the reference
142          */
143         public Rel(final MutableFloatScalar.Rel<U> value)
144         {
145             super(value.getUnit());
146             // System.out.println("Created Rel");
147             initialize(value);
148         }
149 
150         /** {@inheritDoc} */
151         @Override
152         public final MutableFloatScalar.Rel<U> mutable()
153         {
154             return new MutableFloatScalar.Rel<U>(this);
155         }
156 
157         /** {@inheritDoc} */
158         @Override
159         public final int compareTo(final Rel<U> o)
160         {
161             return new Float(getSI()).compareTo(o.getSI());
162         }
163 
164         /** {@inheritDoc} */
165         @Override
166         public final FloatScalar.Rel<U> copy()
167         {
168             return this;
169         }
170 
171     }
172 
173     /**
174      * Create a mutable version of this FloatScalar. <br>
175      * The mutable version is created as a deep copy of this. Delayed copying is not worthwhile for a Scalar.
176      * @return MutableFloatScalar&lt;U&gt;
177      */
178     public abstract MutableFloatScalar<U> mutable();
179 
180     /**
181      * Initialize the valueSI field (performing conversion to the SI standard unit if needed).
182      * @param value float; the value in the unit of this FloatScalar
183      */
184     protected final void initialize(final float value)
185     {
186         if (this.getUnit().equals(this.getUnit().getStandardUnit()))
187         {
188             setValueSI(value);
189         }
190         else
191         {
192             setValueSI((float) expressAsSIUnit(value));
193         }
194     }
195 
196     /**
197      * Initialize the valueSI field. As the provided value is already in the SI standard unit, conversion is never necessary.
198      * @param value FloatScalar&lt;U&gt;; the value to use for initialization
199      */
200     protected final void initialize(final FloatScalar<U> value)
201     {
202         setValueSI(value.getSI());
203     }
204 
205     /**
206      * Retrieve the value in the underlying SI unit.
207      * @return float
208      */
209     public final float getSI()
210     {
211         return this.valueSI;
212     }
213 
214     /**
215      * Set the value in the underlying SI unit.
216      * @param value float; the new value in the underlying SI unit
217      */
218     protected final void setValueSI(final float value)
219     {
220         this.valueSI = value;
221     }
222 
223     /**
224      * Retrieve the value in the original unit.
225      * @return float
226      */
227     public final float getInUnit()
228     {
229         return (float) expressAsSpecifiedUnit(getSI());
230     }
231 
232     /**
233      * Retrieve the value converted into some specified unit.
234      * @param targetUnit U; the unit to convert the value into
235      * @return float
236      */
237     public final float getInUnit(final U targetUnit)
238     {
239         return (float) ValueUtil.expressAsUnit(getSI(), targetUnit);
240     }
241 
242     /**********************************************************************************/
243     /********************************* NUMBER METHODS *********************************/
244     /**********************************************************************************/
245 
246     /** {@inheritDoc} */
247     @Override
248     public final int intValue()
249     {
250         return Math.round(getSI());
251     }
252 
253     /** {@inheritDoc} */
254     @Override
255     public final long longValue()
256     {
257         return Math.round(getSI());
258     }
259 
260     /** {@inheritDoc} */
261     @Override
262     public final float floatValue()
263     {
264         return getSI();
265     }
266 
267     /** {@inheritDoc} */
268     @Override
269     public final double doubleValue()
270     {
271         return getSI();
272     }
273 
274     /** {@inheritDoc} */
275     @Override
276     public final String toString()
277     {
278         return toString(getUnit(), false, true);
279     }
280 
281     /**
282      * Print this FloatScalar with the value expressed in the specified unit.
283      * @param displayUnit U; the unit into which the value is converted for display
284      * @return String; printable string with the scalar contents expressed in the specified unit
285      */
286     public final String toString(final U displayUnit)
287     {
288         return toString(displayUnit, false, true);
289     }
290 
291     /**
292      * Print this FloatScalar with optional type and unit information.
293      * @param verbose boolean; if true; include type info; if false; exclude type info
294      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
295      * @return String; printable string with the scalar contents
296      */
297     public final String toString(final boolean verbose, final boolean withUnit)
298     {
299         return toString(getUnit(), verbose, withUnit);
300     }
301 
302     /**
303      * Print this FloatScalar with the value expressed in the specified unit.
304      * @param displayUnit U; the unit into which the value is converted for display
305      * @param verbose boolean; if true; include type info; if false; exclude type info
306      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
307      * @return String; printable string with the scalar contents
308      */
309     public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
310     {
311         StringBuffer buf = new StringBuffer();
312         if (verbose)
313         {
314             if (this instanceof MutableFloatScalar)
315             {
316                 buf.append("Mutable   ");
317                 if (this instanceof MutableFloatScalar.Abs)
318                 {
319                     buf.append("Abs ");
320                 }
321                 else if (this instanceof MutableFloatScalar.Rel)
322                 {
323                     buf.append("Rel ");
324                 }
325                 else
326                 {
327                     buf.append("??? ");
328                 }
329             }
330             else
331             {
332                 buf.append("Immutable ");
333                 if (this instanceof FloatScalar.Abs)
334                 {
335                     buf.append("Abs ");
336                 }
337                 else if (this instanceof FloatScalar.Rel)
338                 {
339                     buf.append("Rel ");
340                 }
341                 else
342                 {
343                     buf.append("??? ");
344                 }
345             }
346         }
347         float f = (float) ValueUtil.expressAsUnit(getSI(), displayUnit);
348         buf.append(Format.format(f));
349         if (withUnit)
350         {
351             buf.append(displayUnit.getAbbreviation());
352         }
353         return buf.toString();
354     }
355 
356     /** {@inheritDoc} */
357     @Override
358     public final int hashCode()
359     {
360         final int prime = 31;
361         int result = 1;
362         result = prime * result + Float.floatToIntBits(this.valueSI);
363         return result;
364     }
365 
366     /** {@inheritDoc} */
367     @Override
368     public final boolean equals(final Object obj)
369     {
370         if (this == obj)
371         {
372             return true;
373         }
374         if (obj == null)
375         {
376         return false;
377         }
378         if (!(obj instanceof FloatScalar))
379         {
380             return false;
381         }
382         FloatScalar<?> other = (FloatScalar<?>) obj;
383         // unequal if not both Absolute or both Relative
384         if (this.isAbsolute() != other.isAbsolute() || this.isRelative() != other.isRelative())
385         {
386             return false;
387         }
388         // unequal if the standard SI units differ
389         if (!this.getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
390         {
391             return false;
392         }
393         if (Float.floatToIntBits(this.valueSI) != Float.floatToIntBits(other.valueSI))
394         {
395             return false;
396         }
397         return true;
398     }
399 
400     /**********************************************************************************/
401     /********************************* STATIC METHODS *********************************/
402     /**********************************************************************************/
403 
404     /**
405      * Add a Relative value to an Absolute value. Return a new instance of the value. The unit of the return
406      * value will be the unit of the left argument.
407      * @param left FloatScalar.Abs&lt;U&gt;; the left argument
408      * @param right FloatScalar.Rel&lt;U&gt;; the right argument
409      * @param <U> Unit; the unit of the parameters and the result
410      * @return MutableFloatScalar.Abs&lt;U&gt;; the sum of the values as an Absolute value
411      */
412     public static <U extends Unit<U>> MutableFloatScalar.Abs<U> plus(final FloatScalar.Abs<U> left,
413             final FloatScalar.Rel<U> right)
414     {
415         MutableFloatScalar.Abs<U> result = new MutableFloatScalar.Abs<U>(left);
416         result.incrementByImpl(right);
417         return result;
418     }
419 
420     /**
421      * Add a Relative value to a Relative value. Return a new instance of the value. The unit of the return
422      * value will be the unit of the left argument.
423      * @param left FloatScalar.Rel&lt;U&gt;; the left argument
424      * @param right FloatScalar.Rel&lt;U&gt;; the right argument
425      * @param <U> Unit; the unit of the parameters and the result
426      * @return MutableFloatScalar.Rel&lt;U&gt;; the sum of the values as a Relative value
427      */
428     public static <U extends Unit<U>> MutableFloatScalar.Rel<U> plus(final FloatScalar.Rel<U> left,
429             final FloatScalar.Rel<U> right)
430     {
431         MutableFloatScalar.Rel<U> result = new MutableFloatScalar.Rel<U>(left);
432         result.incrementByImpl(right);
433         return result;
434     }
435 
436     /**
437      * Subtract a Relative value from an absolute value. Return a new instance of the value. The unit of the
438      * return value will be the unit of the left argument.
439      * @param left FloatScalar.Abs&lt;U&gt;; the left value
440      * @param right FloatScalar.Rel&lt;U&gt;; the right value
441      * @param <U> Unit; the unit of the parameters and the result
442      * @return MutableFloatScalar.Abs&lt;U&gt;; the resulting value as an absolute value
443      */
444     public static <U extends Unit<U>> MutableFloatScalar.Abs<U> minus(final FloatScalar.Abs<U> left,
445             final FloatScalar.Rel<U> right)
446     {
447         MutableFloatScalar.Abs<U> result = new MutableFloatScalar.Abs<U>(left);
448         result.decrementByImpl(right);
449         return result;
450     }
451 
452     /**
453      * Subtract a relative value from a relative value. Return a new instance of the value. The unit of the
454      * value will be the unit of the first argument.
455      * @param left FloatScalar.Rel&lt;U&gt;; the left value
456      * @param right FloatScalar.Rel&lt;U&gt;; the right value
457      * @param <U> Unit; the unit of the parameters and the result
458      * @return MutableFloatScalar.Rel&lt;U&gt;; the resulting value as a relative value
459      */
460     public static <U extends Unit<U>> MutableFloatScalar.Rel<U> minus(final FloatScalar.Rel<U> left,
461             final FloatScalar.Rel<U> right)
462     {
463         MutableFloatScalar.Rel<U> result = new MutableFloatScalar.Rel<U>(left);
464         result.decrementByImpl(right);
465         return result;
466     }
467 
468     /**
469      * Subtract two absolute values. Return a new instance of a relative value of the difference. The unit of the value
470      * will be the unit of the first argument.
471      * @param valueAbs1 FloatScalar.Abs&lt;U&gt;; value 1
472      * @param valueAbs2 FloatScalar.Abs&lt;U&gt;; value 2
473      * @param <U> Unit; the unit of the parameters and the result
474      * @return MutableFloatScalar.Rel&lt;U&gt;; the difference of the two absolute values as a relative value
475      */
476     public static <U extends Unit<U>> MutableFloatScalar.Rel<U> minus(final FloatScalar.Abs<U> valueAbs1,
477             final FloatScalar.Abs<U> valueAbs2)
478     {
479         MutableFloatScalar.Rel<U> result = new MutableFloatScalar.Rel<U>(valueAbs1.getInUnit(), valueAbs1.getUnit());
480         result.decrementBy(valueAbs2);
481         return result;
482     }
483 
484     /**
485      * Multiply two values; the result is a new instance with a different (existing or generated) SI unit.
486      * @param left FloatScalar.Abs&lt;?&gt;; the left operand
487      * @param right FloatScalar.Abs&lt;?&gt;; the right operand
488      * @return MutableFloatScalar.Abs&lt;SIUnit&gt;; the product of the two values
489      */
490     public static MutableFloatScalar.Abs<SIUnit> multiply(final FloatScalar.Abs<?> left, final FloatScalar.Abs<?> right)
491     {
492         SIUnit targetUnit =
493                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
494                         right.getUnit().getSICoefficients()).toString());
495         return new MutableFloatScalar.Abs<SIUnit>(left.getSI() * right.getSI(), targetUnit);
496     }
497 
498     /**
499      * Multiply two values; the result is a new instance with a different (existing or generated) SI unit.
500      * @param left FloatScalar.Rel&lt;?&gt;; the left operand
501      * @param right FloatScalar.Rel&lt;?&gt;; the right operand
502      * @return MutableFloatScalar.Rel&lt;SIUnit&gt;; the product of the two values
503      */
504     public static MutableFloatScalar.Rel<SIUnit> multiply(final FloatScalar.Rel<?> left, final FloatScalar.Rel<?> right)
505     {
506         SIUnit targetUnit =
507                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
508                         right.getUnit().getSICoefficients()).toString());
509         return new MutableFloatScalar.Rel<SIUnit>(left.getSI() * right.getSI(), targetUnit);
510     }
511 
512     /**
513      * Divide two values; the result is a new instance with a different (existing or generated) SI unit.
514      * @param left FloatScalar.Abs&lt;?&gt;; the left operand
515      * @param right FloatScalar.Abs&lt;?&gt;; the right operand
516      * @return MutableFloatScalar.Abs&lt;SIUnit&gt;; the ratio of the two values
517      */
518     public static MutableFloatScalar.Abs<SIUnit> divide(final FloatScalar.Abs<?> left, final FloatScalar.Abs<?> right)
519     {
520         SIUnit targetUnit =
521                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.divide(left.getUnit().getSICoefficients(),
522                         right.getUnit().getSICoefficients()).toString());
523         return new MutableFloatScalar.Abs<SIUnit>(left.getSI() / right.getSI(), targetUnit);
524     }
525 
526     /**
527      * Divide two values; the result is a new instance with a different (existing or generated) SI unit.
528      * @param left FloatScalar.Rel&lt;?&gt;; the left operand
529      * @param right FloatScalar.Rel&lt;?&gt;; the right operand
530      * @return MutableFloatScalar.Rel&lt;SIUnit&gt;; the ratio of the two values
531      */
532     public static MutableFloatScalar.Rel<SIUnit> divide(final FloatScalar.Rel<?> left, final FloatScalar.Rel<?> right)
533     {
534         SIUnit targetUnit =
535                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.divide(left.getUnit().getSICoefficients(),
536                         right.getUnit().getSICoefficients()).toString());
537         return new MutableFloatScalar.Rel<SIUnit>(left.getSI() / right.getSI(), targetUnit);
538     }
539 
540     /**
541      * Interpolate between or extrapolate over two values.
542      * @param zero FloatScalar.Abs&lt;U&gt;; zero reference (returned when ratio == 0)
543      * @param one FloatScalar.Abs&lt;U&gt;; one reference (returned when ratio == 1)
544      * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
545      * @param <U> Unit; the unit of the parameters and the result
546      * @return MutableFloatScalar.Abs&lt;U&gt;
547      */
548     public static <U extends Unit<U>> MutableFloatScalar.Abs<U> interpolate(final FloatScalar.Abs<U> zero,
549             final FloatScalar.Abs<U> one, final float ratio)
550     {
551         MutableFloatScalar.Abs<U> result = zero.mutable();
552         result.setSI(result.getSI() * (1 - ratio) + one.getSI() * ratio);
553         return result;
554     }
555 
556     /**
557      * Interpolate between or extrapolate over two values.
558      * @param zero FloatScalar.Rel&lt;U&gt;; zero reference (returned when ratio == 0)
559      * @param one FloatScalar.Rel&lt;U&gt;; one reference (returned when ratio == 1)
560      * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
561      * @param <U> Unit; the unit of the parameters and the result
562      * @return MutableFloatScalar.Rel&lt;U&gt;
563      */
564     public static <U extends Unit<U>> MutableFloatScalar.Rel<U> interpolate(final FloatScalar.Rel<U> zero,
565             final FloatScalar.Rel<U> one, final float ratio)
566     {
567         MutableFloatScalar.Rel<U> result = zero.mutable();
568         result.setSI(result.getSI() * (1 - ratio) + one.getSI() * ratio);
569         return result;
570     }
571 
572 }