View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import org.djunits.unit.DimensionlessUnit;
4   import org.djunits.unit.DurationUnit;
5   import org.djunits.unit.EnergyUnit;
6   import org.djunits.unit.ForceUnit;
7   import org.djunits.unit.LengthUnit;
8   import org.djunits.unit.MoneyUnit;
9   import org.djunits.unit.PowerUnit;
10  import org.djunits.unit.PressureUnit;
11  
12  /**
13   * Easy access methods for the Energy DoubleScalar, which is relative by definition. Instead of:
14   * 
15   * <pre>
16   * DoubleScalar.Rel&lt;EnergyUnit&gt; value = new DoubleScalar.Rel&lt;EnergyUnit&gt;(100.0, EnergyUnit.SI);
17   * </pre>
18   * 
19   * we can now write:
20   * 
21   * <pre>
22   * Energy value = new Energy(100.0, EnergyUnit.SI);
23   * </pre>
24   * 
25   * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
26   * used are compatible.
27   * <p>
28   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
30   * <p>
31   * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
32   * initial version Sep 5, 2015 <br>
33   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
35   */
36  public class Energy extends AbstractDoubleScalarRel<EnergyUnit, Energy>
37  {
38      /** */
39      private static final long serialVersionUID = 20150905L;
40  
41      /** constant with value zero. */
42      public static final Energy ZERO = new Energy(0.0, EnergyUnit.SI);
43  
44      /** constant with value NaN. */
45      @SuppressWarnings("checkstyle:constantname")
46      public static final Energy NaN = new Energy(Double.NaN, EnergyUnit.SI);
47  
48      /** constant with value POSITIVE_INFINITY. */
49      public static final Energy POSITIVE_INFINITY = new Energy(Double.POSITIVE_INFINITY, EnergyUnit.SI);
50  
51      /** constant with value NEGATIVE_INFINITY. */
52      public static final Energy NEGATIVE_INFINITY = new Energy(Double.NEGATIVE_INFINITY, EnergyUnit.SI);
53  
54      /** constant with value MAX_VALUE. */
55      public static final Energy POS_MAXVALUE = new Energy(Double.MAX_VALUE, EnergyUnit.SI);
56  
57      /** constant with value -MAX_VALUE. */
58      public static final Energy NEG_MAXVALUE = new Energy(-Double.MAX_VALUE, EnergyUnit.SI);
59  
60      /**
61       * Construct Energy scalar.
62       * @param value double value
63       * @param unit unit for the double value
64       */
65      public Energy(final double value, final EnergyUnit unit)
66      {
67          super(value, unit);
68      }
69  
70      /**
71       * Construct Energy scalar.
72       * @param value Scalar from which to construct this instance
73       */
74      public Energy(final Energy value)
75      {
76          super(value);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final Energy instantiateRel(final double value, final EnergyUnit unit)
82      {
83          return new Energy(value, unit);
84      }
85  
86      /**
87       * Construct Energy scalar.
88       * @param value double value in SI units
89       * @return the new scalar with the SI value
90       */
91      public static final Energy createSI(final double value)
92      {
93          return new Energy(value, EnergyUnit.SI);
94      }
95  
96      /**
97       * Interpolate between two values.
98       * @param zero the low value
99       * @param one the high value
100      * @param ratio the ratio between 0 and 1, inclusive
101      * @return a Scalar at the ratio between
102      */
103     public static Energy interpolate(final Energy zero, final Energy one, final double ratio)
104     {
105         return new Energy(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
106     }
107 
108     /**
109      * Return the maximum value of two relative scalars.
110      * @param r1 the first scalar
111      * @param r2 the second scalar
112      * @return the maximum value of two relative scalars
113      */
114     public static Energy max(final Energy r1, final Energy r2)
115     {
116         return (r1.gt(r2)) ? r1 : r2;
117     }
118 
119     /**
120      * Return the maximum value of more than two relative scalars.
121      * @param r1 the first scalar
122      * @param r2 the second scalar
123      * @param rn the other scalars
124      * @return the maximum value of more than two relative scalars
125      */
126     public static Energy max(final Energy r1, final Energy r2, final Energy... rn)
127     {
128         Energy maxr = (r1.gt(r2)) ? r1 : r2;
129         for (Energy r : rn)
130         {
131             if (r.gt(maxr))
132             {
133                 maxr = r;
134             }
135         }
136         return maxr;
137     }
138 
139     /**
140      * Return the minimum value of two relative scalars.
141      * @param r1 the first scalar
142      * @param r2 the second scalar
143      * @return the minimum value of two relative scalars
144      */
145     public static Energy min(final Energy r1, final Energy r2)
146     {
147         return (r1.lt(r2)) ? r1 : r2;
148     }
149 
150     /**
151      * Return the minimum value of more than two relative scalars.
152      * @param r1 the first scalar
153      * @param r2 the second scalar
154      * @param rn the other scalars
155      * @return the minimum value of more than two relative scalars
156      */
157     public static Energy min(final Energy r1, final Energy r2, final Energy... rn)
158     {
159         Energy minr = (r1.lt(r2)) ? r1 : r2;
160         for (Energy r : rn)
161         {
162             if (r.lt(minr))
163             {
164                 minr = r;
165             }
166         }
167         return minr;
168     }
169 
170     /**
171      * Calculate the division of Energy and Energy, which results in a Dimensionless scalar.
172      * @param v Energy scalar
173      * @return Dimensionless scalar as a division of Energy and Energy
174      */
175     public final Dimensionless divideBy(final Energy v)
176     {
177         return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
178     }
179 
180     /**
181      * Calculate the division of Energy and Force, which results in a Length scalar.
182      * @param v Energy scalar
183      * @return Length scalar as a division of Energy and Force
184      */
185     public final Length divideBy(final Force v)
186     {
187         return new Length(this.si / v.si, LengthUnit.SI);
188     }
189 
190     /**
191      * Calculate the division of Energy and Length, which results in a Force scalar.
192      * @param v Energy scalar
193      * @return Force scalar as a division of Energy and Length
194      */
195     public final Force divideBy(final Length v)
196     {
197         return new Force(this.si / v.si, ForceUnit.SI);
198     }
199 
200     /**
201      * Calculate the multiplication of Energy and LinearDensity, which results in a Force scalar.
202      * @param v Energy scalar
203      * @return Force scalar as a multiplication of Energy and LinearDensity
204      */
205     public final Force multiplyBy(final LinearDensity v)
206     {
207         return new Force(this.si * v.si, ForceUnit.SI);
208     }
209 
210     /**
211      * Calculate the division of Energy and Duration, which results in a Power scalar.
212      * @param v Energy scalar
213      * @return Power scalar as a division of Energy and Duration
214      */
215     public final Power divideBy(final Duration v)
216     {
217         return new Power(this.si / v.si, PowerUnit.SI);
218     }
219 
220     /**
221      * Calculate the division of Energy and Power, which results in a Duration scalar.
222      * @param v Energy scalar
223      * @return Duration scalar as a division of Energy and Power
224      */
225     public final Duration divideBy(final Power v)
226     {
227         return new Duration(this.si / v.si, DurationUnit.SI);
228     }
229 
230     /**
231      * Calculate the division of Energy and Volume, which results in a Pressure scalar.
232      * @param v Energy scalar
233      * @return Pressure scalar as a division of Energy and Volume
234      */
235     public final Pressure divideBy(final Volume v)
236     {
237         return new Pressure(this.si / v.si, PressureUnit.SI);
238     }
239 
240     /**
241      * Calculate the multiplication of Energy and Frequency, which results in a Power scalar.
242      * @param v Energy scalar
243      * @return Power scalar as a multiplication of Energy and Frequency
244      */
245     public final Power multiplyBy(final Frequency v)
246     {
247         return new Power(this.si * v.si, PowerUnit.SI);
248     }
249 
250     /**
251      * Calculate the multiplication of Energy and MoneyPerEnergy, which results in a Money scalar.
252      * @param v Energy scalar
253      * @return Money scalar as a multiplication of Energy and MoneyPerEnergy
254      */
255     public final Money multiplyBy(final MoneyPerEnergy v)
256     {
257         return new Money(this.si * v.si, MoneyUnit.getStandardMoneyUnit());
258     }
259 
260 }