View Javadoc
1   package org.djunits.value.vfloat.scalar.base;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.value.Absolute;
5   import org.djunits.value.AbstractScalar;
6   import org.djunits.value.formatter.Format;
7   import org.djunits.value.util.ValueUtil;
8   
9   /**
10   * The most basic abstract class for the FloatScalar.
11   * <p>
12   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
17   * @param <U> the unit
18   * @param <S> the type
19   */
20  public abstract class AbstractFloatScalar<U extends Unit<U>, S extends AbstractFloatScalar<U, S>> extends AbstractScalar<U, S>
21          implements FloatScalarInterface<U, S>
22  {
23      /** */
24      private static final long serialVersionUID = 20161015L;
25  
26      /** The value, stored in the standard SI unit. */
27      @SuppressWarnings("checkstyle:visibilitymodifier")
28      public final float si;
29  
30      /**
31       * Construct a new AbstractFloatScalar.
32       * @param unit U; the unit
33       * @param si float; the si value to store
34       */
35      AbstractFloatScalar(final U unit, final float si)
36      {
37          super(unit);
38          this.si = si;
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public final float getSI()
44      {
45          return this.si;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public final float getInUnit()
51      {
52          return (float) ValueUtil.expressAsUnit(getSI(), getDisplayUnit());
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public final float getInUnit(final U targetUnit)
58      {
59          return (float) ValueUtil.expressAsUnit(getSI(), targetUnit);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public final boolean lt(final S o)
65      {
66          return this.getSI() < o.getSI();
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final boolean le(final S o)
72      {
73          return this.getSI() <= o.getSI();
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public final boolean gt(final S o)
79      {
80          return this.getSI() > o.getSI();
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final boolean ge(final S o)
86      {
87          return this.getSI() >= o.getSI();
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public final boolean eq(final S o)
93      {
94          return this.getSI() == o.getSI();
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public final boolean ne(final S o)
100     {
101         return this.getSI() != o.getSI();
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public final boolean lt0()
107     {
108         return this.getSI() < 0.0;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public final boolean le0()
114     {
115         return this.getSI() <= 0.0;
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public final boolean gt0()
121     {
122         return this.getSI() > 0.0;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public final boolean ge0()
128     {
129         return this.getSI() >= 0.0;
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public final boolean eq0()
135     {
136         return this.getSI() == 0.0;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final boolean ne0()
142     {
143         return this.getSI() != 0.0;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public final int compareTo(final S o)
149     {
150         return Float.compare(this.getSI(), o.getSI());
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public int intValue()
156     {
157         return (int) this.getSI();
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public long longValue()
163     {
164         return (long) this.getSI();
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public float floatValue()
170     {
171         return this.getSI();
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public double doubleValue()
177     {
178         return this.getSI();
179     }
180 
181     /**********************************************************************************/
182     /********************************* GENERIC METHODS ********************************/
183     /**********************************************************************************/
184 
185     /** {@inheritDoc} */
186     @Override
187     public String toString()
188     {
189         return toString(getDisplayUnit(), false, true);
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public String toString(final U displayUnit)
195     {
196         return toString(displayUnit, false, true);
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public String toString(final boolean verbose, final boolean withUnit)
202     {
203         return toString(getDisplayUnit(), verbose, withUnit);
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     public String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
209     {
210         StringBuffer buf = new StringBuffer();
211         if (verbose)
212         {
213             buf.append(this instanceof Absolute ? "Abs " : "Rel ");
214         }
215         float d = (float) ValueUtil.expressAsUnit(getSI(), displayUnit);
216         buf.append(Format.format(d));
217         if (withUnit)
218         {
219             buf.append(" "); // Insert one space as prescribed by SI writing conventions
220             buf.append(displayUnit.getDefaultDisplayAbbreviation());
221         }
222         return buf.toString();
223     }
224 
225     /** {@inheritDoc} */
226     @Override
227     public String toTextualString()
228     {
229         return toTextualString(getDisplayUnit());
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     public String toTextualString(final U displayUnit)
235     {
236         float f = (float) ValueUtil.expressAsUnit(getSI(), displayUnit);
237         return f + " " + displayUnit.getDefaultTextualAbbreviation();
238     }
239 
240     /** {@inheritDoc} */
241     @Override
242     public String toDisplayString()
243     {
244         return toDisplayString(getDisplayUnit());
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public String toDisplayString(final U displayUnit)
250     {
251         float f = (float) ValueUtil.expressAsUnit(getSI(), displayUnit);
252         return f + " " + displayUnit.getDefaultDisplayAbbreviation();
253     }
254 
255     /** {@inheritDoc} */
256     @Override
257     @SuppressWarnings("checkstyle:designforextension")
258     public int hashCode()
259     {
260         final int prime = 31;
261         int result = getDisplayUnit().getStandardUnit().hashCode();
262         long temp;
263         temp = Float.floatToIntBits(this.getSI());
264         result = prime * result + (int) (temp ^ (temp >>> 32));
265         return result;
266     }
267 
268     /** {@inheritDoc} */
269     @Override
270     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces", "unchecked"})
271     public boolean equals(final Object obj)
272     {
273         if (this == obj)
274             return true;
275         if (obj == null)
276             return false;
277         if (getClass() != obj.getClass())
278             return false;
279         AbstractFloatScalar<U, S> other = (AbstractFloatScalar<U, S>) obj;
280         if (!getDisplayUnit().getStandardUnit().equals(other.getDisplayUnit().getStandardUnit()))
281             return false;
282         if (Float.floatToIntBits(this.getSI()) != Float.floatToIntBits(other.getSI()))
283             return false;
284         return true;
285     }
286 
287 }