1 package org.djunits.value;
2
3 import java.io.Serializable;
4 import java.util.regex.Pattern;
5
6 import org.djunits.unit.Unit;
7
8 /**
9 * Basics of the Scalar type
10 * <p>
11 * This file was generated by the djunits value classes generator, 26 jun, 2015
12 * <p>
13 * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
15 * <p>
16 * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
17 * initial version 26 jun, 2015 <br>
18 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20 * @param <U> the unit of the values in the constructor and for display
21 */
22 public abstract class Scalar<U extends Unit<U>> extends Number implements Value<U>, Serializable
23 {
24 /** */
25 private static final long serialVersionUID = 20150626L;
26
27 /** The display unit of the Scalar. */
28 private U unit;
29
30 /** Number pattern regex to be used in valueOf() method. */
31 protected static final Pattern NUMBER_PATTERN;
32
33 /** Compile number pattern regex to be used in valueOf() method of derived classes. */
34 static
35 {
36 String regex = "[+-]?\\d+\\.?\\d*([Ee][+-]?\\d+)?";
37 NUMBER_PATTERN = Pattern.compile(regex);
38 }
39
40 /**
41 * Construct a new Scalar.
42 * @param unit U; the unit of the new Scalar
43 */
44 public Scalar(final U unit)
45 {
46 this.unit = unit;
47 }
48
49 /** {@inheritDoc} */
50 @Override
51 public final U getUnit()
52 {
53 return this.unit;
54 }
55
56 /** {@inheritDoc} */
57 @Override
58 public final double expressAsSIUnit(final double value)
59 {
60 if (this.unit.isBaseSIUnit())
61 {
62 return value;
63 }
64 return ValueUtil.expressAsSIUnit(value, this.unit);
65 }
66
67 /**
68 * Convert a value from the standard SI unit into the unit of this Scalar.
69 * @param value double; the value to convert
70 * @return double; the value in the unit of this Scalar
71 */
72 protected final double expressAsSpecifiedUnit(final double value)
73 {
74 if (this.unit.isBaseSIUnit())
75 {
76 return value;
77 }
78 return ValueUtil.expressAsUnit(value, this.unit);
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public void setDisplayUnit(final U newUnit)
84 {
85 this.unit = newUnit;
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public final boolean isAbsolute()
91 {
92 return this instanceof Absolute;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public final boolean isRelative()
98 {
99 return this instanceof Relative;
100 }
101
102 /**
103 * Returns a String representation of the scalar value that can be parsed back into its scalar type. The String
104 * representation is the double value in the unit, followed by the official abbreviation of the unit without spaces, in the
105 * current locale.
106 * @param value Scalar<U>; the value to parse into a String
107 * @param <U> the unit type for the scalar
108 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
109 */
110 public static <U extends Unit<U>> String stringOf(final Scalar<U> value)
111 {
112 return value.expressAsSpecifiedUnit(value.doubleValue()) + value.getUnit().getAbbreviation();
113 }
114
115 /**
116 * Returns a String representation of the scalar value that can be parsed back into its scalar type. The String
117 * representation is the double value in the unit, followed by the official abbreviation of the unit without spaces, in the
118 * English locale.
119 * @param value Scalar<U>; the value to parse into a String
120 * @param <U> the unit type for the scalar
121 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
122 */
123 public static <U extends Unit<U>> String stringOfDefaultLocale(final Scalar<U> value)
124 {
125 return value.expressAsSpecifiedUnit(value.doubleValue()) + value.getUnit().getDefaultLocaleAbbreviation();
126 }
127
128 /**
129 * Returns a String representation of the scalar value that can be parsed back into its scalar type. The String
130 * representation is the double value in the unit, followed by the default textual abbreviation of the unit without spaces,
131 * in the current locale.
132 * @param value Scalar<U>; the value to parse into a String
133 * @param <U> the unit type for the scalar
134 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
135 */
136 public static <U extends Unit<U>> String textualStringOf(final Scalar<U> value)
137 {
138 return value.expressAsSpecifiedUnit(value.doubleValue()) + value.getUnit().getDefaultTextualRepresentation();
139 }
140
141 /**
142 * Returns a String representation of the scalar value that can be parsed back into its scalar type. The String
143 * representation is the double value in the unit, followed by the default textual abbreviation of the unit without spaces,
144 * in the English locale.
145 * @param value Scalar<U>; the value to parse into a String
146 * @param <U> the unit type for the scalar
147 * @return the String representation of the value in its unit, followed by the official abbreviation of the unit
148 */
149 public static <U extends Unit<U>> String textualStringOfDefaultLocale(final Scalar<U> value)
150 {
151 return value.expressAsSpecifiedUnit(value.doubleValue()) + value.getUnit().getDefaultLocaleTextualRepresentation();
152 }
153
154 // No hashcode or equals -- has to be implemented on a deeper level
155 }