View Javadoc
1   package org.djunits.value.vfloat.scalar.base;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.value.Relative;
5   import org.djunits.value.vfloat.scalar.FloatDimensionless;
6   import org.djunits.value.vfloat.scalar.FloatSIScalar;
7   
8   /**
9    * The typed, abstract FloatScalarRel 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-2025 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 <U> the unit
18   * @param <R> the Relative class for reference purposes
19   */
20  public abstract class FloatScalarRel<U extends Unit<U>, R extends FloatScalarRel<U, R>> extends FloatScalar<U, R>
21          implements Relative<U, R>
22  {
23      /**  */
24      private static final long serialVersionUID = 20150626L;
25  
26      /**
27       * Construct a new Relative Immutable FloatScalar.
28       * @param value the value of the new Relative Immutable FloatScalar
29       * @param unit the unit of the new Relative Immutable FloatScalar
30       */
31      public FloatScalarRel(final float value, final U unit)
32      {
33          super(value, unit);
34      }
35  
36      /**
37       * Construct a new Relative Immutable FloatScalar from an existing Relative Immutable FloatScalar.
38       * @param value the reference
39       */
40      public FloatScalarRel(final R value)
41      {
42          super(value);
43      }
44  
45      /**
46       * Construct a new Relative Immutable FloatScalar of the right type. Each extending class must implement this method.
47       * @param value the float value
48       * @param unit the unit
49       * @return R a new relative instance of the FloatScalar of the right type
50       */
51      public abstract R instantiateRel(float value, U unit);
52  
53      @Override
54      public final R plus(final R increment)
55      {
56          if (getDisplayUnit().isBaseSIUnit())
57          {
58              return instantiateRel(this.getSI() + increment.getSI(), getDisplayUnit().getStandardUnit());
59          }
60          return getDisplayUnit().equals(increment.getDisplayUnit())
61                  ? instantiateRel(getInUnit() + increment.getInUnit(), getDisplayUnit())
62                  : instantiateRel(this.getSI() + increment.getSI(), getDisplayUnit().getStandardUnit());
63      }
64  
65      @Override
66      public final R minus(final R decrement)
67      {
68          if (getDisplayUnit().isBaseSIUnit())
69          {
70              return instantiateRel(this.getSI() - decrement.getSI(), getDisplayUnit().getStandardUnit());
71          }
72          return getDisplayUnit().equals(decrement.getDisplayUnit())
73                  ? instantiateRel(getInUnit() - decrement.getInUnit(), getDisplayUnit())
74                  : instantiateRel(this.getSI() - decrement.getSI(), getDisplayUnit().getStandardUnit());
75      }
76  
77      /**
78       * Multiply this scalar by another scalar and create a new scalar.
79       * @param otherScalar the value by which this scalar is multiplied
80       * @return a new scalar instance with correct SI dimensions
81       */
82      public FloatSIScalar times(final FloatScalarRel<?, ?> otherScalar)
83      {
84          return FloatSIScalar.multiply(this, otherScalar);
85      }
86  
87      /**
88       * Create the reciprocal of this scalar with the correct dimensions.
89       * @return a new scalar instance with correct SI dimensions
90       */
91      public FloatScalarRel<?, ?> reciprocal()
92      {
93          return FloatSIScalar.divide(FloatDimensionless.ONE, this);
94      }
95  
96      /**
97       * Divide this scalar by another scalar and create a new scalar.
98       * @param otherScalar the value by which this scalar is divided
99       * @return a new scalar instance with correct SI dimensions
100      */
101     public FloatSIScalar divide(final FloatScalarRel<?, ?> otherScalar)
102     {
103         return FloatSIScalar.divide(this, otherScalar);
104     }
105 
106     /**********************************************************************************/
107     /********************************** MATH METHODS **********************************/
108     /**********************************************************************************/
109 
110     @Override
111     @SuppressWarnings("checkstyle:designforextension")
112     public R abs()
113     {
114         return instantiateRel(Math.abs(getInUnit()), getDisplayUnit());
115     }
116 
117     @Override
118     @SuppressWarnings("checkstyle:designforextension")
119     public R ceil()
120     {
121         return instantiateRel((float) Math.ceil(getInUnit()), getDisplayUnit());
122     }
123 
124     @Override
125     @SuppressWarnings("checkstyle:designforextension")
126     public R floor()
127     {
128         return instantiateRel((float) Math.floor(getInUnit()), getDisplayUnit());
129     }
130 
131     @Override
132     @SuppressWarnings("checkstyle:designforextension")
133     public R rint()
134     {
135         return instantiateRel((float) Math.rint(getInUnit()), getDisplayUnit());
136     }
137 
138     @Override
139     @SuppressWarnings("checkstyle:designforextension")
140     public R neg()
141     {
142         return instantiateRel(-getInUnit(), getDisplayUnit());
143     }
144 
145     @Override
146     @SuppressWarnings("checkstyle:designforextension")
147     public R times(final double constant)
148     {
149         return instantiateRel((float) (getInUnit() * constant), getDisplayUnit());
150     }
151 
152     @Override
153     @SuppressWarnings("checkstyle:designforextension")
154     public R divide(final double constant)
155     {
156         return instantiateRel((float) (getInUnit() / constant), getDisplayUnit());
157     }
158 
159     @Override
160     @SuppressWarnings("checkstyle:designforextension")
161     public R times(final float constant)
162     {
163         return instantiateRel(getInUnit() * constant, getDisplayUnit());
164     }
165 
166     @Override
167     @SuppressWarnings("checkstyle:designforextension")
168     public R divide(final float constant)
169     {
170         return instantiateRel(getInUnit() / constant, getDisplayUnit());
171     }
172 
173 }