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.Absolute;
6   import org.djunits.value.util.ValueUtil;
7   
8   /**
9    * The typed, abstract FloatScalarAbs class that forms the basis of all FloatScalar 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 FloatScalarAbs<AU extends AbsoluteLinearUnit<AU, RU>,
23          A extends FloatScalarAbs<AU, A, RU, R>, RU extends Unit<RU>,
24          R extends FloatScalarRelWithAbs<AU, A, RU, R>> extends FloatScalar<AU, A> implements Absolute<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 FloatScalarAbs(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 FloatScalarAbs(final A value)
44      {
45          super(value.getDisplayUnit(), value.getSI());
46      }
47  
48      /**
49       * Construct a new Relative Immutable FloatScalar of the right type. Each extending class must implement this method.
50       * @param value float; the float value
51       * @param unit RU; the unit
52       * @return R a new relative instance of the FloatScalar of the right type
53       */
54      public abstract R instantiateRel(float value, RU unit);
55  
56      /**
57       * Construct a new Absolute Immutable FloatScalar of the right type. Each extending class must implement this method.
58       * @param value float; the float value
59       * @param unit AU; the absolute unit
60       * @return A a new absolute instance of the FloatScalar of the right type
61       */
62      public abstract A instantiateAbs(float value, AU unit);
63  
64      @Override
65      public final A plus(final R increment)
66      {
67          AU targetUnit = getDisplayUnit();
68          return instantiateAbs(getInUnit() + increment.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
69      }
70  
71      @Override
72      public final A minus(final R decrement)
73      {
74          AU targetUnit = getDisplayUnit();
75          return instantiateAbs(getInUnit() - decrement.getInUnit(targetUnit.getRelativeUnit()), targetUnit);
76      }
77  
78      @Override
79      public final R minus(final A decrement)
80      {
81          RU targetUnit = getDisplayUnit().getRelativeUnit();
82          return instantiateRel(getInUnit() - decrement.getInUnit(getDisplayUnit()), targetUnit);
83      }
84  
85      /**********************************************************************************/
86      /********************************** MATH METHODS **********************************/
87      /**********************************************************************************/
88  
89      @Override
90      @SuppressWarnings("checkstyle:designforextension")
91      public A abs()
92      {
93          return instantiateAbs(Math.abs(getInUnit()), getDisplayUnit());
94      }
95  
96      @Override
97      @SuppressWarnings("checkstyle:designforextension")
98      public A ceil()
99      {
100         return instantiateAbs((float) Math.ceil(getInUnit()), getDisplayUnit());
101     }
102 
103     @Override
104     @SuppressWarnings("checkstyle:designforextension")
105     public A floor()
106     {
107         return instantiateAbs((float) Math.floor(getInUnit()), getDisplayUnit());
108     }
109 
110     @Override
111     @SuppressWarnings("checkstyle:designforextension")
112     public A neg()
113     {
114         return instantiateAbs(-getInUnit(), getDisplayUnit());
115     }
116 
117     @Override
118     @SuppressWarnings("checkstyle:designforextension")
119     public A rint()
120     {
121         return instantiateAbs((float) Math.rint(getInUnit()), getDisplayUnit());
122     }
123 
124 }