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-2017 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 the double value
52       * @param unit 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      /********************************** MATH METHODS **********************************/
91      /**********************************************************************************/
92  
93      /** {@inheritDoc} */
94      @Override
95      @SuppressWarnings("checkstyle:designforextension")
96      public R abs()
97      {
98          return instantiateRel(Math.abs(getInUnit()), getUnit());
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     @SuppressWarnings("checkstyle:designforextension")
104     public R ceil()
105     {
106         return instantiateRel(Math.ceil(getInUnit()), getUnit());
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     @SuppressWarnings("checkstyle:designforextension")
112     public R floor()
113     {
114         return instantiateRel(Math.floor(getInUnit()), getUnit());
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     @SuppressWarnings("checkstyle:designforextension")
120     public R rint()
121     {
122         return instantiateRel(Math.rint(getInUnit()), getUnit());
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     @SuppressWarnings("checkstyle:designforextension")
128     public R round()
129     {
130         return instantiateRel(Math.round(getInUnit()), getUnit());
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     @SuppressWarnings("checkstyle:designforextension")
136     public R neg()
137     {
138         return instantiateRel(-getInUnit(), getUnit());
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     @SuppressWarnings("checkstyle:designforextension")
144     public R multiplyBy(final double constant)
145     {
146         return instantiateRel(getInUnit() * constant, getUnit());
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     @SuppressWarnings("checkstyle:designforextension")
152     public R divideBy(final double constant)
153     {
154         return instantiateRel(getInUnit() / constant, getUnit());
155     }
156 }