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