View Javadoc
1   package org.djunits.value.vdouble.scalar;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.value.Absolute;
5   import org.djunits.value.Scalar;
6   import org.djunits.value.ValueUtil;
7   import org.djunits.value.formatter.Format;
8   
9   /**
10   * The most basic abstract class for the DoubleScalar.
11   * <p>
12   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
14   * </p>
15   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
16   * initial version Oct 15, 2016 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   * @param <U> the unit
21   * @param <T> the type
22   */
23  public abstract class AbstractDoubleScalar<U extends Unit<U>, T extends AbstractDoubleScalar<U, T>> extends Scalar<U>
24          implements DoubleScalarInterface, Comparable<T>
25  {
26      /** */
27      private static final long serialVersionUID = 20161015L;
28  
29      /** The value, stored in the standard SI unit. */
30      @SuppressWarnings("checkstyle:visibilitymodifier")
31      public final double si;
32  
33      /**
34       * @param unit the unit
35       * @param si the si value to store
36       */
37      AbstractDoubleScalar(final U unit, final double si)
38      {
39          super(unit);
40          this.si = si;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public final double getSI()
46      {
47          return this.si;
48      }
49  
50      /**
51       * Test if this DoubleScalar is less than a DoubleScalar.
52       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
53       * @return boolean
54       */
55      public final boolean lt(final T o)
56      {
57          return this.si < o.si;
58      }
59  
60      /**
61       * Test if this DoubleScalar is less than or equal to a DoubleScalar.
62       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
63       * @return boolean
64       */
65      public final boolean le(final T o)
66      {
67          return this.si <= o.si;
68      }
69  
70      /**
71       * Test if this DoubleScalar is greater than or equal to a DoubleScalar.
72       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
73       * @return boolean
74       */
75      public final boolean gt(final T o)
76      {
77          return this.si > o.si;
78      }
79  
80      /**
81       * Test if this DoubleScalar is greater than a DoubleScalar.
82       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
83       * @return boolean
84       */
85      public final boolean ge(final T o)
86      {
87          return this.si >= o.si;
88      }
89  
90      /**
91       * Test if this DoubleScalar is equal to a DoubleScalar.
92       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
93       * @return boolean
94       */
95      public final boolean eq(final T o)
96      {
97          return this.si == o.si;
98      }
99  
100     /**
101      * Test if this DoubleScalar is not equal to a DoubleScalar.
102      * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
103      * @return boolean
104      */
105     public final boolean ne(final T o)
106     {
107         return this.si != o.si;
108     }
109 
110     /**
111      * Test if this DoubleScalar is less than 0.0.
112      * @return boolean
113      */
114     public final boolean lt0()
115     {
116         return this.si < 0.0;
117     }
118 
119     /**
120      * Test if this DoubleScalar is less than or equal to 0.0.
121      * @return boolean
122      */
123     public final boolean le0()
124     {
125         return this.si <= 0.0;
126     }
127 
128     /**
129      * Test if this DoubleScalar is greater than or equal to 0.0.
130      * @return boolean
131      */
132     public final boolean gt0()
133     {
134         return this.si > 0.0;
135     }
136 
137     /**
138      * Test if this DoubleScalar is greater than 0.0.
139      * @return boolean
140      */
141     public final boolean ge0()
142     {
143         return this.si >= 0.0;
144     }
145 
146     /**
147      * Test if this DoubleScalar is equal to 0.0.
148      * @return boolean
149      */
150     public final boolean eq0()
151     {
152         return this.si == 0.0;
153     }
154 
155     /**
156      * Test if this DoubleScalar is not equal to 0.0.
157      * @return boolean
158      */
159     public final boolean ne0()
160     {
161         return this.si != 0.0;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     @SuppressWarnings("checkstyle:designforextension")
167     public final int compareTo(final T o)
168     {
169         return Double.compare(this.si, o.si);
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public final double getInUnit()
175     {
176         return expressAsSpecifiedUnit(getSI());
177     }
178 
179     /**
180      * Retrieve the value converted into some specified unit.
181      * @param targetUnit U; the unit to convert the value into
182      * @return double
183      */
184     public final double getInUnit(final U targetUnit)
185     {
186         return ValueUtil.expressAsUnit(getSI(), targetUnit);
187     }
188 
189     /**********************************************************************************/
190     /********************************* NUMBER METHODS *********************************/
191     /**********************************************************************************/
192 
193     /** {@inheritDoc} */
194     @Override
195     public final int intValue()
196     {
197         return (int) Math.round(getSI());
198     }
199 
200     /** {@inheritDoc} */
201     @Override
202     public final long longValue()
203     {
204         return Math.round(getSI());
205     }
206 
207     /** {@inheritDoc} */
208     @Override
209     public final float floatValue()
210     {
211         return (float) getSI();
212     }
213 
214     /** {@inheritDoc} */
215     @Override
216     public final double doubleValue()
217     {
218         return getSI();
219     }
220 
221     /** {@inheritDoc} */
222     @Override
223     public final String toString()
224     {
225         return toString(getUnit(), false, true);
226     }
227 
228     /**
229      * Print this DoubleScalar with the value expressed in the specified unit.
230      * @param displayUnit U; the unit into which the value is converted for display
231      * @return String; printable string with the scalar contents expressed in the specified unit
232      */
233     public final String toString(final U displayUnit)
234     {
235         return toString(displayUnit, false, true);
236     }
237 
238     /**
239      * Print this DoubleScalar with optional type and unit information.
240      * @param verbose boolean; if true; include type info; if false; exclude type info
241      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
242      * @return String; printable string with the scalar contents
243      */
244     public final String toString(final boolean verbose, final boolean withUnit)
245     {
246         return toString(getUnit(), verbose, withUnit);
247     }
248 
249     /**
250      * Print this DoubleScalar with the value expressed in the specified unit.
251      * @param displayUnit U; the unit into which the value is converted for display
252      * @param verbose boolean; if true; include type info; if false; exclude type info
253      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
254      * @return String; printable string with the scalar contents
255      */
256     public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
257     {
258         StringBuffer buf = new StringBuffer();
259         if (verbose)
260         {
261             buf.append(this instanceof Absolute ? "Abs " : "Rel ");
262         }
263         double d = ValueUtil.expressAsUnit(getSI(), displayUnit);
264         buf.append(Format.format(d));
265         if (withUnit)
266         {
267             buf.append(displayUnit.getAbbreviation());
268         }
269         return buf.toString();
270     }
271 
272     /** {@inheritDoc} */
273     @Override
274     @SuppressWarnings("checkstyle:designforextension")
275     public int hashCode()
276     {
277         final int prime = 31;
278         int result = getUnit().getStandardUnit().hashCode();
279         long temp;
280         temp = Double.doubleToLongBits(this.si);
281         result = prime * result + (int) (temp ^ (temp >>> 32));
282         return result;
283     }
284 
285     /** {@inheritDoc} */
286     @Override
287     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces", "unchecked" })
288     public boolean equals(final Object obj)
289     {
290         if (this == obj)
291             return true;
292         if (obj == null)
293             return false;
294         if (getClass() != obj.getClass())
295             return false;
296         AbstractDoubleScalar<U, T> other = (AbstractDoubleScalar<U, T>) obj;
297         if (!getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
298             return false;
299         if (Double.doubleToLongBits(this.si) != Double.doubleToLongBits(other.si))
300             return false;
301         return true;
302     }
303 
304 }