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 /** {@inheritDoc} */ 36 @Override 37 public final U getDisplayUnit() 38 { 39 return this.displayUnit; 40 } 41 42 /** {@inheritDoc} */ 43 @Override 44 public void setDisplayUnit(final U newUnit) 45 { 46 Throw.whenNull(newUnit, "newUnit may not be null"); 47 this.displayUnit = newUnit; 48 } 49 50 // No hashcode or equals -- has to be implemented on a deeper level 51 52 /** 53 * Test if this DoubleScalar is less than another DoubleScalar. 54 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 55 * @return boolean; true if this is less than o; false otherwise 56 */ 57 public abstract boolean lt(S o); 58 59 /** 60 * Test if this DoubleScalar is less than or equal to another DoubleScalar. 61 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 62 * @return boolean; true if this is less than or equal to o; false otherwise 63 */ 64 public abstract boolean le(S o); 65 66 /** 67 * Test if this DoubleScalar is greater than another DoubleScalar. 68 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 69 * @return boolean; true if this is greater than o; false otherwise 70 */ 71 public abstract boolean gt(S o); 72 73 /** 74 * Test if this DoubleScalar is greater than or equal to another DoubleScalar. 75 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 76 * @return boolean; true if this is greater than or equal to o; false otherwise 77 */ 78 public abstract boolean ge(S o); 79 80 /** 81 * Test if this DoubleScalar is equal to another DoubleScalar. 82 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 83 * @return boolean; true if this is equal to o; false otherwise 84 */ 85 public abstract boolean eq(S o); 86 87 /** 88 * Test if this DoubleScalar is not equal to another DoubleScalar. 89 * @param o T, a relative typed DoubleScalar; the right hand side operand of the comparison 90 * @return boolean; true if this is not equal to o; false otherwise 91 */ 92 public abstract boolean ne(S o); 93 94 /** 95 * Test if this DoubleScalar is less than 0.0. 96 * @return boolean; true if this is less than 0.0; false if this is not less than 0.0 97 */ 98 public abstract boolean lt0(); 99 100 /** 101 * Test if this DoubleScalar is less than or equal to 0.0. 102 * @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 103 */ 104 public abstract boolean le0(); 105 106 /** 107 * Test if this DoubleScalar is greater than 0.0. 108 * @return boolean; true if this is greater than 0.0; false if this is not greater than 0.0 109 */ 110 public abstract boolean gt0(); 111 112 /** 113 * Test if this DoubleScalar is greater than or equal to 0.0. 114 * @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 115 */ 116 public abstract boolean ge0(); 117 118 /** 119 * Test if this DoubleScalar is equal to 0.0. 120 * @return boolean; true if this is equal to 0.0; false if this is not equal to 0.0 121 */ 122 public abstract boolean eq0(); 123 124 /** 125 * Test if this DoubleScalar is not equal to 0.0. 126 * @return boolean; true if this is not equal to 0.0; false if this is equal to 0.0 127 */ 128 public abstract boolean ne0(); 129 130 /** 131 * Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is 132 * added between the number and the unit. 133 * @return a String with the value with the default textual representation of the unit attached. 134 */ 135 public abstract String toTextualString(); 136 137 /** 138 * Concise textual representation of this value, without the engineering formatting, so without trailing zeroes. A space is 139 * added between the number and the unit. 140 * @param displayUnit U; the display unit for the value 141 * @return a String with the value with the default textual representation of the provided unit attached. 142 */ 143 public abstract String toTextualString(U displayUnit); 144 145 /** 146 * Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is 147 * added between the number and the unit. 148 * @return a String with the value with the default display representation of the unit attached. 149 */ 150 public abstract String toDisplayString(); 151 152 /** 153 * Concise display description of this value, without the engineering formatting, so without trailing zeroes. A space is 154 * added between the number and the unit. 155 * @param displayUnit U; the display unit for the value 156 * @return a String with the value with the default display representation of the provided unit attached. 157 */ 158 public abstract String toDisplayString(U displayUnit); 159 160 /** 161 * Format a string according to the current locale and the standard (minimized) format, such as "3.14" or "300.0". 162 * @param d double; the number to format 163 * @return String; the formatted number using the current Locale 164 */ 165 public String format(final double d) 166 { 167 if (d < 1E-5 || d > 1E5) 168 return format(d, "%E"); 169 return format(d, "%f"); 170 } 171 172 /** 173 * Format a string according to the current locale and the provided format string. 174 * @param d double; the number to format 175 * @param format String; the formatting string to use for the number 176 * @return String; the formatted number using the current Locale and the format string 177 */ 178 public String format(final double d, final String format) 179 { 180 String s = String.format(format, d); 181 if (s.contains("e") || s.contains("E")) 182 return s; 183 while (s.endsWith("0") && s.length() > 2) 184 s = s.substring(0, s.length() - 1); 185 String last = s.substring(s.length() - 1); 186 if (!"01234567890".contains(last)) 187 s += "0"; 188 return s; 189 } 190 191 }