View Javadoc
1   package org.djunits.value.vdouble.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 DoubleScalar.
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 AbstractDoubleScalar<U extends Unit<U>, S extends AbstractDoubleScalar<U, S>> extends AbstractScalar<U, S>
21          implements DoubleScalarInterface<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 double si;
29  
30      /**
31       * Construct a new AbstractDoubleScalar.
32       * @param unit U; the unit
33       * @param si double; the si value to store
34       */
35      AbstractDoubleScalar(final U unit, final double si)
36      {
37          super(unit);
38          this.si = si;
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public final double getSI()
44      {
45          return this.si;
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public final double getInUnit()
51      {
52          return ValueUtil.expressAsUnit(getSI(), getDisplayUnit());
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public final double getInUnit(final U targetUnit)
58      {
59          return 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 Double.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 (float) 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     protected Object clone() throws CloneNotSupportedException
188     {
189         return super.clone();
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public final String toString()
195     {
196         return toString(getDisplayUnit(), false, true);
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public final String toString(final U displayUnit)
202     {
203         return toString(displayUnit, false, true);
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     public final String toString(final boolean verbose, final boolean withUnit)
209     {
210         return toString(getDisplayUnit(), verbose, withUnit);
211     }
212 
213     /** {@inheritDoc} */
214     @Override
215     public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
216     {
217         StringBuffer buf = new StringBuffer();
218         if (verbose)
219         {
220             buf.append(this instanceof Absolute ? "Abs " : "Rel ");
221         }
222         double d = ValueUtil.expressAsUnit(getSI(), displayUnit);
223         buf.append(Format.format(d));
224         if (withUnit)
225         {
226             buf.append(" "); // Insert one space as prescribed by SI writing conventions
227             buf.append(displayUnit.getDefaultDisplayAbbreviation());
228         }
229         return buf.toString();
230     }
231 
232     /** {@inheritDoc} */
233     @Override
234     @SuppressWarnings("checkstyle:designforextension")
235     public int hashCode()
236     {
237         final int prime = 31;
238         int result = getDisplayUnit().getStandardUnit().hashCode();
239         long temp;
240         temp = Double.doubleToLongBits(this.getSI());
241         result = prime * result + (int) (temp ^ (temp >>> 32));
242         return result;
243     }
244 
245     /** {@inheritDoc} */
246     @Override
247     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces", "unchecked"})
248     public boolean equals(final Object obj)
249     {
250         if (this == obj)
251             return true;
252         if (obj == null)
253             return false;
254         if (getClass() != obj.getClass())
255             return false;
256         AbstractDoubleScalar<U, S> other = (AbstractDoubleScalar<U, S>) obj;
257         if (!getDisplayUnit().getStandardUnit().equals(other.getDisplayUnit().getStandardUnit()))
258             return false;
259         if (Double.doubleToLongBits(this.getSI()) != Double.doubleToLongBits(other.getSI()))
260             return false;
261         return true;
262     }
263 
264 }