View Javadoc
1   package org.djunits.value.base;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.value.Value;
5   import org.djutils.exceptions.Throw;
6   
7   /**
8    * Scalar to distinguish a scalar from vectors and matrices.
9    * <p>
10   * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
12   * </p>
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
14   * @param <U> the unit
15   * @param <S> the scalar type with the given unit
16   */
17  public abstract class Scalar<U extends Unit<U>, S extends Scalar<U, S>> extends Number implements Value<U, S>, Comparable<S>
18  {
19      /**  */
20      private static final long serialVersionUID = 20150626L;
21  
22      /** The display unit of this AbstractScalar. */
23      private U displayUnit;
24  
25      /**
26       * Construct a new Scalar.
27       * @param displayUnit U; the unit of the new AbstractScalar
28       */
29      protected Scalar(final U displayUnit)
30      {
31          Throw.whenNull(displayUnit, "display unit cannot be null");
32          this.displayUnit = displayUnit;
33      }
34  
35      @Override
36      public final U getDisplayUnit()
37      {
38          return this.displayUnit;
39      }
40  
41      @Override
42      public void setDisplayUnit(final U newUnit)
43      {
44          Throw.whenNull(newUnit, "newUnit may not be null");
45          this.displayUnit = newUnit;
46      }
47  
48      // No hashcode or equals -- has to be implemented on a deeper level
49  
50      /**
51       * Test if this DoubleScalar is less than another DoubleScalar.
52       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
53       * @return boolean; true if this is less than o; false otherwise
54       */
55      public abstract boolean lt(S o);
56  
57      /**
58       * Test if this DoubleScalar is less than or equal to another DoubleScalar.
59       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
60       * @return boolean; true if this is less than or equal to o; false otherwise
61       */
62      public abstract boolean le(S o);
63  
64      /**
65       * Test if this DoubleScalar is greater than another DoubleScalar.
66       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
67       * @return boolean; true if this is greater than o; false otherwise
68       */
69      public abstract boolean gt(S o);
70  
71      /**
72       * Test if this DoubleScalar is greater than or equal to another DoubleScalar.
73       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
74       * @return boolean; true if this is greater than or equal to o; false otherwise
75       */
76      public abstract boolean ge(S o);
77  
78      /**
79       * Test if this DoubleScalar is equal to another DoubleScalar.
80       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
81       * @return boolean; true if this is equal to o; false otherwise
82       */
83      public abstract boolean eq(S o);
84  
85      /**
86       * Test if this DoubleScalar is not equal to another DoubleScalar.
87       * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison
88       * @return boolean; true if this is not equal to o; false otherwise
89       */
90      public abstract boolean ne(S o);
91  
92      /**
93       * Test if this DoubleScalar is less than 0.0.
94       * @return boolean; true if this is less than 0.0; false if this is not less than 0.0
95       */
96      public abstract boolean lt0();
97  
98      /**
99       * Test if this DoubleScalar is less than or equal to 0.0.
100      * @return boolean; true if this is less than or equal to 0.0; false if this is not less than or equal to 0.0
101      */
102     public abstract boolean le0();
103 
104     /**
105      * Test if this DoubleScalar is greater than 0.0.
106      * @return boolean; true if this is greater than 0.0; false if this is not greater than 0.0
107      */
108     public abstract boolean gt0();
109 
110     /**
111      * Test if this DoubleScalar is greater than or equal to 0.0.
112      * @return boolean; true if this is greater than or equal to 0.0; false if this is not greater than or equal to 0.0
113      */
114     public abstract boolean ge0();
115 
116     /**
117      * Test if this DoubleScalar is equal to 0.0.
118      * @return boolean; true if this is equal to 0.0; false if this is not equal to 0.0
119      */
120     public abstract boolean eq0();
121 
122     /**
123      * Test if this DoubleScalar is not equal to 0.0.
124      * @return boolean; true if this is not equal to 0.0; false if this is equal to 0.0
125      */
126     public abstract boolean ne0();
127 
128     /**
129      * Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is
130      * added between the number and the unit.
131      * @return a String with the value with the default textual representation of the unit attached.
132      */
133     public abstract String toTextualString();
134 
135     /**
136      * Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is
137      * added between the number and the unit.
138      * @param displayUnit U; the display unit for the value
139      * @return a String with the value with the default textual representation of the provided unit attached.
140      */
141     public abstract String toTextualString(U displayUnit);
142 
143     /**
144      * Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is
145      * added between the number and the unit.
146      * @return a String with the value with the default display representation of the unit attached.
147      */
148     public abstract String toDisplayString();
149 
150     /**
151      * Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is
152      * added between the number and the unit.
153      * @param displayUnit U; the display unit for the value
154      * @return a String with the value with the default display representation of the provided unit attached.
155      */
156     public abstract String toDisplayString(U displayUnit);
157 
158     /**
159      * Format a string according to the current locale and the standard (minimized) format, such as "3.14" or "300.0".
160      * @param d double; the number to format
161      * @return String; the formatted number using the current Locale
162      */
163     public String format(final double d)
164     {
165         if (d < 1E-5 || d > 1E5)
166             return format(d, "%E");
167         return format(d, "%f");
168     }
169 
170     /**
171      * Format a string according to the current locale and the provided format string.
172      * @param d double; the number to format
173      * @param format String; the formatting string to use for the number
174      * @return String; the formatted number using the current Locale and the format string
175      */
176     public String format(final double d, final String format)
177     {
178         String s = String.format(format, d);
179         if (s.contains("e") || s.contains("E"))
180             return s;
181         while (s.endsWith("0") && s.length() > 2)
182             s = s.substring(0, s.length() - 1);
183         String last = s.substring(s.length() - 1);
184         if (!"01234567890".contains(last))
185             s += "0";
186         return s;
187     }
188 
189 }