View Javadoc
1   package org.djunits.value.vfloat.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 FloatScalarAbs class that forms the basis of all FloatScalar 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-2023 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   * @param <AU> the absolute unit
17   * @param <A> the Absolute class for reference purposes
18   * @param <RU> the relative unit
19   * @param <R> the Relative class for reference purposes
20   */
21  public abstract class AbstractFloatScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>,
22          A extends AbstractFloatScalarAbs<AU, A, RU, R>, RU extends Unit<RU>,
23          R extends AbstractFloatScalarRelWithAbs<AU, A, RU, R>> extends AbstractFloatScalar<AU, A>
24          implements FloatScalarInterface.Abs<AU, A, RU, R>
25  {
26      /**  */
27      private static final long serialVersionUID = 20150626L;
28  
29      /**
30       * Construct a new Absolute Immutable FloatScalar.
31       * @param value float; the value of the new Absolute Immutable FloatScalar
32       * @param unit AU; the unit of the new Absolute Immutable FloatScalar
33       */
34      public AbstractFloatScalarAbs(final float value, final AU unit)
35      {
36          super(unit, unit.isBaseSIUnit() ? value : (float) ValueUtil.expressAsSIUnit(value, unit));
37      }
38  
39      /**
40       * Construct a new Absolute Immutable FloatScalar from an existing Absolute Immutable FloatScalar.
41       * @param value A, an absolute typed FloatScalar; the reference
42       */
43      public AbstractFloatScalarAbs(final A value)
44      {
45          super(value.getDisplayUnit(), value.getSI());
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public final A plus(final R increment)
51      {
52          AU targetUnit = getDisplayUnit();
53          return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public final A minus(final R decrement)
59      {
60          AU targetUnit = getDisplayUnit();
61          return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public final R minus(final A decrement)
67      {
68          RU targetUnit = getDisplayUnit().getRelativeUnit();
69          return instantiateRel(getInUnit() - decrement.getInUnit(getDisplayUnit()), targetUnit);
70      }
71  
72      /**********************************************************************************/
73      /********************************** MATH METHODS **********************************/
74      /**********************************************************************************/
75  
76      /** {@inheritDoc} */
77      @Override
78      @SuppressWarnings("checkstyle:designforextension")
79      public A abs()
80      {
81          return instantiateAbs(Math.abs(getInUnit()), getDisplayUnit());
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      @SuppressWarnings("checkstyle:designforextension")
87      public A ceil()
88      {
89          return instantiateAbs((float) Math.ceil(getInUnit()), getDisplayUnit());
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      @SuppressWarnings("checkstyle:designforextension")
95      public A floor()
96      {
97          return instantiateAbs((float) Math.floor(getInUnit()), getDisplayUnit());
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     @SuppressWarnings("checkstyle:designforextension")
103     public A neg()
104     {
105         return instantiateAbs(-getInUnit(), getDisplayUnit());
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     @SuppressWarnings("checkstyle:designforextension")
111     public A rint()
112     {
113         return instantiateAbs((float) Math.rint(getInUnit()), getDisplayUnit());
114     }
115 
116 }