View Javadoc
1   package org.opentrafficsim.core.value.vdouble.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 DoubleScalar.
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 DoubleScalar
24   */
25  public abstract class DoubleScalar<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 double valueSI;
32  
33      /**
34       * Construct a new Immutable DoubleScalar.
35       * @param unit U; the unit of the new DoubleScalar
36       */
37      protected  DoubleScalar(final U unit)
38      {
39          super(unit);
40          // System.out.println("Created DoubleScalar");
41      }
42  
43      /**
44       * @param <U> Unit
45       */
46      public static class Abs<U extends Unit<U>> extends DoubleScalar<U> implements Absolute, Comparable<Abs<U>>
47      {
48          /**  */
49          private static final long serialVersionUID = 20150309L;
50  
51          /**
52           * Construct a new Absolute Immutable DoubleScalar.
53           * @param value double; the value of the new Absolute Immutable DoubleScalar
54           * @param unit U; the unit of the new Absolute Immutable DoubleScalar
55           */
56          public Abs(final double 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 DoubleScalar from an existing Absolute Immutable DoubleScalar.
65           * @param value DoubleScalar.Abs&lt;U&gt;; the reference
66           */
67          public Abs(final DoubleScalar.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 DoubleScalar from an existing Absolute MutableDoubleScalar.
76           * @param value MutableDoubleScalar.Abs&lt;U&gt;; the reference
77           */
78          public Abs(final MutableDoubleScalar.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 MutableDoubleScalar.Abs<U> mutable()
88          {
89              return new MutableDoubleScalar.Abs<U>(this);
90          }
91  
92          /** {@inheritDoc} */
93          @Override
94          public final int compareTo(final Abs<U> o)
95          {
96              return new Double(getSI()).compareTo(o.getSI());
97          }
98  
99          /** {@inheritDoc} */
100         @Override
101         public final DoubleScalar.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 DoubleScalar<U> implements Relative, Comparable<Rel<U>>
112     {
113         /**  */
114         private static final long serialVersionUID = 20150309L;
115 
116         /**
117          * Construct a new Relative Immutable DoubleScalar.
118          * @param value double; the value of the new Relative Immutable DoubleScalar
119          * @param unit U; the unit of the new Relative Immutable DoubleScalar
120          */
121         public Rel(final double 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 DoubleScalar from an existing Relative Immutable DoubleScalar.
130          * @param value DoubleScalar.Rel&lt;U&gt;; the reference
131          */
132         public Rel(final DoubleScalar.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 DoubleScalar from an existing Relative MutableDoubleScalar.
141          * @param value MutableDoubleScalar.Rel&lt;U&gt;; the reference
142          */
143         public Rel(final MutableDoubleScalar.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 MutableDoubleScalar.Rel<U> mutable()
153         {
154             return new MutableDoubleScalar.Rel<U>(this);
155         }
156 
157         /** {@inheritDoc} */
158         @Override
159         public final int compareTo(final Rel<U> o)
160         {
161             return new Double(getSI()).compareTo(o.getSI());
162         }
163 
164         /** {@inheritDoc} */
165         @Override
166         public final DoubleScalar.Rel<U> copy()
167         {
168             return this;
169         }
170 
171     }
172 
173     /**
174      * Create a mutable version of this DoubleScalar. <br>
175      * The mutable version is created as a deep copy of this. Delayed copying is not worthwhile for a Scalar.
176      * @return MutableDoubleScalar&lt;U&gt;
177      */
178     public abstract MutableDoubleScalar<U> mutable();
179 
180     /**
181      * Initialize the valueSI field (performing conversion to the SI standard unit if needed).
182      * @param value double; the value in the unit of this DoubleScalar
183      */
184     protected final void initialize(final double value)
185     {
186         if (this.getUnit().equals(this.getUnit().getStandardUnit()))
187         {
188             setValueSI(value);
189         }
190         else
191         {
192             setValueSI(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 DoubleScalar&lt;U&gt;; the value to use for initialization
199      */
200     protected final void initialize(final DoubleScalar<U> value)
201     {
202         setValueSI(value.getSI());
203     }
204 
205     /**
206      * Retrieve the value in the underlying SI unit.
207      * @return double
208      */
209     public final double getSI()
210     {
211         return this.valueSI;
212     }
213 
214     /**
215      * Set the value in the underlying SI unit.
216      * @param value double; the new value in the underlying SI unit
217      */
218     protected final void setValueSI(final double value)
219     {
220         this.valueSI = value;
221     }
222 
223     /**
224      * Retrieve the value in the original unit.
225      * @return double
226      */
227     public final double getInUnit()
228     {
229         return 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 double
236      */
237     public final double getInUnit(final U targetUnit)
238     {
239         return ValueUtil.expressAsUnit(getSI(), targetUnit);
240     }
241 
242     /**********************************************************************************/
243     /********************************* NUMBER METHODS *********************************/
244     /**********************************************************************************/
245 
246     /** {@inheritDoc} */
247     @Override
248     public final int intValue()
249     {
250         return (int) 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 (float) 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 DoubleScalar 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 DoubleScalar 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 DoubleScalar 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 MutableDoubleScalar)
315             {
316                 buf.append("Mutable   ");
317                 if (this instanceof MutableDoubleScalar.Abs)
318                 {
319                     buf.append("Abs ");
320                 }
321                 else if (this instanceof MutableDoubleScalar.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 DoubleScalar.Abs)
334                 {
335                     buf.append("Abs ");
336                 }
337                 else if (this instanceof DoubleScalar.Rel)
338                 {
339                     buf.append("Rel ");
340                 }
341                 else
342                 {
343                     buf.append("??? ");
344                 }
345             }
346         }
347         double d = ValueUtil.expressAsUnit(getSI(), displayUnit);
348         buf.append(Format.format(d));
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         long temp;
363         temp = Double.doubleToLongBits(this.valueSI);
364         result = prime * result + (int) (temp ^ (temp >>> 32));
365         return result;
366     }
367 
368     /** {@inheritDoc} */
369     @Override
370     public final boolean equals(final Object obj)
371     {
372         if (this == obj)
373         {
374             return true;
375         }
376         if (obj == null)
377         {
378         return false;
379         }
380         if (!(obj instanceof DoubleScalar))
381         {
382             return false;
383         }
384         DoubleScalar<?> other = (DoubleScalar<?>) obj;
385         // unequal if not both Absolute or both Relative
386         if (this.isAbsolute() != other.isAbsolute() || this.isRelative() != other.isRelative())
387         {
388             return false;
389         }
390         // unequal if the standard SI units differ
391         if (!this.getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
392         {
393             return false;
394         }
395         if (Double.doubleToLongBits(this.valueSI) != Double.doubleToLongBits(other.valueSI))
396         {
397             return false;
398         }
399         return true;
400     }
401 
402     /**********************************************************************************/
403     /********************************* STATIC METHODS *********************************/
404     /**********************************************************************************/
405 
406     /**
407      * Add a Relative value to an Absolute value. Return a new instance of the value. The unit of the return
408      * value will be the unit of the left argument.
409      * @param left DoubleScalar.Abs&lt;U&gt;; the left argument
410      * @param right DoubleScalar.Rel&lt;U&gt;; the right argument
411      * @param <U> Unit; the unit of the parameters and the result
412      * @return MutableDoubleScalar.Abs&lt;U&gt;; the sum of the values as an Absolute value
413      */
414     public static <U extends Unit<U>> MutableDoubleScalar.Abs<U> plus(final DoubleScalar.Abs<U> left,
415             final DoubleScalar.Rel<U> right)
416     {
417         MutableDoubleScalar.Abs<U> result = new MutableDoubleScalar.Abs<U>(left);
418         result.incrementByImpl(right);
419         return result;
420     }
421 
422     /**
423      * Add a Relative value to a Relative value. Return a new instance of the value. The unit of the return
424      * value will be the unit of the left argument.
425      * @param left DoubleScalar.Rel&lt;U&gt;; the left argument
426      * @param right DoubleScalar.Rel&lt;U&gt;; the right argument
427      * @param <U> Unit; the unit of the parameters and the result
428      * @return MutableDoubleScalar.Rel&lt;U&gt;; the sum of the values as a Relative value
429      */
430     public static <U extends Unit<U>> MutableDoubleScalar.Rel<U> plus(final DoubleScalar.Rel<U> left,
431             final DoubleScalar.Rel<U> right)
432     {
433         MutableDoubleScalar.Rel<U> result = new MutableDoubleScalar.Rel<U>(left);
434         result.incrementByImpl(right);
435         return result;
436     }
437 
438     /**
439      * Subtract a Relative value from an absolute value. Return a new instance of the value. The unit of the
440      * return value will be the unit of the left argument.
441      * @param left DoubleScalar.Abs&lt;U&gt;; the left value
442      * @param right DoubleScalar.Rel&lt;U&gt;; the right value
443      * @param <U> Unit; the unit of the parameters and the result
444      * @return MutableDoubleScalar.Abs&lt;U&gt;; the resulting value as an absolute value
445      */
446     public static <U extends Unit<U>> MutableDoubleScalar.Abs<U> minus(final DoubleScalar.Abs<U> left,
447             final DoubleScalar.Rel<U> right)
448     {
449         MutableDoubleScalar.Abs<U> result = new MutableDoubleScalar.Abs<U>(left);
450         result.decrementByImpl(right);
451         return result;
452     }
453 
454     /**
455      * Subtract a relative value from a relative value. Return a new instance of the value. The unit of the
456      * value will be the unit of the first argument.
457      * @param left DoubleScalar.Rel&lt;U&gt;; the left value
458      * @param right DoubleScalar.Rel&lt;U&gt;; the right value
459      * @param <U> Unit; the unit of the parameters and the result
460      * @return MutableDoubleScalar.Rel&lt;U&gt;; the resulting value as a relative value
461      */
462     public static <U extends Unit<U>> MutableDoubleScalar.Rel<U> minus(final DoubleScalar.Rel<U> left,
463             final DoubleScalar.Rel<U> right)
464     {
465         MutableDoubleScalar.Rel<U> result = new MutableDoubleScalar.Rel<U>(left);
466         result.decrementByImpl(right);
467         return result;
468     }
469 
470     /**
471      * Subtract two absolute values. Return a new instance of a relative value of the difference. The unit of the value
472      * will be the unit of the first argument.
473      * @param valueAbs1 DoubleScalar.Abs&lt;U&gt;; value 1
474      * @param valueAbs2 DoubleScalar.Abs&lt;U&gt;; value 2
475      * @param <U> Unit; the unit of the parameters and the result
476      * @return MutableDoubleScalar.Rel&lt;U&gt;; the difference of the two absolute values as a relative value
477      */
478     public static <U extends Unit<U>> MutableDoubleScalar.Rel<U> minus(final DoubleScalar.Abs<U> valueAbs1,
479             final DoubleScalar.Abs<U> valueAbs2)
480     {
481         MutableDoubleScalar.Rel<U> result = new MutableDoubleScalar.Rel<U>(valueAbs1.getInUnit(), valueAbs1.getUnit());
482         result.decrementBy(valueAbs2);
483         return result;
484     }
485 
486     /**
487      * Multiply two values; the result is a new instance with a different (existing or generated) SI unit.
488      * @param left DoubleScalar.Abs&lt;?&gt;; the left operand
489      * @param right DoubleScalar.Abs&lt;?&gt;; the right operand
490      * @return MutableDoubleScalar.Abs&lt;SIUnit&gt;; the product of the two values
491      */
492     public static MutableDoubleScalar.Abs<SIUnit> multiply(final DoubleScalar.Abs<?> left,
493             final DoubleScalar.Abs<?> right)
494     {
495         SIUnit targetUnit =
496                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
497                         right.getUnit().getSICoefficients()).toString());
498         return new MutableDoubleScalar.Abs<SIUnit>(left.getSI() * right.getSI(), targetUnit);
499     }
500 
501     /**
502      * Multiply two values; the result is a new instance with a different (existing or generated) SI unit.
503      * @param left DoubleScalar.Rel&lt;?&gt;; the left operand
504      * @param right DoubleScalar.Rel&lt;?&gt;; the right operand
505      * @return MutableDoubleScalar.Rel&lt;SIUnit&gt;; the product of the two values
506      */
507     public static MutableDoubleScalar.Rel<SIUnit> multiply(final DoubleScalar.Rel<?> left,
508             final DoubleScalar.Rel<?> right)
509     {
510         SIUnit targetUnit =
511                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
512                         right.getUnit().getSICoefficients()).toString());
513         return new MutableDoubleScalar.Rel<SIUnit>(left.getSI() * right.getSI(), targetUnit);
514     }
515 
516     /**
517      * Divide two values; the result is a new instance with a different (existing or generated) SI unit.
518      * @param left DoubleScalar.Abs&lt;?&gt;; the left operand
519      * @param right DoubleScalar.Abs&lt;?&gt;; the right operand
520      * @return MutableDoubleScalar.Abs&lt;SIUnit&gt;; the ratio of the two values
521      */
522     public static MutableDoubleScalar.Abs<SIUnit> divide(final DoubleScalar.Abs<?> left, final DoubleScalar.Abs<?> right)
523     {
524         SIUnit targetUnit =
525                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.divide(left.getUnit().getSICoefficients(),
526                         right.getUnit().getSICoefficients()).toString());
527         return new MutableDoubleScalar.Abs<SIUnit>(left.getSI() / right.getSI(), targetUnit);
528     }
529 
530     /**
531      * Divide two values; the result is a new instance with a different (existing or generated) SI unit.
532      * @param left DoubleScalar.Rel&lt;?&gt;; the left operand
533      * @param right DoubleScalar.Rel&lt;?&gt;; the right operand
534      * @return MutableDoubleScalar.Rel&lt;SIUnit&gt;; the ratio of the two values
535      */
536     public static MutableDoubleScalar.Rel<SIUnit> divide(final DoubleScalar.Rel<?> left, final DoubleScalar.Rel<?> right)
537     {
538         SIUnit targetUnit =
539                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.divide(left.getUnit().getSICoefficients(),
540                         right.getUnit().getSICoefficients()).toString());
541         return new MutableDoubleScalar.Rel<SIUnit>(left.getSI() / right.getSI(), targetUnit);
542     }
543 
544     /**
545      * Interpolate between or extrapolate over two values.
546      * @param zero DoubleScalar.Abs&lt;U&gt;; zero reference (returned when ratio == 0)
547      * @param one DoubleScalar.Abs&lt;U&gt;; one reference (returned when ratio == 1)
548      * @param ratio double; the ratio that determines where between (or outside) zero and one the result lies
549      * @param <U> Unit; the unit of the parameters and the result
550      * @return MutableDoubleScalar.Abs&lt;U&gt;
551      */
552     public static <U extends Unit<U>> MutableDoubleScalar.Abs<U> interpolate(final DoubleScalar.Abs<U> zero,
553             final DoubleScalar.Abs<U> one, final double ratio)
554     {
555         MutableDoubleScalar.Abs<U> result = zero.mutable();
556         result.setSI(result.getSI() * (1 - ratio) + one.getSI() * ratio);
557         return result;
558     }
559 
560     /**
561      * Interpolate between or extrapolate over two values.
562      * @param zero DoubleScalar.Rel&lt;U&gt;; zero reference (returned when ratio == 0)
563      * @param one DoubleScalar.Rel&lt;U&gt;; one reference (returned when ratio == 1)
564      * @param ratio double; the ratio that determines where between (or outside) zero and one the result lies
565      * @param <U> Unit; the unit of the parameters and the result
566      * @return MutableDoubleScalar.Rel&lt;U&gt;
567      */
568     public static <U extends Unit<U>> MutableDoubleScalar.Rel<U> interpolate(final DoubleScalar.Rel<U> zero,
569             final DoubleScalar.Rel<U> one, final double ratio)
570     {
571         MutableDoubleScalar.Rel<U> result = zero.mutable();
572         result.setSI(result.getSI() * (1 - ratio) + one.getSI() * ratio);
573         return result;
574     }
575 
576 }