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