View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.value.MathFunctionsRel;
5   import org.djunits.value.Relative;
6   import org.djunits.value.ValueUtil;
7   import org.djunits.value.vdouble.DoubleMathFunctions;
8   
9   /**
10   * The typed, abstract DoubleScalarRel class that forms the basis of all DoubleScalar definitions and extensions.<br>
11   * Note: A relative scalar class can implement the toAbs() method if it has an absolute equivalent.
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
15   * </p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version Oct 13, 2016 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   * @param <U> the unit
22   * @param <R> the Relative class for reference purposes
23   */
24  public abstract class AbstractDoubleScalarRel<U extends Unit<U>, R extends AbstractDoubleScalarRel<U, R>>
25          extends AbstractDoubleScalar<U, R> implements Relative, MathFunctionsRel<R>, DoubleMathFunctions<R>
26  {
27      /**  */
28      private static final long serialVersionUID = 20150626L;
29  
30      /**
31       * Construct a new Relative Immutable DoubleScalar.
32       * @param value double; the value of the new Relative Immutable DoubleScalar
33       * @param unit U; the unit of the new Relative Immutable DoubleScalar
34       */
35      public AbstractDoubleScalarRel(final double value, final U unit)
36      {
37          super(unit, unit.isBaseSIUnit() ? value : ValueUtil.expressAsSIUnit(value, unit));
38      }
39  
40      /**
41       * Construct a new Relative Immutable DoubleScalar from an existing Relative Immutable DoubleScalar.
42       * @param value R, a relative typed DoubleScalar; the reference
43       */
44      public AbstractDoubleScalarRel(final R value)
45      {
46          super(value.getUnit(), value.si);
47      }
48  
49      /**
50       * Construct a new Relative Immutable DoubleScalar of the right type. Each extending class must implement this method.
51       * @param value double; the double value
52       * @param unit U; the unit
53       * @return R a new relative instance of the DoubleScalar of the right type
54       */
55      public abstract R instantiateRel(double value, U unit);
56  
57      /**
58       * Increment the value by the supplied value and return the result. If the units are equal, the result is expressed in that
59       * unit. If the units are unequal, the result is expressed in the standard (often SI) unit.
60       * @param increment R, a relative typed DoubleScalar; amount by which the value is incremented
61       * @return Absolute DoubleScalar
62       */
63      public final R plus(final R increment)
64      {
65          if (getUnit().isBaseSIUnit())
66          {
67              return instantiateRel(this.si + increment.si, getUnit().getStandardUnit());
68          }
69          return getUnit().equals(increment.getUnit()) ? instantiateRel(getInUnit() + increment.getInUnit(), getUnit())
70                  : instantiateRel(this.si + increment.si, getUnit().getStandardUnit());
71      }
72  
73      /**
74       * Decrement the value by the supplied value and return the result. If the units are equal, the result is expressed in that
75       * unit. If the units are unequal, the result is expressed in the standard (often SI) unit.
76       * @param decrement R, a relative typed DoubleScalar; amount by which the value is decremented
77       * @return Relative DoubleScalar
78       */
79      public final R minus(final R decrement)
80      {
81          if (getUnit().isBaseSIUnit())
82          {
83              return instantiateRel(this.si - decrement.si, getUnit().getStandardUnit());
84          }
85          return getUnit().equals(decrement.getUnit()) ? instantiateRel(getInUnit() - decrement.getInUnit(), getUnit())
86                  : instantiateRel(this.si - decrement.si, getUnit().getStandardUnit());
87      }
88  
89      /**
90       * Interpolate between two values.
91       * @param zero Duration; the low value
92       * @param one Duration; the high value
93       * @param ratio double; the ratio between 0 and 1, inclusive
94       * @return a Scalar at the ratio between
95       * @param <U> the unit
96       * @param <R> the Relative class for reference purposes
97       */
98      public static <U extends Unit<U>, R extends AbstractDoubleScalarRel<U, R>> R interpolate(final R zero, final R one,
99              final double ratio)
100     {
101         return zero.instantiateRel(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
102     }
103 
104     /**********************************************************************************/
105     /********************************** MATH METHODS **********************************/
106     /**********************************************************************************/
107 
108     /** {@inheritDoc} */
109     @Override
110     @SuppressWarnings("checkstyle:designforextension")
111     public R abs()
112     {
113         return instantiateRel(Math.abs(getInUnit()), getUnit());
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     @SuppressWarnings("checkstyle:designforextension")
119     public R ceil()
120     {
121         return instantiateRel(Math.ceil(getInUnit()), getUnit());
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public R floor()
128     {
129         return instantiateRel(Math.floor(getInUnit()), getUnit());
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     @SuppressWarnings("checkstyle:designforextension")
135     public R rint()
136     {
137         return instantiateRel(Math.rint(getInUnit()), getUnit());
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     @SuppressWarnings("checkstyle:designforextension")
143     public R round()
144     {
145         return instantiateRel(Math.round(getInUnit()), getUnit());
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     @SuppressWarnings("checkstyle:designforextension")
151     public R neg()
152     {
153         return instantiateRel(-getInUnit(), getUnit());
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     @SuppressWarnings("checkstyle:designforextension")
159     public R multiplyBy(final double constant)
160     {
161         return instantiateRel(getInUnit() * constant, getUnit());
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     @SuppressWarnings("checkstyle:designforextension")
167     public R divideBy(final double constant)
168     {
169         return instantiateRel(getInUnit() / constant, getUnit());
170     }
171 }