View Javadoc
1   package org.djunits.value.vdouble.scalar.base;
2   
3   import org.djunits.unit.AbsoluteLinearUnit;
4   import org.djunits.unit.Unit;
5   import org.djunits.value.Absolute;
6   import org.djunits.value.util.ValueUtil;
7   
8   /**
9    * The typed, abstract DoubleScalarAbs class that forms the basis of all DoubleScalar definitions and extensions.<br>
10   * Note: A relative scalar class can implement the toAbs() method if it has an absolute equivalent.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
17   * @param <AU> the absolute unit
18   * @param <A> the Absolute class for reference purposes
19   * @param <RU> the relative unit
20   * @param <R> the Relative class for reference purposes
21   */
22  public abstract class DoubleScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>,
23          A extends DoubleScalarAbs<AU, A, RU, R>, RU extends Unit<RU>,
24          R extends DoubleScalarRelWithAbs<AU, A, RU, R>> extends DoubleScalar<AU, A>
25          implements Absolute<AU, A, RU, R>
26  {
27      /**  */
28      private static final long serialVersionUID = 20150626L;
29  
30      /**
31       * Construct a new Absolute Immutable DoubleScalar.
32       * @param value double; the value of the new Absolute Immutable DoubleScalar
33       * @param unit AU; the unit of the new Absolute Immutable DoubleScalar
34       */
35      public DoubleScalarAbs(final double value, final AU unit)
36      {
37          super(unit, unit.isBaseSIUnit() ? value : ValueUtil.expressAsSIUnit(value, unit));
38      }
39  
40      /**
41       * Construct a new Absolute Immutable DoubleScalar from an existing Absolute Immutable DoubleScalar.
42       * @param value A, an absolute typed DoubleScalar; the reference
43       */
44      public DoubleScalarAbs(final A value)
45      {
46          super(value.getDisplayUnit(), value.getSI());
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 RU; the unit
53       * @return R a new relative instance of the DoubleScalar of the right type
54       */
55      public abstract R instantiateRel(double value, RU unit);
56  
57      /**
58       * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method.
59       * @param value double; the double value
60       * @param unit AU; the absolute unit
61       * @return A a new absolute instance of the DoubleScalar of the right type
62       */
63      public abstract A instantiateAbs(double value, AU unit);
64      
65      @Override
66      public final A plus(final R increment)
67      {
68          AU targetUnit = getDisplayUnit();
69          return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
70      }
71  
72      @Override
73      public final A minus(final R decrement)
74      {
75          AU targetUnit = getDisplayUnit();
76          return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
77      }
78  
79      @Override
80      public final R minus(final A decrement)
81      {
82          RU targetUnit = getDisplayUnit().getRelativeUnit();
83          return instantiateRel(getInUnit() - decrement.getInUnit(getDisplayUnit()), targetUnit);
84      }
85  
86      /**********************************************************************************/
87      /********************************** MATH METHODS **********************************/
88      /**********************************************************************************/
89  
90      @Override
91      public A abs()
92      {
93          return instantiateAbs(Math.abs(getInUnit()), getDisplayUnit());
94      }
95  
96      @Override
97      public A ceil()
98      {
99          return instantiateAbs(Math.ceil(getInUnit()), getDisplayUnit());
100     }
101 
102     @Override
103     public A floor()
104     {
105         return instantiateAbs(Math.floor(getInUnit()), getDisplayUnit());
106     }
107 
108     @Override
109     public A neg()
110     {
111         return instantiateAbs(-getInUnit(), getDisplayUnit());
112     }
113 
114     @Override
115     public A rint()
116     {
117         return instantiateAbs(Math.rint(getInUnit()), getDisplayUnit());
118     }
119 
120 }