• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unitsofmeasurement / indriya / 2305

29 Mar 2025 07:22PM UTC coverage: 71.208%. Remained the same
2305

push

circleci

web-flow
Merge pull request #437 from david-nominal/patch-1

use unicode superscript instead of Square M Squared and Square M Cubed

2 of 2 new or added lines in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

3732 of 5241 relevant lines covered (71.21%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.04
/src/main/java/tech/units/indriya/format/SimpleUnitFormat.java
1
/*
2
 * Units of Measurement Reference Implementation
3
 * Copyright (c) 2005-2025, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
4
 *
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without modification,
8
 * are permitted provided that the following conditions are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright notice,
11
 *    this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
14
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
15
 *
16
 * 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
package tech.units.indriya.format;
31

32
import java.io.IOException;
33
import java.text.FieldPosition;
34
import java.text.ParsePosition;
35
import java.util.HashMap;
36
import java.util.Map;
37
import java.util.stream.Collectors;
38
import java.util.stream.Stream;
39

40
import javax.measure.BinaryPrefix;
41
import javax.measure.MeasurementError;
42
import javax.measure.MetricPrefix;
43
import javax.measure.Prefix;
44
import javax.measure.Quantity;
45
import javax.measure.Unit;
46
import javax.measure.UnitConverter;
47
import javax.measure.format.MeasurementParseException;
48
import javax.measure.format.UnitFormat;
49

50
import static javax.measure.MetricPrefix.MICRO;
51

52
import tech.units.indriya.AbstractUnit;
53
import tech.units.indriya.function.AddConverter;
54
import tech.units.indriya.function.MultiplyConverter;
55
import tech.units.indriya.function.RationalNumber;
56
import tech.units.indriya.unit.AlternateUnit;
57
import tech.units.indriya.unit.AnnotatedUnit;
58
import tech.units.indriya.unit.BaseUnit;
59
import tech.units.indriya.unit.ProductUnit;
60
import tech.units.indriya.unit.TransformedUnit;
61
import tech.units.indriya.unit.Units;
62

63
import static tech.units.indriya.format.FormatConstants.MIDDLE_DOT;
64

65
/**
66
 * <p>
67
 * This class implements the {@link UnitFormat} interface for formatting and parsing {@link Unit units}.
68
 * </p>
69
 *
70
 * <p>
71
 * For all SI units, the <b>24 SI prefixes</b> used to form decimal multiples and sub-multiples are recognized. As well as the <b>8 binary prefixes</b>.<br>
72
 * {@link Units} are directly recognized. For example:<br>
73
 * <code>
74
 *        UnitFormat format = SimpleUnitFormat.getInstance();<br>
75
 *        format.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS));<br>
76
 *        format.parse("kW").equals(MetricPrefix.KILO(Units.WATT));<br>
77
 *        format.parse("ft").equals(Units.METRE.multiply(0.3048))</code>
78
 * </p>
79
 *
80
 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
81
 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
82
 * @author Eric Russell
83
 * @author Andi Huber
84
 * @version 2.18, Jan 29, 2025
85
 * @since 1.0
86
 */
87
public abstract class SimpleUnitFormat extends AbstractUnitFormat {
88
    // private static final long serialVersionUID = 4149424034841739785L;
89

90
    /**
91
     * Flavor of this format
92
     *
93
     * @author Werner
94
     *
95
     */
96
    public static enum Flavor {
1✔
97
        /** @deprecated use DEFAULT */
98
         Default, 
1✔
99
         /** The default format flavor */
100
         DEFAULT, 
1✔
101
         /** The ASCII_INSTANCE format flavor */
102
         ASCII
1✔
103
    }
104

105
    private static final String MU = "\u03bc";
106

107
    /**
108
     * Holds the default format instance.
109
     */
110
    private static final DefaultFormat DEFAULT_INSTANCE = new DefaultFormat().init();
1✔
111

112
    /**
113
     * Holds the ASCII_INSTANCE format instance.
114
     */
115
    private static final ASCIIFormat ASCII_INSTANCE = new ASCIIFormat().init();
1✔
116

117
    /**
118
     * Returns the globally shared unit format instance (used by {@link AbstractUnit#parse(CharSequence) AbstractUnit.parse()} and
119
     * {@link AbstractUnit#toString() AbstractUnit.toString()}).
120
     *
121
     * @return the default unit format.
122
     */
123
    public static SimpleUnitFormat getInstance() {
124
        return getInstance(Flavor.DEFAULT);
1✔
125
    }
126

127
    /**
128
     * Returns the {@link SimpleUnitFormat} in the desired {@link Flavor}
129
     *
130
     * @return the instance for the given {@link Flavor}.
131
     */
132
    public static SimpleUnitFormat getInstance(Flavor flavor) {
133
        switch (flavor) {
1✔
134
        case ASCII:
135
            return SimpleUnitFormat.ASCII_INSTANCE;
1✔
136
        default:
137
            return DEFAULT_INSTANCE;
1✔
138
        }
139
    }
140

141
    /**
142
     * Similar to {@link #getInstance()}, but returns a new, non-shared unit format instance,
143
     * instead of a shared singleton instance.
144
     *
145
     * @return a new instance of the default unit format.
146
     * @see #getInstance()
147
     * @since 2.7
148
     */
149
    public static SimpleUnitFormat getNewInstance() {
150
        return getNewInstance(Flavor.DEFAULT);
1✔
151
    }
152

153
    /**
154
     * Similar to {@link #getInstance(Flavor)}, but returns a new {@link SimpleUnitFormat} instance in the desired
155
     * {@link Flavor}, instead of a shared singleton instance.
156
     *
157
     * @return a new instance for the given {@link Flavor}.
158
     * @see #getInstance(Flavor)
159
     * @since 2.7 
160
     */
161
    public static SimpleUnitFormat getNewInstance(Flavor flavor) {
162
        switch (flavor) {
1✔
163
        case ASCII:
164
            return new ASCIIFormat().init();
×
165
        default:
166
            return new DefaultFormat().init();
1✔
167
        }
168
    }
169

170
    /**
171
     * Base constructor.
172
     */
173
    protected SimpleUnitFormat() {
1✔
174
    }
1✔
175

176
    /**
177
     * Formats the specified unit.
178
     *
179
     * @param unit
180
     *          the unit to format.
181
     * @param appendable
182
     *          the appendable destination.
183
     * @throws IOException
184
     *           if an error occurs.
185
     */
186
    public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException;
187

188
    /**
189
     * Parses a sequence of character to produce a unit or a rational product of unit.
190
     *
191
     * @param csq
192
     *          the <code>CharSequence</code> to parse.
193
     * @param pos
194
     *          an object holding the parsing index and error position.
195
     * @return an {@link Unit} parsed from the character sequence.
196
     * @throws IllegalArgumentException
197
     *           if the character sequence contains an illegal syntax.
198
     */
199
    @SuppressWarnings("rawtypes")
200
    public abstract Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException;
201

202
    /**
203
     * Parses a sequence of character to produce a single unit.
204
     *
205
     * @param csq
206
     *          the <code>CharSequence</code> to parse.
207
     * @param pos
208
     *          an object holding the parsing index and error position.
209
     * @return an {@link Unit} parsed from the character sequence.
210
     * @throws IllegalArgumentException
211
     *           if the character sequence does not contain a valid unit identifier.
212
     */
213
    @SuppressWarnings("rawtypes")
214
    public abstract Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException;
215

216
    /**
217
     * Attaches a system-wide label to the specified unit. For example: <code>SimpleUnitFormat.getInstance().label(DAY.multiply(365), "year");
218
     * SimpleUnitFormat.getInstance().label(METER.multiply(0.3048), "ft");</code> If the specified label is already associated to an unit the previous
219
     * association is discarded or ignored.
220
     * <p>
221
     * If you set a different label without calling {@link #removeLabel(Unit)}), {@link #removeAlias(Unit, String)}), using the old label, or {@link #removeAliases(Unit)}) on the given unit, the old label is overwritten for <b>labeling/<b> purposes, but it remains like an <b>alias</b> (it still works for parsing). 
222
     * </p>
223
     * @param unit
224
     *          the unit being labeled.
225
     * @param label
226
     *          the new label for this unit.
227
     * @throws IllegalArgumentException
228
     *           if the label is not a {@link SimpleUnitFormat#isValidIdentifier(String)} valid identifier.
229
     */
230
    public abstract void label(Unit<?> unit, String label);
231

232
        /**
233
         * Removes the system-wide label (added by {@link #label(Unit, String)}) and all system-wide aliases (added by {@link #alias(Unit, String)}) for this unit.
234
         *
235
         * @param unit
236
         *          the unit for which label shall be removed.
237
         */
238
        public abstract void removeLabel(Unit<?> unit);
239

240
        /**
241
     * Attaches a system-wide alias to this unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize
242
     * different variants of the same unit. For example: <code> SimpleUnitFormat.getInstance().alias(METER.multiply(0.3048), "foot");
243
     * SimpleUnitFormat.getInstance().alias(METRE.multiply(0.3048), "feet"); SimpleUnitFormat.getInstance().alias(METER, "meter");
244
     * SimpleUnitFormat.getInstance().alias(METRE, "meter"); </code> If the specified alias is already associated to a unit or applied as a label, the association is
245
     * replaced by the new one.
246
     *
247
     * @param unit
248
     *          the unit being aliased.
249
     * @param alias
250
     *          the alias attached to this unit.
251
     * @throws IllegalArgumentException
252
     *           if the label is not a {@link SimpleUnitFormat#isValidIdentifier(String)} valid identifier.
253
     */
254
    public abstract void alias(Unit<?> unit, String alias);
255

256
        /**
257
         * Removes the given system-wide alias (added by {@link #alias(Unit, String)}) for this unit and keeps the label (added by {@link #label(Unit, String)})
258
         *
259
         * @param unit
260
         *          the unit for which alias shall be removed.
261
         *          
262
         * @param alias
263
         *          the alias to be removed.          
264
         */
265
        public abstract void removeAlias(Unit<?> unit, String alias);
266
    
267
        /**
268
         * Removes all system-wide aliases (added by {@link #alias(Unit, String)}) for this unit and keeps the label (added by {@link #label(Unit, String)})
269
         *
270
         * @param unit
271
         *          the unit for which aliases shall be removed.
272
         */
273
        public abstract void removeAliases(Unit<?> unit);
274

275
    /**
276
     * Indicates if the specified name can be used as unit identifier.
277
     *
278
     * @param name
279
     *          the identifier to be tested.
280
     * @return <code>true</code> if the name specified can be used as label or alias for this format;<code>false</code> otherwise.
281
     */
282
    protected abstract boolean isValidIdentifier(String name);
283

284
    /**
285
     * Formats an unit and appends the resulting text to a given string buffer (implements <code>java.text.Format</code>).
286
     *
287
     * @param unit
288
     *          the unit to format.
289
     * @param toAppendTo
290
     *          where the text is to be appended
291
     * @param pos
292
     *          the field position (not used).
293
     * @return <code>toAppendTo</code>
294
     */
295
    public final StringBuffer format(Object unit, final StringBuffer toAppendTo, FieldPosition pos) {
296
        try {
297
            final Object dest = toAppendTo;
×
298
            if (dest instanceof Appendable) {
×
299
                format((Unit<?>) unit, (Appendable) dest);
×
300
            } else { // When retroweaver is used to produce 1.4 binaries. TODO is this still relevant?
301
                format((Unit<?>) unit, new Appendable() {
×
302
                    public Appendable append(char arg0) throws IOException {
303
                        toAppendTo.append(arg0);
×
304
                        return null;
×
305
                    }
306
                    public Appendable append(CharSequence arg0) throws IOException {
307
                        toAppendTo.append(arg0);
×
308
                        return null;
×
309
                    }
310
                    public Appendable append(CharSequence arg0, int arg1, int arg2) throws IOException {
311
                        toAppendTo.append(arg0.subSequence(arg1, arg2));
×
312
                        return null;
×
313
                    }
314
                });
315
            }
316
            return toAppendTo;
×
317
        } catch (IOException e) {
×
318
            throw new MeasurementError(e); // Should never happen.
×
319
        }
320
    }
321

322
    /**
323
     * Parses the text from a string to produce an object (implements <code>java.text.Format</code>).
324
     *
325
     * @param source
326
     *          the string source, part of which should be parsed.
327
     * @param pos
328
     *          the cursor position.
329
     * @return the corresponding unit or <code>null</code> if the string cannot be parsed.
330
     */
331
    public final Unit<?> parseObject(String source, ParsePosition pos) throws MeasurementParseException {
332
        return parseProductUnit(source, pos);
1✔
333
    }
334

335
    /**
336
     * This class represents an exponent with both a power (numerator) and a root (denominator).
337
     */
338
    private static class Exponent {
339
        public final int pow;
340
        public final int root;
341

342
        public Exponent(int pow, int root) {
1✔
343
            this.pow = pow;
1✔
344
            this.root = root;
1✔
345
        }
1✔
346
    }
347

348
    /**
349
     * This class represents the default (Unicode) format.
350
     * internal class, please extend either SimpleUnitFormat or AbstractUnitFormat
351
     */
352
    static class DefaultFormat extends SimpleUnitFormat {
353

354
        // Initializes the standard unit databases.
355

356
        static final Unit<?>[] METRIC_UNITS = { Units.AMPERE, Units.BECQUEREL, Units.CANDELA, Units.COULOMB, Units.FARAD, Units.GRAY, Units.HENRY,
1✔
357
                Units.HERTZ, Units.JOULE, Units.KATAL, Units.KELVIN, Units.LUMEN, Units.LUX, Units.METRE, Units.MOLE, Units.NEWTON, Units.OHM, Units.PASCAL,
358
                Units.RADIAN, Units.SECOND, Units.SIEMENS, Units.SIEVERT, Units.STERADIAN, Units.TESLA, Units.VOLT, Units.WATT, Units.WEBER };
359

360
        static final String[] METRIC_PREFIX_SYMBOLS =
361
                Stream.of(MetricPrefix.values())
1✔
362
                .map(Prefix::getSymbol)
1✔
363
                .collect(Collectors.toList())
1✔
364
                .toArray(new String[] {});
1✔
365

366
        // TODO try to consolidate those
367
        static final UnitConverter[] METRIC_PREFIX_CONVERTERS =
368
                Stream.of(MetricPrefix.values())
1✔
369
                .map(MultiplyConverter::ofPrefix)
1✔
370
                .collect(Collectors.toList())
1✔
371
                .toArray(new UnitConverter[] {});
1✔
372

373
        static final String[] BINARY_PREFIX_SYMBOLS =
374
                Stream.of(BinaryPrefix.values())
1✔
375
                .map(Prefix::getSymbol)
1✔
376
                .collect(Collectors.toList())
1✔
377
                .toArray(new String[] {});
1✔
378

379
        static final UnitConverter[] BINARY_PREFIX_CONVERTERS =
1✔
380
                Stream.of(BinaryPrefix.values())
1✔
381
                .map(MultiplyConverter::ofPrefix)
1✔
382
                .collect(Collectors.toList())
1✔
383
                .toArray(new UnitConverter[] {});
1✔
384

385
        /**
386
         * Holds the unique symbols collection (base units or alternate units).
387
         */
388
        private final Map<String, Unit<?>> symbolToUnit = new HashMap<>();
1✔
389

390
        private static enum Token { EOF, IDENTIFIER, OPEN_PAREN, CLOSE_PAREN, EXPONENT, MULTIPLY, DIVIDE,
1✔
391
            PLUS, INTEGER, FLOAT };
1✔
392

393

394
        DefaultFormat() {
1✔
395
            // Hack, somehow µg is not found.
396
            symbolToUnit.put(MetricPrefix.MICRO.getSymbol() + "g", MICRO(Units.GRAM));
1✔
397
            symbolToUnit.put("μg", MICRO(Units.GRAM));
1✔
398
            symbolToUnit.put(MU + "g", MICRO(Units.GRAM));
1✔
399
        }
1✔
400

401
        private DefaultFormat init() {
402

403
            for (int i = 0; i < METRIC_UNITS.length; i++) {
1✔
404
                Unit<?> si = METRIC_UNITS[i];
1✔
405
                String symbol = (si instanceof BaseUnit) ? ((BaseUnit<?>) si).getSymbol() : ((AlternateUnit<?>) si).getSymbol();
1✔
406
                label(si, symbol);
1✔
407
                for (int j = 0; j < METRIC_PREFIX_SYMBOLS.length; j++) {
1✔
408
                    Unit<?> u = si.prefix(MetricPrefix.values()[j]);
1✔
409
                    label(u, METRIC_PREFIX_SYMBOLS[j] + symbol);
1✔
410
                    if ( "µ".equals(METRIC_PREFIX_SYMBOLS[j]) ) {
1✔
411
                        label(u, MU + symbol);
1✔
412
                    }
413
                } // TODO what about BINARY_PREFIX here?
414
            }
415

416
            // -- GRAM/KILOGRAM
417

418
            label(Units.GRAM, "g");
1✔
419
            for(MetricPrefix prefix : MetricPrefix.values()) {
1✔
420
                switch (prefix) {
1✔
421
                case KILO:
422
                    label(Units.KILOGRAM, "kg");
1✔
423
                    break;
1✔
424
                case MICRO:
425
                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
1✔
426
                    break;
1✔
427
                default:
428
                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
1✔
429
                    break;
430
                }
431
            }
432

433
            label(MICRO(Units.GRAM), MetricPrefix.MICRO.getSymbol() + "g");
1✔
434

435
            // Alias in ASCIIFormat for Ohm
436
            aliasWithPrefixes(Units.OHM, "Ohm");
1✔
437

438
            // Special case for DEGREE_CELSIUS.
439
            labelWithPrefixes(Units.CELSIUS, "℃");
1✔
440
            aliasWithPrefixes(Units.CELSIUS, "°C");
1✔
441
            
442
            // Additional cases and aliases
443
            label(AbstractUnit.ONE, "one");
1✔
444
            label(Units.PERCENT, "%");
1✔
445
            
446
            // https://en.wikipedia.org/wiki/Non-SI_units_mentioned_in_the_SI#Units_officially_accepted_for_use_with_the_SI
447
            // The SI prefixes can be used with several of these units, but not, for example, with the non-SI units of time.
448
            // Also see https://github.com/unitsofmeasurement/indriya/issues/433 
449
            label(Units.MINUTE, "min");
1✔
450
            label(Units.HOUR, "h");
1✔
451
            label(Units.DAY, "d");
1✔
452
            alias(Units.DAY, "day");
1✔
453
            label(Units.WEEK, "wk");
1✔
454
            alias(Units.WEEK, "week");
1✔
455
            label(Units.YEAR, "yr");
1✔
456
            alias(Units.YEAR, "y"); 
1✔
457
            alias(Units.YEAR, "year");
1✔
458
            alias(Units.YEAR, "days365");
1✔
459
            alias(Units.YEAR, "a");
1✔
460
            label(Units.MONTH, "mo");
1✔
461
            alias(Units.MONTH, "mon");
1✔
462
            alias(Units.MONTH, "month");
1✔
463
            label(Units.KILOMETRE_PER_HOUR, "km/h");
1✔
464
            labelWithPrefixes(Units.SQUARE_METRE, "m\u00B2");
1✔
465
            aliasWithPrefixes(Units.SQUARE_METRE, "m2");
1✔
466
            labelWithPrefixes(Units.CUBIC_METRE, "m\u00B3");
1✔
467
            aliasWithPrefixes(Units.CUBIC_METRE, "m3");
1✔
468
            labelWithPrefixes(Units.LITRE, "l");
1✔
469

470
            return this;
1✔
471
        }
472

473
        /**
474
         * Holds the name to unit mapping.
475
         */
476
        protected final Map<String, Unit<?>> nameToUnit = new HashMap<>();
1✔
477

478
        /**
479
         * Holds the unit to name mapping.
480
         */
481
        protected final Map<Unit<?>, String> unitToName = new HashMap<>();
1✔
482

483
        @Override
484
        public String toString() {
485
            return SimpleUnitFormat.class.getSimpleName();
1✔
486
        }
487

488
        @Override
489
        public void label(Unit<?> unit, String label) {
490
            if (!isValidIdentifier(label))
1✔
491
                throw new IllegalArgumentException("Label: " + label + " is not a valid identifier.");
×
492
            synchronized (this) {
1✔
493
                nameToUnit.put(label, unit);
1✔
494
                unitToName.put(unit, label);
1✔
495
            }
1✔
496
        }
1✔
497
        
498
            @Override
499
                public void removeLabel(Unit<?> unit) {
500
                        unitToName.remove(unit);
1✔
501
                        nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit));
1✔
502
                }
1✔
503

504
        @Override
505
        public void alias(Unit<?> unit, String alias) {
506
            if (!isValidIdentifier(alias))
1✔
507
                throw new IllegalArgumentException("Alias: " + alias + " is not a valid identifier.");
×
508
            synchronized (this) {
1✔
509
                nameToUnit.put(alias, unit);
1✔
510
            }
1✔
511
        }
1✔
512

513
        @Override
514
                public void removeAlias(Unit<?> unit, String alias) {
515
                        nameToUnit.remove(alias);
1✔
516
                }
1✔
517
        
518
                @Override
519
                public void removeAliases(Unit<?> unit) {
520
                        final String alias = unitToName.get(unit);
1✔
521
                        nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit) && !e.getKey().equals(alias));
1✔
522
                }
1✔
523

524
        @Override
525
        protected boolean isValidIdentifier(String name) {
526
            if ((name == null) || (name.length() == 0))
1✔
527
                return false;
×
528
            return isUnitIdentifierPart(name.charAt(0));
1✔
529
        }
530
        
531
        /**
532
         * Applies {@link #label(Unit, String)} for this unit and all standard prefixes.
533
         * 
534
         * @param unit a unit
535
         * @param label a label
536
         */
537
        private void labelWithPrefixes(Unit<?> unit, String label) {
538
                label(unit, label);
1✔
539
                // TODO try to optimize this
540
            for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1✔
541
                    label(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + label);
1✔
542
            }
543
            for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1✔
544
                    label(unit.prefix(BinaryPrefix.values()[i]), BINARY_PREFIX_SYMBOLS[i] + label);
1✔
545
            }
546
        }
1✔
547
        
548
        /**
549
         * Applies {@link #alias(Unit, String)} for this unit and all standard prefixes.
550
         * 
551
         * @param unit a unit
552
         * @param alias an alias
553
         */
554
        private void aliasWithPrefixes(Unit<?> unit, String alias) {
555
                alias(unit, alias);
1✔
556
                // TODO try to optimize this
557
            for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1✔
558
                alias(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + alias);
1✔
559
            }
560
            for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1✔
561
                    alias(unit.prefix(BinaryPrefix.values()[i]), BINARY_PREFIX_SYMBOLS[i] + alias);
1✔
562
            }
563

564
        }
1✔
565

566
        protected static boolean isUnitIdentifierPart(char ch) {
567
            return Character.isLetter(ch)
1✔
568
                    || (!Character.isWhitespace(ch) && !Character.isDigit(ch) && (ch != MIDDLE_DOT) && (ch != '*') && (ch != '/') && (ch != '(') && (ch != ')')
1✔
569
                            && (ch != '[') && (ch != ']') && (ch != '\u00b9') && (ch != '\u00b2') && (ch != '\u00b3') && (ch != '^') && (ch != '+') && (ch != '-'));
570
        }
571

572
        // Returns the name for the specified unit or null if product unit.
573
        protected String nameFor(Unit<?> unit) {
574
            // Searches label database.
575
            String label = unitToName.get(unit);
1✔
576
            if (label != null)
1✔
577
                return label;
1✔
578
            if (unit instanceof BaseUnit)
1✔
579
                return ((BaseUnit<?>) unit).getSymbol();
1✔
580
            if (unit instanceof AlternateUnit)
1✔
581
                return ((AlternateUnit<?>) unit).getSymbol();
1✔
582
            if (unit instanceof TransformedUnit) {
1✔
583
                TransformedUnit<?> tfmUnit = (TransformedUnit<?>) unit;
1✔
584
                if (tfmUnit.getSymbol() != null) {
1✔
585
                    return tfmUnit.getSymbol();
1✔
586
                }
587
                Unit<?> baseUnit = tfmUnit.getParentUnit();
1✔
588
                UnitConverter cvtr = tfmUnit.getConverter(); // tfmUnit.getSystemConverter();
1✔
589
                StringBuilder result = new StringBuilder();
1✔
590
                String baseUnitName = baseUnit.toString();
1✔
591
                String prefix = prefixFor(cvtr);
1✔
592
                if ((baseUnitName.indexOf(MIDDLE_DOT) >= 0) || (baseUnitName.indexOf('*') >= 0) || (baseUnitName.indexOf('/') >= 0)) {
1✔
593
                    // We could use parentheses whenever baseUnits is an
594
                    // instanceof ProductUnit, but most ProductUnits have
595
                    // aliases,
596
                    // so we'd end up with a lot of unnecessary parentheses.
597
                    result.append('(');
1✔
598
                    result.append(baseUnitName);
1✔
599
                    result.append(')');
1✔
600
                } else {
601
                    result.append(baseUnitName);
1✔
602
                }
603
                if (prefix != null) {
1✔
604
                    result.insert(0, prefix);
1✔
605
                } else {
606
                    if (cvtr instanceof AddConverter) {
1✔
607
                        result.append('+');
1✔
608
                        result.append(((AddConverter) cvtr).getOffset());
1✔
609
                    } else if (cvtr instanceof MultiplyConverter) {
1✔
610
                        Number scaleFactor = ((MultiplyConverter) cvtr).getFactor();
1✔
611
                        if(scaleFactor instanceof RationalNumber) {
1✔
612

613
                            RationalNumber rational = (RationalNumber)scaleFactor;
1✔
614
                            RationalNumber reciprocal = rational.reciprocal();
1✔
615
                            if(reciprocal.isInteger()) {
1✔
616
                                result.append('/');
1✔
617
                                result.append(reciprocal.toString()); // renders as integer
1✔
618
                            } else {
619
                                result.append('*');
1✔
620
                                result.append(scaleFactor);
1✔
621
                            }
622

623
                        } else {
1✔
624
                            result.append('*');
×
625
                            result.append(scaleFactor);
×
626
                        }
627

628
                    } else { // Other converters.
1✔
629
                        return "[" + baseUnit + "?]";
1✔
630
                    }
631
                }
632
                return result.toString();
1✔
633
            }
634
            if (unit instanceof AnnotatedUnit<?>) {
1✔
635
                AnnotatedUnit<?> annotatedUnit = (AnnotatedUnit<?>) unit;
1✔
636
                final StringBuilder annotable = new StringBuilder(nameFor(annotatedUnit.getActualUnit()));
1✔
637
                if (annotatedUnit.getAnnotation() != null) {
1✔
638
                    annotable.append('{'); // TODO maybe also configure this one similar to mix delimiter
1✔
639
                    annotable.append(annotatedUnit.getAnnotation());
1✔
640
                    annotable.append('}');
1✔
641
                }
642
                return annotable.toString();
1✔
643
            }
644
            return null; // Product unit.
1✔
645
        }
646

647
        // Returns the prefix for the specified unit converter.
648
        protected String prefixFor(UnitConverter converter) {
649
            for (int i = 0; i < METRIC_PREFIX_CONVERTERS.length; i++) {
1✔
650
                if (METRIC_PREFIX_CONVERTERS[i].equals(converter)) {
1✔
651
                    return METRIC_PREFIX_SYMBOLS[i];
1✔
652
                }
653
            }
654
            for (int j = 0; j < BINARY_PREFIX_CONVERTERS.length; j++) {
1✔
655
                if (BINARY_PREFIX_CONVERTERS[j].equals(converter)) {
1✔
656
                    return BINARY_PREFIX_SYMBOLS[j];
1✔
657
                }
658
            }
659
            return null; // TODO or return blank?
1✔
660
        }
661

662
        // Returns the unit for the specified name.
663
        protected Unit<?> unitFor(String name) {
664
            Unit<?> unit = nameToUnit.get(name);
1✔
665
            if (unit != null) {
1✔
666
                return unit;
1✔
667
            } else {
668
                unit = symbolToUnit.get(name);
1✔
669
            }
670
            return unit;
1✔
671
        }
672

673
        // //////////////////////////
674
        // Parsing.
675
        @SuppressWarnings({ "rawtypes", "unchecked" })
676
        public Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException {
677
            int startIndex = pos.getIndex();
1✔
678
            String name = readIdentifier(csq, pos);
1✔
679
            Unit unit = unitFor(name);
1✔
680
            check(unit != null, name + " not recognized", csq, startIndex);
1✔
681
            return unit;
1✔
682
        }
683

684
        @SuppressWarnings({ "rawtypes", "unchecked" })
685
        @Override
686
        public Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException {
687
                Unit result = null;
1✔
688
                if (csq == null) {
1✔
689
                    throw new MeasurementParseException("Cannot parse null", csq, pos.getIndex());
×
690
            } else {
691
                    result = unitFor(csq.toString());
1✔
692
                    if (result != null)
1✔
693
                            return result;
1✔
694
            }
695
                result = AbstractUnit.ONE;
1✔
696
            Token token = nextToken(csq, pos);
1✔
697
            switch (token) {
1✔
698
            case IDENTIFIER:
699
                result = parseSingleUnit(csq, pos);
1✔
700
                break;
1✔
701
            case OPEN_PAREN:
702
                pos.setIndex(pos.getIndex() + 1);
×
703
                result = parseProductUnit(csq, pos);
×
704
                token = nextToken(csq, pos);
×
705
                check(token == Token.CLOSE_PAREN, "')' expected", csq, pos.getIndex());
×
706
                pos.setIndex(pos.getIndex() + 1);
×
707
                break;
×
708
            default:
709
                break;
710
            }
711
            token = nextToken(csq, pos);
1✔
712
            while (true) {
713
                switch (token) {
1✔
714
                case EXPONENT:
715
                    Exponent e = readExponent(csq, pos);
1✔
716
                    if (e.pow != 1) {
1✔
717
                        result = result.pow(e.pow);
1✔
718
                    }
719
                    if (e.root != 1) {
1✔
720
                        result = result.root(e.root);
1✔
721
                    }
722
                    break;
723
                case MULTIPLY:
724
                    pos.setIndex(pos.getIndex() + 1);
×
725
                    token = nextToken(csq, pos);
×
726
                    if (token == Token.INTEGER) {
×
727
                        long n = readLong(csq, pos);
×
728
                        if (n != 1) {
×
729
                            result = result.multiply(n);
×
730
                        }
731
                    } else if (token == Token.FLOAT) {
×
732
                        double d = readDouble(csq, pos);
×
733
                        if (d != 1.0) {
×
734
                            result = result.multiply(d);
×
735
                        }
736
                    } else {
×
737
                        result = result.multiply(parseProductUnit(csq, pos));
×
738
                    }
739
                    break;
×
740
                case DIVIDE:
741
                    pos.setIndex(pos.getIndex() + 1);
1✔
742
                    token = nextToken(csq, pos);
1✔
743
                    if (token == Token.INTEGER) {
1✔
744
                        long n = readLong(csq, pos);
×
745
                        if (n != 1) {
×
746
                            result = result.divide(n);
×
747
                        }
748
                    } else if (token == Token.FLOAT) {
1✔
749
                        double d = readDouble(csq, pos);
×
750
                        if (d != 1.0) {
×
751
                            result = result.divide(d);
×
752
                        }
753
                    } else {
×
754
                        result = result.divide(parseProductUnit(csq, pos));
1✔
755
                    }
756
                    break;
1✔
757
                case PLUS:
758
                    pos.setIndex(pos.getIndex() + 1);
×
759
                    token = nextToken(csq, pos);
×
760
                    if (token == Token.INTEGER) {
×
761
                        long n = readLong(csq, pos);
×
762
                        if (n != 1) {
×
763
                            result = result.shift(n);
×
764
                        }
765
                    } else if (token == Token.FLOAT) {
×
766
                        double d = readDouble(csq, pos);
×
767
                        if (d != 1.0) {
×
768
                            result = result.shift(d);
×
769
                        }
770
                    } else {
×
771
                        throw new MeasurementParseException("not a number", csq, pos.getIndex());
×
772
                    }
773
                    break;
774
                case EOF:
775
                case CLOSE_PAREN:
776
                    return result;
1✔
777
                default:
778
                    throw new MeasurementParseException("unexpected token " + token, csq, pos.getIndex());
1✔
779
                }
780
                token = nextToken(csq, pos);
1✔
781
            }
782
        }
783

784
        private static Token nextToken(CharSequence csq, ParsePosition pos) {
785
            final int length = csq.length();
1✔
786
            while (pos.getIndex() < length) {
1✔
787
                char c = csq.charAt(pos.getIndex());
1✔
788
                if (isUnitIdentifierPart(c)) {
1✔
789
                    return Token.IDENTIFIER;
1✔
790
                } else if (c == '(') {
1✔
791
                    return Token.OPEN_PAREN;
×
792
                } else if (c == ')') {
1✔
793
                    return Token.CLOSE_PAREN;
×
794
                } else if ((c == '^') || (c == '\u00b9') || (c == '\u00b2') || (c == '\u00b3')) {
1✔
795
                    return Token.EXPONENT;
1✔
796
                } else if (c == '*') {
1✔
797
                    if (csq.length() == pos.getIndex() + 1) {
×
798
                        throw new MeasurementParseException("unexpected token " + Token.EOF, csq, pos.getIndex()); // return ;
×
799
                    }
800
                    char c2 = csq.charAt(pos.getIndex() + 1);
×
801
                    return c2 == '*' ? Token.EXPONENT : Token.MULTIPLY;
×
802
                } else if (c == MIDDLE_DOT) {
1✔
803
                    return Token.MULTIPLY;
×
804
                } else if (c == '/') {
1✔
805
                    return Token.DIVIDE;
1✔
806
                } else if (c == '+') {
1✔
807
                    return Token.PLUS;
×
808
                } else if ((c == '-') || Character.isDigit(c)) {
1✔
809
                    int index = pos.getIndex() + 1;
1✔
810
                    while ((index < length) && (Character.isDigit(c) || (c == '-') || (c == '.') || (c == 'E'))) {
1✔
811
                        c = csq.charAt(index++);
1✔
812
                        if (c == '.') {
1✔
813
                            return Token.FLOAT;
×
814
                        }
815
                    }
816
                    return Token.INTEGER;
1✔
817
                }
818
                pos.setIndex(pos.getIndex() + 1);
1✔
819
            }
1✔
820
            return Token.EOF;
1✔
821
        }
822

823
        private static void check(boolean expr, String message, CharSequence csq, int index) throws MeasurementParseException {
824
            if (!expr) {
1✔
825
                throw new MeasurementParseException(message + " (in " + csq + " at index " + index + ")", index);
1✔
826
            }
827
        }
1✔
828

829
        private static Exponent readExponent(CharSequence csq, ParsePosition pos) {
830
            char c = csq.charAt(pos.getIndex());
1✔
831
            if (c == '^') {
1✔
832
                pos.setIndex(pos.getIndex() + 1);
1✔
UNCOV
833
            } else if (c == '*') {
×
834
                pos.setIndex(pos.getIndex() + 2);
×
835
            }
836
            final int length = csq.length();
1✔
837
            int pow = 0;
1✔
838
            boolean isPowNegative = false;
1✔
839
            boolean parseRoot = false;
1✔
840

841
            POWERLOOP: while (pos.getIndex() < length) {
1✔
842
                c = csq.charAt(pos.getIndex());
1✔
843
                switch(c) {
1✔
844
                case '-': isPowNegative = true; break;
×
845
                case '\u00b9': pow = pow * 10 + 1; break;
×
846
                case '\u00b2': pow = pow * 10 + 2; break;
×
UNCOV
847
                case '\u00b3': pow = pow * 10 + 3; break;
×
848
                case ':': parseRoot = true; break POWERLOOP;
1✔
849
                default:
850
                    if (c >= '0' && c <= '9') pow = pow * 10 + (c - '0');
1✔
851
                    else break POWERLOOP;
852
                }
853
                pos.setIndex(pos.getIndex() + 1);
1✔
854
            }
855
            if (pow == 0) pow = 1;
1✔
856

857
            int root = 0;
1✔
858
            boolean isRootNegative = false;
1✔
859
            if (parseRoot) {
1✔
860
                pos.setIndex(pos.getIndex() + 1);
1✔
861
                ROOTLOOP: while (pos.getIndex() < length) {
1✔
862
                    c = csq.charAt(pos.getIndex());
1✔
863
                    switch(c) {
1✔
864
                    case '-': isRootNegative = true; break;
×
865
                    case '\u00b9': root = root * 10 + 1; break;
×
866
                    case '\u00b2': root = root * 10 + 2; break;
×
867
                    case '\u00b3': root = root * 10 + 3; break;
×
868
                    default:
869
                        if (c >= '0' && c <= '9') root = root * 10 + (c - '0');
1✔
870
                        else break ROOTLOOP;
871
                    }
872
                    pos.setIndex(pos.getIndex() + 1);
1✔
873
                }
874
            }
875
            if (root == 0) root = 1;
1✔
876

877
            return new Exponent(isPowNegative ? -pow : pow, isRootNegative ? -root : root);
1✔
878
        }
879

880
        private static long readLong(CharSequence csq, ParsePosition pos) {
881
            final int length = csq.length();
×
882
            int result = 0;
×
883
            boolean isNegative = false;
×
884
            while (pos.getIndex() < length) {
×
885
                char c = csq.charAt(pos.getIndex());
×
886
                if (c == '-') {
×
887
                    isNegative = true;
×
888
                } else if ((c >= '0') && (c <= '9')) {
×
889
                    result = result * 10 + (c - '0');
×
890
                } else {
891
                    break;
892
                }
893
                pos.setIndex(pos.getIndex() + 1);
×
894
            }
×
895
            return isNegative ? -result : result;
×
896
        }
897

898
        private static double readDouble(CharSequence csq, ParsePosition pos) {
899
            final int length = csq.length();
×
900
            int start = pos.getIndex();
×
901
            int end = start + 1;
×
902
            while (end < length) {
×
903
                if ("0123456789+-.E".indexOf(csq.charAt(end)) < 0) {
×
904
                    break;
×
905
                }
906
                end += 1;
×
907
            }
908
            pos.setIndex(end + 1);
×
909
            return Double.parseDouble(csq.subSequence(start, end).toString());
×
910
        }
911

912
        private static String readIdentifier(CharSequence csq, ParsePosition pos) {
913
            final int length = csq.length();
1✔
914
            int start = pos.getIndex();
1✔
915
            int i = start;
1✔
916
            while ((++i < length) && isUnitIdentifierPart(csq.charAt(i))) {
1✔
917
            }
918
            pos.setIndex(i);
1✔
919
            return csq.subSequence(start, i).toString();
1✔
920
        }
921

922
        // //////////////////////////
923
        // Formatting.
924

925
        @Override
926
        public Appendable format(Unit<?> unit, Appendable appendable) throws IOException {
927
            String name = nameFor(unit);
1✔
928
            if (name != null) {
1✔
929
                return appendable.append(name);
1✔
930
            }
931
            if (!(unit instanceof ProductUnit)) {
1✔
932
                throw new IllegalArgumentException("Cannot format given Object as a Unit");
×
933
            }
934

935
            // Product unit.
936
            ProductUnit<?> productUnit = (ProductUnit<?>) unit;
1✔
937

938
            // Special case: self-powered product unit
939
            if (productUnit.getUnitCount() == 1 && productUnit.getUnit(0) instanceof ProductUnit) {
1✔
940
                final ProductUnit<?> powerUnit = (ProductUnit<?>) productUnit.getUnit(0);
×
941
                // is the sub-unit known under a given label?
942
                if (nameFor(powerUnit) == null)
×
943
                    // apply the power to the sub-units and format those instead
944
                    return format(ProductUnit.ofPow(powerUnit, productUnit.getUnitPow(0)), appendable);
×
945
            }
946

947
            int invNbr = 0;
1✔
948

949
            // Write positive exponents first.
950
            boolean start = true;
1✔
951
            for (int i = 0; i < productUnit.getUnitCount(); i++) {
1✔
952
                int pow = productUnit.getUnitPow(i);
1✔
953
                if (pow >= 0) {
1✔
954
                    if (!start) {
1✔
955
                        appendable.append(MIDDLE_DOT); // Separator.
1✔
956
                    }
957
                    name = nameFor(productUnit.getUnit(i));
1✔
958
                    int root = productUnit.getUnitRoot(i);
1✔
959
                    append(appendable, name, pow, root);
1✔
960
                    start = false;
1✔
961
                } else {
1✔
962
                    invNbr++;
1✔
963
                }
964
            }
965

966
            // Write negative exponents.
967
            if (invNbr != 0) {
1✔
968
                if (start) {
1✔
969
                    appendable.append('1'); // e.g. 1/s
1✔
970
                }
971
                appendable.append('/');
1✔
972
                if (invNbr > 1) {
1✔
973
                    appendable.append('(');
1✔
974
                }
975
                start = true;
1✔
976
                for (int i = 0; i < productUnit.getUnitCount(); i++) {
1✔
977
                    int pow = productUnit.getUnitPow(i);
1✔
978
                    if (pow < 0) {
1✔
979
                        name = nameFor(productUnit.getUnit(i));
1✔
980
                        int root = productUnit.getUnitRoot(i);
1✔
981
                        if (!start) {
1✔
982
                            appendable.append(MIDDLE_DOT); // Separator.
1✔
983
                        }
984
                        append(appendable, name, -pow, root);
1✔
985
                        start = false;
1✔
986
                    }
987
                }
988
                if (invNbr > 1) {
1✔
989
                    appendable.append(')');
1✔
990
                }
991
            }
992
            return appendable;
1✔
993
        }
994

995
        private static void append(Appendable appendable, CharSequence symbol, int pow, int root) throws IOException {
996
            appendable.append(symbol);
1✔
997
            if ((pow != 1) || (root != 1)) {
1✔
998
                // Write exponent.
999
                if ((pow == 2) && (root == 1)) {
1✔
1000
                    appendable.append('\u00b2'); // Square
1✔
1001
                } else if ((pow == 3) && (root == 1)) {
1✔
1002
                    appendable.append('\u00b3'); // Cubic
1✔
1003
                } else {
1004
                    // Use general exponent form.
1005
                    appendable.append('^');
1✔
1006
                    appendable.append(String.valueOf(pow));
1✔
1007
                    if (root != 1) {
1✔
1008
                        appendable.append(':');
1✔
1009
                        appendable.append(String.valueOf(root));
1✔
1010
                    }
1011
                }
1012
            }
1013
        }
1✔
1014

1015
        // private static final long serialVersionUID = 1L;
1016

1017
        @Override
1018
        public Unit<?> parse(CharSequence csq) throws MeasurementParseException {
1019
            return parse(csq, 0);
1✔
1020
        }
1021

1022
        protected Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException {
1023
            return parse(csq, new ParsePosition(index));
1✔
1024
        }
1025

1026
        @Override
1027
        public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException {
1028
            return parseObject(csq.toString(), cursor);
1✔
1029
        }
1030
    }
1031

1032
    /**
1033
     * This class represents the ASCII_INSTANCE format.
1034
     */
1035
    private static final class ASCIIFormat extends DefaultFormat {
1036

1037
        private ASCIIFormat() {
1038
            super();
1039
        }
1040

1041
        private ASCIIFormat init() {
1042

1043
            // ASCII_INSTANCE
1044
            for (int i = 0; i < METRIC_UNITS.length; i++) {
1✔
1045
                Unit<?> si = METRIC_UNITS[i];
1✔
1046
                String symbol = (si instanceof BaseUnit) ? ((BaseUnit<?>) si).getSymbol() : ((AlternateUnit<?>) si).getSymbol();
1✔
1047
                if (isAllAscii(symbol))
1✔
1048
                    label(si, symbol);
1✔
1049
                for (int j = 0; j < METRIC_PREFIX_SYMBOLS.length; j++) {
1✔
1050
                    Unit<?> u = si.prefix(MetricPrefix.values()[j]);
1✔
1051
                    if ( "µ".equals(METRIC_PREFIX_SYMBOLS[j]) ) {
1✔
1052
                        label(u, "micro" + asciiSymbol(symbol));
1✔
1053
                    }
1054
                } // TODO what about BINARY_PREFIX here?
1055
            }
1056

1057
            // -- GRAM/KILOGRAM
1058

1059
            label(Units.GRAM, "g");
1✔
1060
            for(MetricPrefix prefix : MetricPrefix.values()) {
1✔
1061
                switch (prefix) {
1✔
1062
                case KILO:
1063
                    label(Units.KILOGRAM, "kg");
1✔
1064
                    break;
1✔
1065
                case MICRO:
1066
                    label(MICRO(Units.GRAM), "microg"); // instead of 'µg' -> 'microg'
1✔
1067
                    break;
1✔
1068
                default:
1069
                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
1✔
1070
                    break;
1071
                }
1072
            }
1073

1074
            // ASCIIFormat for Ohm
1075
            labelWithAsciiPrefixes(Units.OHM, "Ohm");
1✔
1076

1077
            // Special case for DEGREE_CELSIUS.
1078
            labelWithAsciiPrefixes(Units.CELSIUS, "Celsius");
1✔
1079
            aliasWithAsciiPrefixes(Units.CELSIUS, "Cel");
1✔
1080

1081
            label(Units.METRE, "m");
1✔
1082
            label(Units.SECOND, "s");
1✔
1083
            label(Units.KILOMETRE_PER_HOUR, "km/h");
1✔
1084
            alias(Units.SQUARE_METRE, "m2");
1✔
1085
            alias(Units.CUBIC_METRE, "m3");
1✔
1086

1087
            // -- LITRE
1088

1089
            label(Units.LITRE, "l");
1✔
1090
            for(Prefix prefix : MetricPrefix.values()) {
1✔
1091
                if(prefix==MICRO) {
1✔
1092
                    label(MICRO(Units.LITRE), "microL"); // instead of 'µL' -> 'microL'
1✔
1093
                } else {
1094
                    label(Units.LITRE.prefix(prefix), prefix.getSymbol()+"L");
1✔
1095
                }
1096
            }
1097
            label(Units.NEWTON, "N");
1✔
1098
            label(Units.RADIAN, "rad");
1✔
1099

1100
            label(AbstractUnit.ONE, "one");
1✔
1101

1102
            return this;
1✔
1103
        }
1104

1105

1106
        @Override
1107
        protected String nameFor(Unit<?> unit) {
1108
            // First search if specific ASCII_INSTANCE name should be used.
1109
            String name = unitToName.get(unit);
1✔
1110
            if (name != null)
1✔
1111
                return name;
1✔
1112
            // Else returns default name.
1113
            return DEFAULT_INSTANCE.nameFor(unit);
1✔
1114
        }
1115

1116
        @Override
1117
        protected Unit<?> unitFor(String name) {
1118
            // First search if specific ASCII_INSTANCE name.
1119
            Unit<?> unit = nameToUnit.get(name);
1✔
1120
            if (unit != null)
1✔
1121
                return unit;
1✔
1122
            // Else returns default mapping.
1123
            return DEFAULT_INSTANCE.unitFor(name);
1✔
1124
        }
1125

1126
        @Override
1127
        public String toString() {
1128
            return "SimpleUnitFormat - ASCII";
1✔
1129
        }
1130

1131
        @Override
1132
        public Appendable format(Unit<?> unit, Appendable appendable) throws IOException {
1133
            String name = nameFor(unit);
1✔
1134
            if (name != null)
1✔
1135
                return appendable.append(name);
1✔
1136
            if (!(unit instanceof ProductUnit))
1✔
1137
                throw new IllegalArgumentException("Cannot format given Object as a Unit");
×
1138

1139
            ProductUnit<?> productUnit = (ProductUnit<?>) unit;
1✔
1140
            for (int i = 0; i < productUnit.getUnitCount(); i++) {
1✔
1141
                if (i != 0) {
1✔
1142
                    appendable.append('*'); // Separator.
1✔
1143
                }
1144
                name = nameFor(productUnit.getUnit(i));
1✔
1145
                int pow = productUnit.getUnitPow(i);
1✔
1146
                int root = productUnit.getUnitRoot(i);
1✔
1147
                appendable.append(name);
1✔
1148
                if ((pow != 1) || (root != 1)) {
1✔
1149
                    // Use general exponent form.
1150
                    appendable.append('^');
1✔
1151
                    appendable.append(String.valueOf(pow));
1✔
1152
                    if (root != 1) {
1✔
1153
                        appendable.append(':');
1✔
1154
                        appendable.append(String.valueOf(root));
1✔
1155
                    }
1156
                }
1157
            }
1158
            return appendable;
1✔
1159
        }
1160

1161
        @Override
1162
        protected boolean isValidIdentifier(String name) {
1163
            if ((name == null) || (name.length() == 0))
1✔
1164
                return false;
×
1165
            // label must not begin with a digit or mathematical operator
1166
            return isUnitIdentifierPart(name.charAt(0)) && isAllAscii(name);
1✔
1167
            /*
1168
             * for (int i = 0; i < name.length(); i++) { if
1169
             * (!isAsciiCharacter(name.charAt(i))) return false; } return true;
1170
             */
1171
        }
1172
        
1173
        /**
1174
         * Applies {@link #alias(Unit, String)} for this unit and all standard prefixes, if the alias contains only ASCII characters.
1175
         * 
1176
         * @param unit a unit
1177
         * @param alias an alias
1178
         */
1179
        private void aliasWithAsciiPrefixes(Unit<?> unit, String alias) {
1180
                if (isValidIdentifier(alias)) {
1✔
1181
                        alias(unit, alias);
1✔
1182
                    for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1✔
1183
                        alias(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + alias);
1✔
1184
                    }
1185
                    for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1✔
1186
                            alias(unit.prefix(BinaryPrefix.values()[i]), asciiPrefix(BINARY_PREFIX_SYMBOLS[i]) + alias);
1✔
1187
                    }
1188
                }
1189
        }
1✔
1190
        
1191
        /**
1192
         * Applies {@link #label(Unit, String)} for this unit and all standard prefixes, if the label contains only ASCII characters.
1193
         * 
1194
         * @param unit a unit
1195
         * @param alias an label
1196
         */
1197
        private void labelWithAsciiPrefixes(Unit<?> unit, String label) {
1198
                if (isValidIdentifier(label)) {
1✔
1199
                        label(unit, label);
1✔
1200
                    for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1✔
1201
                            label(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + label);
1✔
1202
                    }
1203
                    for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1✔
1204
                            label(unit.prefix(BinaryPrefix.values()[i]), asciiPrefix(BINARY_PREFIX_SYMBOLS[i]) + label);
1✔
1205
                    }
1206
                }
1207
        }
1✔
1208
        
1209
        private static String asciiPrefix(String prefix) {
1210
            return "µ".equals(prefix) ? "micro" : prefix;
1✔
1211
        }
1212

1213
        private static String asciiSymbol(String s) {
1214
            return "Ω".equals(s) ? "Ohm" : s;
1✔
1215
        }
1216

1217
        /** to check if a string only contains US-ASCII_INSTANCE characters */
1218
        private static boolean isAllAscii(String input) {
1219
            boolean isASCII = true;
1✔
1220
            for (int i = 0; i < input.length(); i++) {
1✔
1221
                int c = input.charAt(i);
1✔
1222
                if (c > 0x7F) {
1✔
1223
                    isASCII = false;
1✔
1224
                    break;
1✔
1225
                }
1226
            }
1227
            return isASCII;
1✔
1228
        }
1229
    }
1230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc