View Javadoc
1   package org.djunits.value.vfloat.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.vfloat.FloatMathFunctions;
8   
9   /**
10   * The typed, abstract FloatScalarRel class that forms the basis of all FloatScalar 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-2018 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
23   */
24  public abstract class AbstractFloatScalarRel<U extends Unit<U>, R extends AbstractFloatScalarRel<U, R>>
25          extends AbstractFloatScalar<U, R> implements Relative, MathFunctionsRel<R>, FloatMathFunctions<R>
26  {
27      /**  */
28      private static final long serialVersionUID = 20150626L;
29  
30      /**
31       * Construct a new Relative Immutable FloatScalar.
32       * @param value float; the value of the new Relative Immutable FloatScalar
33       * @param unit U; the unit of the new Relative Immutable FloatScalar
34       */
35      public AbstractFloatScalarRel(final float value, final U unit)
36      {
37          super(unit, unit.isBaseSIUnit() ? value : (float) ValueUtil.expressAsSIUnit(value, unit));
38      }
39  
40      /**
41       * Construct a new Relative Immutable FloatScalar from an existing Relative Immutable FloatScalar.
42       * @param value R, a relative typed FloatScalar; the reference
43       */
44      public AbstractFloatScalarRel(final R value)
45      {
46          super(value.getUnit(), value.si);
47      }
48  
49      /**
50       * Construct a new Relative Immutable FloatScalar of the right type. Each extending class must implement this method.
51       * @param value the float value
52       * @param unit the unit
53       * @return R a new relative instance of the FloatScalar of the right type
54       */
55      public abstract R instantiateRel(float 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 FloatScalar; amount by which the value is incremented
61       * @return Absolute FloatScalar
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 FloatScalar; amount by which the value is decremented
77       * @return Relative FloatScalar
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((float) Math.ceil(getInUnit()), getUnit());
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     @SuppressWarnings("checkstyle:designforextension")
112     public R floor()
113     {
114         return instantiateRel((float) Math.floor(getInUnit()), getUnit());
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     @SuppressWarnings("checkstyle:designforextension")
120     public R rint()
121     {
122         return instantiateRel((float) 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 float constant)
145     {
146         return instantiateRel(getInUnit() * constant, getUnit());
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     @SuppressWarnings("checkstyle:designforextension")
152     public R divideBy(final float constant)
153     {
154         return instantiateRel(getInUnit() / constant, getUnit());
155     }
156 }