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.util.ValueUtil;
6   
7   /**
8    * The typed, abstract DoubleScalarAbs class that forms the basis of all DoubleScalar definitions and extensions.<br>
9    * Note: A relative scalar class can implement the toAbs() method if it has an absolute equivalent.
10   * <p>
11   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
13   * </p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
16   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</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 AbstractDoubleScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>,
23          A extends AbstractDoubleScalarAbs<AU, A, RU, R>, RU extends Unit<RU>,
24          R extends AbstractDoubleScalarRelWithAbs<AU, A, RU, R>> extends AbstractDoubleScalar<AU, A>
25          implements DoubleScalarInterface.Abs<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 AbstractDoubleScalarAbs(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 AbstractDoubleScalarAbs(final A value)
45      {
46          super(value.getDisplayUnit(), value.getSI());
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public final A plus(final R increment)
52      {
53          AU targetUnit = getDisplayUnit();
54          return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public final A minus(final R decrement)
60      {
61          AU targetUnit = getDisplayUnit();
62          return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final R minus(final A decrement)
68      {
69          RU targetUnit = getDisplayUnit().getRelativeUnit();
70          return instantiateRel(getInUnit() - decrement.getInUnit(getDisplayUnit()), targetUnit);
71      }
72  
73      /**********************************************************************************/
74      /********************************** MATH METHODS **********************************/
75      /**********************************************************************************/
76  
77      /** {@inheritDoc} */
78      @Override
79      @SuppressWarnings("checkstyle:designforextension")
80      public A abs()
81      {
82          return instantiateAbs(Math.abs(getInUnit()), getDisplayUnit());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      @SuppressWarnings("checkstyle:designforextension")
88      public A ceil()
89      {
90          return instantiateAbs(Math.ceil(getInUnit()), getDisplayUnit());
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      @SuppressWarnings("checkstyle:designforextension")
96      public A floor()
97      {
98          return instantiateAbs(Math.floor(getInUnit()), getDisplayUnit());
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     @SuppressWarnings("checkstyle:designforextension")
104     public A neg()
105     {
106         return instantiateAbs(-getInUnit(), getDisplayUnit());
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     @SuppressWarnings("checkstyle:designforextension")
112     public A rint()
113     {
114         return instantiateAbs(Math.rint(getInUnit()), getDisplayUnit());
115     }
116 
117 }