001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2020, Units of Measurement project. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tech.units.indriya.format; 031 032import static tech.units.indriya.format.FormatBehavior.LOCALE_NEUTRAL; 033import static tech.units.indriya.format.CommonFormatter.parseCompoundAsLeading; 034import static tech.units.indriya.format.CommonFormatter.parseCompoundAsPrimary; 035 036import java.io.IOException; 037import java.text.NumberFormat; 038import java.text.ParsePosition; 039import java.util.Locale; 040import javax.measure.Quantity; 041import javax.measure.Unit; 042import javax.measure.format.MeasurementParseException; 043import javax.measure.format.UnitFormat; 044 045import tech.units.indriya.AbstractUnit; 046import tech.units.indriya.quantity.CompoundQuantity; 047import tech.units.indriya.quantity.Quantities; 048 049/** 050 * An implementation of {@link javax.measure.format.QuantityFormat QuantityFormat} combining {@linkplain NumberFormat} and {@link UnitFormat} 051 * separated by a delimiter. 052 * 053 * @author <a href="mailto:werner@units.tech">Werner Keil</a> 054 * @author <a href="mailto:thodoris.bais@gmail.com">Thodoris Bais</a> 055 * 056 * @version 1.9, $Date: 2019-04-14 $ 057 * @since 2.0 058 */ 059@SuppressWarnings({ "rawtypes", "unchecked" }) 060public class NumberDelimiterQuantityFormat extends AbstractQuantityFormat { 061 062 /** 063 * Holds the default format instance (SimpleUnitFormat). 064 */ 065 private static final NumberDelimiterQuantityFormat SIMPLE = new NumberDelimiterQuantityFormat.Builder() 066 .setNumberFormat(NumberFormat.getInstance(Locale.ROOT)).setUnitFormat(SimpleUnitFormat.getInstance()).build(); 067 068 /** 069 * Holds the localized format instance. 070 */ 071 private static final NumberDelimiterQuantityFormat LOCAL = new NumberDelimiterQuantityFormat.Builder() 072 .setNumberFormat(NumberFormat.getInstance()) 073 .setUnitFormat(LocalUnitFormat.getInstance()) 074 .setLocaleSensitive(true).build(); 075 076 /** 077 * 078 */ 079 private static final long serialVersionUID = 3546952599885869402L; 080 081 private transient NumberFormat numberFormat; 082 private transient UnitFormat unitFormat; 083 private transient Unit primaryUnit; 084 private String delimiter; 085 private String mixDelimiter; 086 private boolean localeSensitive; 087 088 private NumberDelimiterQuantityFormat() { 089 /* private constructor */ } 090 091 /** 092 * A fluent Builder to easily create new instances of <code>NumberDelimiterQuantityFormat</code>. 093 */ 094 public static class Builder { 095 096 private transient NumberFormat numberFormat; 097 private transient UnitFormat unitFormat; 098 private transient Unit primaryUnit; 099 private transient String delimiter = DEFAULT_DELIMITER; 100 private transient String mixedRadixDelimiter; 101 private boolean localeSensitive; 102 103 /** 104 * Sets the numberFormat parameter to the given {@code NumberFormat}. 105 * @param numberFormat the {@link NumberFormat} 106 * @throws NullPointerException if {@code numberFormat} is {@code null} 107 * @return this {@code NumberDelimiterQuantityFormat.Builder} 108 */ 109 public Builder setNumberFormat(NumberFormat numberFormat) { 110 if (numberFormat == null) { 111 throw new NullPointerException(); 112 } 113 this.numberFormat = numberFormat; 114 return this; 115 } 116 117 /** 118 * Sets the unitFormat parameter to the given {@code UnitFormat}. 119 * @param numberFormat the {@link UnitFormat} 120 * @throws NullPointerException if {@code unitFormat} is {@code null} 121 * @return this {@code NumberDelimiterQuantityFormat.Builder} 122 */ 123 public Builder setUnitFormat(UnitFormat unitFormat) { 124 if (unitFormat == null) { 125 throw new NullPointerException(); 126 } 127 this.unitFormat = unitFormat; 128 return this; 129 } 130 131 /** 132 * Sets the primary unit parameter for multiple {@link CompoundQuantity mixed quantities} to the given {@code Unit}. 133 * @param primary the primary {@link Unit} 134 * @throws NullPointerException if {@code primary} is {@code null} 135 * @return this {@code NumberDelimiterQuantityFormat.Builder} 136 */ 137 public Builder setPrimaryUnit(final Unit primary) { 138 if (unitFormat == null) { 139 throw new NullPointerException(); 140 } 141 this.primaryUnit = primary; 142 return this; 143 } 144 145 /** 146 * Sets the delimiter between a {@code NumberFormat} and {@code UnitFormat}. 147 * @param delimiter the delimiter to use 148 * @throws NullPointerException if {@code delimiter} is {@code null} 149 * @return this {@code NumberDelimiterQuantityFormat.Builder} 150 */ 151 public Builder setDelimiter(String delimiter) { 152 if (delimiter == null) { 153 throw new NullPointerException(); 154 } 155 this.delimiter = delimiter; 156 return this; 157 } 158 159 /** 160 * Sets the radix delimiter between multiple {@link CompoundQuantity mixed quantities}. 161 * @param radixPartsDelimiter the delimiter to use 162 * @throws NullPointerException if {@code radixPartsDelimiter} is {@code null} 163 * @return this {@code NumberDelimiterQuantityFormat.Builder} 164 */ 165 public Builder setRadixPartsDelimiter(String radixPartsDelimiter) { 166 if (radixPartsDelimiter == null) { 167 throw new NullPointerException(); 168 } 169 this.mixedRadixDelimiter = radixPartsDelimiter; 170 return this; 171 } 172 173 /** 174 * Sets the {@code localeSensitive} flag. 175 * @param localeSensitive the flag, if the {@code NumberDelimiterQuantityFormat} to be built will depend on a {@code Locale} to perform its tasks. 176 * @return this {@code NumberDelimiterQuantityFormat.Builder} 177 * @see UnitFormat#isLocaleSensitive() 178 */ 179 public Builder setLocaleSensitive(boolean localeSensitive) { 180 this.localeSensitive = localeSensitive; 181 return this; 182 } 183 184 public NumberDelimiterQuantityFormat build() { 185 NumberDelimiterQuantityFormat quantityFormat = new NumberDelimiterQuantityFormat(); 186 quantityFormat.numberFormat = this.numberFormat; 187 quantityFormat.unitFormat = this.unitFormat; 188 quantityFormat.primaryUnit = this.primaryUnit; 189 quantityFormat.delimiter = this.delimiter; 190 quantityFormat.mixDelimiter = this.mixedRadixDelimiter; 191 quantityFormat.localeSensitive = this.localeSensitive; 192 return quantityFormat; 193 } 194 } 195 196 /** 197 * Returns an instance of {@link NumberDelimiterQuantityFormat} with a particular {@link FormatBehavior}, either locale-sensitive or locale-neutral. 198 * For example: <code>NumberDelimiterQuantityFormat.getInstance(LOCALE_NEUTRAL))</code> returns<br> 199 * <code>new NumberDelimiterQuantityFormat.Builder() 200 .setNumberFormat(NumberFormat.getInstance(Locale.ROOT)).setUnitFormat(SimpleUnitFormat.getInstance()).build();</code> 201 * 202 * @param style 203 * the format behavior to apply. 204 * @return <code>NumberDelimiterQuantityFormat.getInstance(NumberFormat.getInstance(), UnitFormat.getInstance())</code> 205 */ 206 public static NumberDelimiterQuantityFormat getInstance(FormatBehavior style) { 207 switch (style) { 208 case LOCALE_NEUTRAL: 209 return SIMPLE; 210 case LOCALE_SENSITIVE: 211 return LOCAL; 212 default: 213 return SIMPLE; 214 } 215 } 216 217 /** 218 * Returns a new instance of {@link Builder}. 219 * 220 * @return a new {@link Builder}. 221 */ 222 public static final Builder builder() { 223 return new Builder(); 224 } 225 226 /** 227 * Returns the default format. 228 * 229 * @return the desired format. 230 */ 231 public static NumberDelimiterQuantityFormat getInstance() { 232 return getInstance(LOCALE_NEUTRAL); 233 } 234 235 /** 236 * Returns the quantity format using the specified number format and unit format (the number and unit are separated by one space). 237 * 238 * @param numberFormat 239 * the number format. 240 * @param unitFormat 241 * the unit format. 242 * @return the corresponding format. 243 */ 244 public static NumberDelimiterQuantityFormat getInstance(NumberFormat numberFormat, UnitFormat unitFormat) { 245 return new NumberDelimiterQuantityFormat.Builder().setNumberFormat(numberFormat).setUnitFormat(unitFormat).build(); 246 } 247 248 @Override 249 public Appendable format(Quantity<?> quantity, Appendable dest) throws IOException { 250 int fract = 0; 251 /* 252 if (quantity instanceof MixedQuantity) { 253 final MixedQuantity<?> compQuant = (MixedQuantity<?>) quantity; 254 if (compQuant.getUnit() instanceof MixedUnit) { 255 final MixedUnit<?> compUnit = (MixedUnit<?>) compQuant.getUnit(); 256 final Number[] values = compQuant.getValues(); 257 if (values.length == compUnit.getUnits().size()) { 258 final StringBuffer sb = new StringBuffer(); // we use StringBuffer here because of java.text.Format compatibility 259 for (int i = 0; i < values.length; i++) { 260 if (values[i] != null) { 261 fract = getFractionDigitsCount(values[i].doubleValue()); 262 } else { 263 fract = 0; 264 } 265 if (fract > 1) { 266 numberFormat.setMaximumFractionDigits(fract + 1); 267 } 268 sb.append(numberFormat.format(values[i])); 269 sb.append(delimiter); 270 sb.append(unitFormat.format(compUnit.getUnits().get(i))); 271 if (i < values.length - 1) { 272 sb.append((mixDelimiter != null ? mixDelimiter : DEFAULT_DELIMITER)); // we need null for parsing but not 273 // formatting 274 } 275 } 276 return sb; 277 } else { 278 throw new IllegalArgumentException( 279 String.format("%s values don't match %s in mixed unit", values.length, compUnit.getUnits().size())); 280 } 281 } else { 282 throw new MeasurementException("A mixed quantity must contain a mixed unit"); 283 } 284 } else { 285 */ 286 if (quantity != null && quantity.getValue() != null) { 287 fract = getFractionDigitsCount(quantity.getValue().doubleValue()); 288 } 289 if (fract > 1) { 290 numberFormat.setMaximumFractionDigits(fract + 1); 291 } 292 dest.append(numberFormat.format(quantity.getValue())); 293 if (quantity.getUnit().equals(AbstractUnit.ONE)) 294 return dest; 295 dest.append(delimiter); 296 return unitFormat.format(quantity.getUnit(), dest); 297 //} 298 } 299 300 @Override 301 public Quantity<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException, MeasurementParseException { 302 final String str = csq.toString(); 303 final int index = cursor.getIndex(); 304 if (mixDelimiter != null && !mixDelimiter.equals(delimiter)) { 305 if (primaryUnit != null) { 306 return parseCompoundAsPrimary(str, numberFormat, unitFormat, primaryUnit, delimiter, mixDelimiter, index); 307 } else { 308 return parseCompoundAsLeading(str, numberFormat, unitFormat, delimiter, mixDelimiter, index); 309 } 310 } else if (mixDelimiter != null && mixDelimiter.equals(delimiter)) { 311 if (primaryUnit != null) { 312 return parseCompoundAsPrimary(str, numberFormat, unitFormat, primaryUnit, delimiter, index); 313 } else { 314 return parseCompoundAsLeading(str, numberFormat, unitFormat, delimiter, index); 315 } 316 } 317 final Number number = numberFormat.parse(str, cursor); 318 if (number == null) 319 throw new IllegalArgumentException("Number cannot be parsed"); 320 final String[] parts = str.substring(index).split(delimiter); 321 if (parts.length < 2) { 322 throw new IllegalArgumentException("No Unit found"); 323 } 324 final Unit unit = unitFormat.parse(parts[1]); 325 return Quantities.getQuantity(number, unit); 326 } 327 328 @Override 329 protected Quantity<?> parse(CharSequence csq, int index) throws IllegalArgumentException, MeasurementParseException { 330 return parse(csq, new ParsePosition(index)); 331 } 332 333 @Override 334 public Quantity<?> parse(CharSequence csq) throws IllegalArgumentException, MeasurementParseException { 335 return parse(csq, 0); 336 } 337 338 @Override 339 public String toString() { 340 return getClass().getSimpleName(); 341 } 342 343 @Override 344 public boolean isLocaleSensitive() { 345 return localeSensitive; 346 } 347 348 @Override 349 protected StringBuffer formatCompound(CompoundQuantity<?> comp, StringBuffer dest) { 350 final StringBuffer sb = new StringBuffer(); 351 int i = 0; 352 for (Quantity<?> q : comp.getQuantities()) { 353 sb.append(format(q)); 354 if (i < comp.getQuantities().size() - 1 ) { 355 sb.append((mixDelimiter != null ? mixDelimiter : DEFAULT_DELIMITER)); // we need null for parsing but not 356 } 357 i++; 358 } 359 return sb; 360 } 361 362 public CompoundQuantity<?> parseCompound(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException, MeasurementParseException { 363 final String str = csq.toString(); 364 final int index = cursor.getIndex(); 365 if (mixDelimiter != null && !mixDelimiter.equals(delimiter)) { 366 return CommonFormatter.parseCompound(str, numberFormat, unitFormat, delimiter, mixDelimiter, index); 367 } else if (mixDelimiter != null && mixDelimiter.equals(delimiter)) { 368 return CommonFormatter.parseCompound(str, numberFormat, unitFormat, delimiter, index); 369 } 370 final Number number = numberFormat.parse(str, cursor); 371 if (number == null) 372 throw new IllegalArgumentException("Number cannot be parsed"); 373 final String[] parts = str.substring(index).split(delimiter); 374 if (parts.length < 2) { 375 throw new IllegalArgumentException("No Unit found"); 376 } 377 final Unit unit = unitFormat.parse(parts[1]); 378 return CompoundQuantity.of(Quantities.getQuantity(number, unit)); 379 } 380 381 protected CompoundQuantity<?> parseCompound(CharSequence csq, int index) throws IllegalArgumentException, MeasurementParseException { 382 return parseCompound(csq, new ParsePosition(index)); 383 } 384 385 public CompoundQuantity<?> parseCompound(CharSequence csq) throws IllegalArgumentException, MeasurementParseException { 386 return parseCompound(csq, 0); 387 } 388 389 // Private helper methods 390 391 private static int getFractionDigitsCount(double d) { 392 if (d >= 1) { // we only need the fraction digits 393 d = d - (long) d; 394 } 395 if (d == 0) { // nothing to count 396 return 0; 397 } 398 d *= 10; // shifts 1 digit to left 399 int count = 1; 400 while (d - (long) d != 0) { // keeps shifting until there are no more 401 // fractions 402 d *= 10; 403 count++; 404 } 405 return count; 406 } 407 408}