View Javadoc
1   package org.djunits.value.vfloat.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 FloatScalar.
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 16, 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 class AbstractFloatScalar<U extends Unit<U>, T extends AbstractFloatScalar<U, T>> extends Scalar<U>
24          implements FloatScalarInterface, 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 float si;
32  
33      /**
34       * @param unit the unit
35       * @param si the si value to store
36       */
37      AbstractFloatScalar(final U unit, final float si)
38      {
39          super(unit);
40          this.si = si;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public final float getSI()
46      {
47          return this.si;
48      }
49  
50      /**
51       * Test if this Relative FloatScalar is less than a Relative FloatScalar.
52       * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is less than or equal to a Relative FloatScalar.
62       * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is greater than or equal to a Relative FloatScalar.
72       * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is greater than a Relative FloatScalar.
82       * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is equal to a Relative FloatScalar.
92       * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is not equal to a Relative FloatScalar.
102      * @param o T, a relative typed FloatScalar; 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 Relative FloatScalar is less than 0.0.
112      * @return boolean
113      */
114     public final boolean lt0()
115     {
116         return this.si < 0.0f;
117     }
118 
119     /**
120      * Test if this Relative FloatScalar is less than or equal to 0.0.
121      * @return boolean
122      */
123     public final boolean le0()
124     {
125         return this.si <= 0.0f;
126     }
127 
128     /**
129      * Test if this Relative FloatScalar is greater than or equal to 0.0.
130      * @return boolean
131      */
132     public final boolean gt0()
133     {
134         return this.si > 0.0f;
135     }
136 
137     /**
138      * Test if this Relative FloatScalar is greater than 0.0.
139      * @return boolean
140      */
141     public final boolean ge0()
142     {
143         return this.si >= 0.0f;
144     }
145 
146     /**
147      * Test if this Relative FloatScalar is equal to 0.0.
148      * @return boolean
149      */
150     public final boolean eq0()
151     {
152         return this.si == 0.0f;
153     }
154 
155     /**
156      * Test if this Relative FloatScalar is not equal to 0.0.
157      * @return boolean
158      */
159     public final boolean ne0()
160     {
161         return this.si != 0.0f;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public final int compareTo(final T o)
167     {
168         return Float.compare(this.si, o.si);
169     }
170 
171     /**
172      * Retrieve the value in the original unit.
173      * @return float
174      */
175     public final float getInUnit()
176     {
177         return (float) expressAsSpecifiedUnit(getSI());
178     }
179 
180     /**
181      * Retrieve the value converted into some specified unit.
182      * @param targetUnit U; the unit to convert the value into
183      * @return float
184      */
185     public final float getInUnit(final U targetUnit)
186     {
187         return (float) ValueUtil.expressAsUnit(getSI(), targetUnit);
188     }
189 
190     /**********************************************************************************/
191     /********************************* NUMBER METHODS *********************************/
192     /**********************************************************************************/
193 
194     /** {@inheritDoc} */
195     @Override
196     public final int intValue()
197     {
198         return Math.round(getSI());
199     }
200 
201     /** {@inheritDoc} */
202     @Override
203     public final long longValue()
204     {
205         return Math.round(getSI());
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     public final double doubleValue()
211     {
212         return getSI();
213     }
214 
215     /** {@inheritDoc} */
216     @Override
217     public final float floatValue()
218     {
219         return getSI();
220     }
221 
222     /** {@inheritDoc} */
223     @Override
224     public final String toString()
225     {
226         return toString(getUnit(), false, true);
227     }
228 
229     /**
230      * Print this FloatScalar with the value expressed in the specified unit.
231      * @param displayUnit U; the unit into which the value is converted for display
232      * @return String; printable string with the scalar contents expressed in the specified unit
233      */
234     public final String toString(final U displayUnit)
235     {
236         return toString(displayUnit, false, true);
237     }
238 
239     /**
240      * Print this FloatScalar with optional type and unit information.
241      * @param verbose boolean; if true; include type info; if false; exclude type info
242      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
243      * @return String; printable string with the scalar contents
244      */
245     public final String toString(final boolean verbose, final boolean withUnit)
246     {
247         return toString(getUnit(), verbose, withUnit);
248     }
249 
250     /**
251      * Print this FloatScalar with the value expressed in the specified unit.
252      * @param displayUnit U; the unit into which the value is converted for display
253      * @param verbose boolean; if true; include type info; if false; exclude type info
254      * @param withUnit boolean; if true; include the unit; of false; exclude the unit
255      * @return String; printable string with the scalar contents
256      */
257     public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
258     {
259         StringBuffer buf = new StringBuffer();
260         if (verbose)
261         {
262             buf.append(this instanceof Absolute ? "Abs " : "Rel ");
263         }
264         float d = (float) ValueUtil.expressAsUnit(getSI(), displayUnit);
265         buf.append(Format.format(d));
266         if (withUnit)
267         {
268             buf.append(displayUnit.getAbbreviation());
269         }
270         return buf.toString();
271     }
272 
273     /** {@inheritDoc} */
274     @Override
275     @SuppressWarnings("checkstyle:designforextension")
276     public int hashCode()
277     {
278         final int prime = 31;
279         int result = getUnit().getStandardUnit().hashCode();
280         result = prime * result + Float.floatToIntBits(this.si);
281         return result;
282     }
283 
284     /** {@inheritDoc} */
285     @Override
286     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces", "unchecked" })
287     public boolean equals(final Object obj)
288     {
289         if (this == obj)
290             return true;
291         if (obj == null)
292             return false;
293         if (getClass() != obj.getClass())
294             return false;
295         AbstractFloatScalar<U, T> other = (AbstractFloatScalar<U, T>) obj;
296         if (!getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
297             return false;
298         if (Float.floatToIntBits(this.si) != Float.floatToIntBits(other.si))
299             return false;
300         return true;
301     }
302 
303 }