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