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