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