001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2024, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
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 java.io.IOException;
033import java.text.FieldPosition;
034import java.text.ParsePosition;
035import java.util.HashMap;
036import java.util.Map;
037import java.util.stream.Collectors;
038import java.util.stream.Stream;
039
040import javax.measure.BinaryPrefix;
041import javax.measure.MeasurementError;
042import javax.measure.MetricPrefix;
043import javax.measure.Prefix;
044import javax.measure.Quantity;
045import javax.measure.Unit;
046import javax.measure.UnitConverter;
047import javax.measure.format.MeasurementParseException;
048import javax.measure.format.UnitFormat;
049
050import static javax.measure.MetricPrefix.MICRO;
051
052import tech.units.indriya.AbstractUnit;
053import tech.units.indriya.function.AddConverter;
054import tech.units.indriya.function.MultiplyConverter;
055import tech.units.indriya.function.RationalNumber;
056import tech.units.indriya.unit.AlternateUnit;
057import tech.units.indriya.unit.AnnotatedUnit;
058import tech.units.indriya.unit.BaseUnit;
059import tech.units.indriya.unit.ProductUnit;
060import tech.units.indriya.unit.TransformedUnit;
061import tech.units.indriya.unit.Units;
062
063import static tech.units.indriya.format.FormatConstants.MIDDLE_DOT;
064
065/**
066 * <p>
067 * This class implements the {@link UnitFormat} interface for formatting and parsing {@link Unit units}.
068 * </p>
069 *
070 * <p>
071 * 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>
072 * {@link Units} are directly recognized. For example:<br>
073 * <code>
074 *        UnitFormat format = SimpleUnitFormat.getInstance();<br>
075 *        format.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS));<br>
076 *        format.parse("kW").equals(MetricPrefix.KILO(Units.WATT));<br>
077 *        format.parse("ft").equals(Units.METRE.multiply(0.3048))</code>
078 * </p>
079 *
080 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
081 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
082 * @author Eric Russell
083 * @author Andi Huber
084 * @version 2.15, Nov 24, 2024
085 * @since 1.0
086 */
087public abstract class SimpleUnitFormat extends AbstractUnitFormat {
088    // private static final long serialVersionUID = 4149424034841739785L;
089
090    /**
091     * Flavor of this format
092     *
093     * @author Werner
094     *
095     */
096    public static enum Flavor {
097        /** @deprecated use DEFAULT_INSTANCE */
098         Default, 
099         /** The default format flavor */
100         DEFAULT, 
101         /** The ASCII_INSTANCE format flavor */
102         ASCII
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();
111
112    /**
113     * Holds the ASCII_INSTANCE format instance.
114     */
115    private static final ASCIIFormat ASCII_INSTANCE = new ASCIIFormat().init();
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);
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) {
134        case ASCII:
135            return SimpleUnitFormat.ASCII_INSTANCE;
136        default:
137            return DEFAULT_INSTANCE;
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);
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) {
163        case ASCII:
164            return new ASCIIFormat().init();
165        default:
166            return new DefaultFormat().init();
167        }
168    }
169
170    /**
171     * Base constructor.
172     */
173    protected SimpleUnitFormat() {
174    }
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);
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) {
343            this.pow = pow;
344            this.root = root;
345        }
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,
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())
362                .map(Prefix::getSymbol)
363                .collect(Collectors.toList())
364                .toArray(new String[] {});
365
366        // TODO try to consolidate those
367        static final UnitConverter[] METRIC_PREFIX_CONVERTERS =
368                Stream.of(MetricPrefix.values())
369                .map(MultiplyConverter::ofPrefix)
370                .collect(Collectors.toList())
371                .toArray(new UnitConverter[] {});
372
373        static final String[] BINARY_PREFIX_SYMBOLS =
374                Stream.of(BinaryPrefix.values())
375                .map(Prefix::getSymbol)
376                .collect(Collectors.toList())
377                .toArray(new String[] {});
378
379        static final UnitConverter[] BINARY_PREFIX_CONVERTERS =
380                Stream.of(BinaryPrefix.values())
381                .map(MultiplyConverter::ofPrefix)
382                .collect(Collectors.toList())
383                .toArray(new UnitConverter[] {});
384
385        /**
386         * Holds the unique symbols collection (base units or alternate units).
387         */
388        private final Map<String, Unit<?>> symbolToUnit = new HashMap<>();
389
390        private static enum Token { EOF, IDENTIFIER, OPEN_PAREN, CLOSE_PAREN, EXPONENT, MULTIPLY, DIVIDE,
391            PLUS, INTEGER, FLOAT };
392
393
394        DefaultFormat() {
395            // Hack, somehow µg is not found.
396            symbolToUnit.put(MetricPrefix.MICRO.getSymbol() + "g", MICRO(Units.GRAM));
397            symbolToUnit.put("μg", MICRO(Units.GRAM));
398            symbolToUnit.put(MU + "g", MICRO(Units.GRAM));
399        }
400
401        private DefaultFormat init() {
402
403            for (int i = 0; i < METRIC_UNITS.length; i++) {
404                Unit<?> si = METRIC_UNITS[i];
405                String symbol = (si instanceof BaseUnit) ? ((BaseUnit<?>) si).getSymbol() : ((AlternateUnit<?>) si).getSymbol();
406                label(si, symbol);
407                for (int j = 0; j < METRIC_PREFIX_SYMBOLS.length; j++) {
408                    Unit<?> u = si.prefix(MetricPrefix.values()[j]);
409                    label(u, METRIC_PREFIX_SYMBOLS[j] + symbol);
410                    if ( "µ".equals(METRIC_PREFIX_SYMBOLS[j]) ) {
411                        label(u, MU + symbol);
412                    }
413                } // TODO what about BINARY_PREFIX here?
414            }
415
416            // -- GRAM/KILOGRAM
417
418            label(Units.GRAM, "g");
419            for(MetricPrefix prefix : MetricPrefix.values()) {
420                switch (prefix) {
421                case KILO:
422                    label(Units.KILOGRAM, "kg");
423                    break;
424                case MICRO:
425                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
426                    break;
427                default:
428                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
429                    break;
430                }
431            }
432
433            label(MICRO(Units.GRAM), MetricPrefix.MICRO.getSymbol() + "g");
434
435            // Alias in ASCIIFormat for Ohm
436            aliasWithPrefixes(Units.OHM, "Ohm");
437
438            // Special case for DEGREE_CELSIUS.
439            labelWithPrefixes(Units.CELSIUS, "℃");
440            aliasWithPrefixes(Units.CELSIUS, "°C");
441            // Additional cases and aliases
442            label(Units.PERCENT, "%");
443            labelWithPrefixes(Units.MINUTE, "min");
444            labelWithPrefixes(Units.HOUR, "h");
445            labelWithPrefixes(Units.DAY, "d");
446            aliasWithPrefixes(Units.DAY, "day");
447            labelWithPrefixes(Units.WEEK, "wk");
448            aliasWithPrefixes(Units.WEEK, "week");
449            label(Units.YEAR, "yr");
450            alias(Units.YEAR, "y");
451            alias(Units.YEAR, "year");
452            alias(Units.YEAR, "days365");
453            alias(Units.YEAR, "a");
454            label(Units.MONTH, "mo");
455            alias(Units.MONTH, "mon");
456            alias(Units.MONTH, "month");
457            label(Units.KILOMETRE_PER_HOUR, "km/h");
458            labelWithPrefixes(Units.SQUARE_METRE, "\u33A1");
459            aliasWithPrefixes(Units.SQUARE_METRE, "m2");
460            labelWithPrefixes(Units.CUBIC_METRE, "\u33A5");
461            aliasWithPrefixes(Units.CUBIC_METRE, "m3");
462            labelWithPrefixes(Units.LITRE, "l");
463
464            label(AbstractUnit.ONE, "one");
465            //label(Units.NEWTON, "N");
466            //label(Units.RADIAN, "rad");
467            //label(Units.METRE, "m");
468            //label(Units.SECOND, "s");
469
470            return this;
471        }
472
473        /**
474         * Holds the name to unit mapping.
475         */
476        protected final Map<String, Unit<?>> nameToUnit = new HashMap<>();
477
478        /**
479         * Holds the unit to name mapping.
480         */
481        protected final Map<Unit<?>, String> unitToName = new HashMap<>();
482
483        @Override
484        public String toString() {
485            return SimpleUnitFormat.class.getSimpleName();
486        }
487
488        @Override
489        public void label(Unit<?> unit, String label) {
490            if (!isValidIdentifier(label))
491                throw new IllegalArgumentException("Label: " + label + " is not a valid identifier.");
492            synchronized (this) {
493                nameToUnit.put(label, unit);
494                unitToName.put(unit, label);
495            }
496        }
497        
498        @Override
499                public void removeLabel(Unit<?> unit) {
500                        unitToName.remove(unit);
501                        nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit));
502                }
503
504        @Override
505        public void alias(Unit<?> unit, String alias) {
506            if (!isValidIdentifier(alias))
507                throw new IllegalArgumentException("Alias: " + alias + " is not a valid identifier.");
508            synchronized (this) {
509                nameToUnit.put(alias, unit);
510            }
511        }
512
513        @Override
514                public void removeAlias(Unit<?> unit, String alias) {
515                        nameToUnit.remove(alias);
516                }
517        
518                @Override
519                public void removeAliases(Unit<?> unit) {
520                        final String alias = unitToName.get(unit);
521                        nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit) && !e.getKey().equals(alias));
522                }
523
524        @Override
525        protected boolean isValidIdentifier(String name) {
526            if ((name == null) || (name.length() == 0))
527                return false;
528            return isUnitIdentifierPart(name.charAt(0));
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);
539                // TODO try to optimize this
540            for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
541                label(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + label);
542            }
543            for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
544                label(unit.prefix(BinaryPrefix.values()[i]), BINARY_PREFIX_SYMBOLS[i] + label);
545            }
546        }
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);
556                // TODO try to optimize this
557            for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
558                alias(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + alias);
559            }
560            for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
561                alias(unit.prefix(BinaryPrefix.values()[i]), BINARY_PREFIX_SYMBOLS[i] + alias);
562            }
563
564        }
565
566        protected static boolean isUnitIdentifierPart(char ch) {
567            return Character.isLetter(ch)
568                    || (!Character.isWhitespace(ch) && !Character.isDigit(ch) && (ch != MIDDLE_DOT) && (ch != '*') && (ch != '/') && (ch != '(') && (ch != ')')
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);
576            if (label != null)
577                return label;
578            if (unit instanceof BaseUnit)
579                return ((BaseUnit<?>) unit).getSymbol();
580            if (unit instanceof AlternateUnit)
581                return ((AlternateUnit<?>) unit).getSymbol();
582            if (unit instanceof TransformedUnit) {
583                TransformedUnit<?> tfmUnit = (TransformedUnit<?>) unit;
584                if (tfmUnit.getSymbol() != null) {
585                    return tfmUnit.getSymbol();
586                }
587                Unit<?> baseUnit = tfmUnit.getParentUnit();
588                UnitConverter cvtr = tfmUnit.getConverter(); // tfmUnit.getSystemConverter();
589                StringBuilder result = new StringBuilder();
590                String baseUnitName = baseUnit.toString();
591                String prefix = prefixFor(cvtr);
592                if ((baseUnitName.indexOf(MIDDLE_DOT) >= 0) || (baseUnitName.indexOf('*') >= 0) || (baseUnitName.indexOf('/') >= 0)) {
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('(');
598                    result.append(baseUnitName);
599                    result.append(')');
600                } else {
601                    result.append(baseUnitName);
602                }
603                if (prefix != null) {
604                    result.insert(0, prefix);
605                } else {
606                    if (cvtr instanceof AddConverter) {
607                        result.append('+');
608                        result.append(((AddConverter) cvtr).getOffset());
609                    } else if (cvtr instanceof MultiplyConverter) {
610                        Number scaleFactor = ((MultiplyConverter) cvtr).getFactor();
611                        if(scaleFactor instanceof RationalNumber) {
612
613                            RationalNumber rational = (RationalNumber)scaleFactor;
614                            RationalNumber reciprocal = rational.reciprocal();
615                            if(reciprocal.isInteger()) {
616                                result.append('/');
617                                result.append(reciprocal.toString()); // renders as integer
618                            } else {
619                                result.append('*');
620                                result.append(scaleFactor);
621                            }
622
623                        } else {
624                            result.append('*');
625                            result.append(scaleFactor);
626                        }
627
628                    } else { // Other converters.
629                        return "[" + baseUnit + "?]";
630                    }
631                }
632                return result.toString();
633            }
634            if (unit instanceof AnnotatedUnit<?>) {
635                AnnotatedUnit<?> annotatedUnit = (AnnotatedUnit<?>) unit;
636                final StringBuilder annotable = new StringBuilder(nameFor(annotatedUnit.getActualUnit()));
637                if (annotatedUnit.getAnnotation() != null) {
638                    annotable.append('{'); // TODO maybe also configure this one similar to mix delimiter
639                    annotable.append(annotatedUnit.getAnnotation());
640                    annotable.append('}');
641                }
642                return annotable.toString();
643            }
644            return null; // Product unit.
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++) {
650                if (METRIC_PREFIX_CONVERTERS[i].equals(converter)) {
651                    return METRIC_PREFIX_SYMBOLS[i];
652                }
653            }
654            for (int j = 0; j < BINARY_PREFIX_CONVERTERS.length; j++) {
655                if (BINARY_PREFIX_CONVERTERS[j].equals(converter)) {
656                    return BINARY_PREFIX_SYMBOLS[j];
657                }
658            }
659            return null; // TODO or return blank?
660        }
661
662        // Returns the unit for the specified name.
663        protected Unit<?> unitFor(String name) {
664            Unit<?> unit = nameToUnit.get(name);
665            if (unit != null) {
666                return unit;
667            } else {
668                unit = symbolToUnit.get(name);
669            }
670            return unit;
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();
678            String name = readIdentifier(csq, pos);
679            Unit unit = unitFor(name);
680            check(unit != null, name + " not recognized", csq, startIndex);
681            return unit;
682        }
683
684        @SuppressWarnings({ "rawtypes", "unchecked" })
685        @Override
686        public Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException {
687                Unit result = null;
688                if (csq == null) {
689                throw new MeasurementParseException("Cannot parse null", csq, pos.getIndex());
690            } else {
691                result = unitFor(csq.toString());
692                if (result != null)
693                        return result;
694            }
695                result = AbstractUnit.ONE;
696            Token token = nextToken(csq, pos);
697            switch (token) {
698            case IDENTIFIER:
699                result = parseSingleUnit(csq, pos);
700                break;
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);
712            while (true) {
713                switch (token) {
714                case EXPONENT:
715                    Exponent e = readExponent(csq, pos);
716                    if (e.pow != 1) {
717                        result = result.pow(e.pow);
718                    }
719                    if (e.root != 1) {
720                        result = result.root(e.root);
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);
742                    token = nextToken(csq, pos);
743                    if (token == Token.INTEGER) {
744                        long n = readLong(csq, pos);
745                        if (n != 1) {
746                            result = result.divide(n);
747                        }
748                    } else if (token == Token.FLOAT) {
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));
755                    }
756                    break;
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;
777                default:
778                    throw new MeasurementParseException("unexpected token " + token, csq, pos.getIndex());
779                }
780                token = nextToken(csq, pos);
781            }
782        }
783
784        private static Token nextToken(CharSequence csq, ParsePosition pos) {
785            final int length = csq.length();
786            while (pos.getIndex() < length) {
787                char c = csq.charAt(pos.getIndex());
788                if (isUnitIdentifierPart(c)) {
789                    return Token.IDENTIFIER;
790                } else if (c == '(') {
791                    return Token.OPEN_PAREN;
792                } else if (c == ')') {
793                    return Token.CLOSE_PAREN;
794                } else if ((c == '^') || (c == '\u00b9') || (c == '\u00b2') || (c == '\u00b3')) {
795                    return Token.EXPONENT;
796                } else if (c == '*') {
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) {
803                    return Token.MULTIPLY;
804                } else if (c == '/') {
805                    return Token.DIVIDE;
806                } else if (c == '+') {
807                    return Token.PLUS;
808                } else if ((c == '-') || Character.isDigit(c)) {
809                    int index = pos.getIndex() + 1;
810                    while ((index < length) && (Character.isDigit(c) || (c == '-') || (c == '.') || (c == 'E'))) {
811                        c = csq.charAt(index++);
812                        if (c == '.') {
813                            return Token.FLOAT;
814                        }
815                    }
816                    return Token.INTEGER;
817                }
818                pos.setIndex(pos.getIndex() + 1);
819            }
820            return Token.EOF;
821        }
822
823        private static void check(boolean expr, String message, CharSequence csq, int index) throws MeasurementParseException {
824            if (!expr) {
825                throw new MeasurementParseException(message + " (in " + csq + " at index " + index + ")", index);
826            }
827        }
828
829        private static Exponent readExponent(CharSequence csq, ParsePosition pos) {
830            char c = csq.charAt(pos.getIndex());
831            if (c == '^') {
832                pos.setIndex(pos.getIndex() + 1);
833            } else if (c == '*') {
834                pos.setIndex(pos.getIndex() + 2);
835            }
836            final int length = csq.length();
837            int pow = 0;
838            boolean isPowNegative = false;
839            boolean parseRoot = false;
840
841            POWERLOOP: while (pos.getIndex() < length) {
842                c = csq.charAt(pos.getIndex());
843                switch(c) {
844                case '-': isPowNegative = true; break;
845                case '\u00b9': pow = pow * 10 + 1; break;
846                case '\u00b2': pow = pow * 10 + 2; break;
847                case '\u00b3': pow = pow * 10 + 3; break;
848                case ':': parseRoot = true; break POWERLOOP;
849                default:
850                    if (c >= '0' && c <= '9') pow = pow * 10 + (c - '0');
851                    else break POWERLOOP;
852                }
853                pos.setIndex(pos.getIndex() + 1);
854            }
855            if (pow == 0) pow = 1;
856
857            int root = 0;
858            boolean isRootNegative = false;
859            if (parseRoot) {
860                pos.setIndex(pos.getIndex() + 1);
861                ROOTLOOP: while (pos.getIndex() < length) {
862                    c = csq.charAt(pos.getIndex());
863                    switch(c) {
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');
870                        else break ROOTLOOP;
871                    }
872                    pos.setIndex(pos.getIndex() + 1);
873                }
874            }
875            if (root == 0) root = 1;
876
877            return new Exponent(isPowNegative ? -pow : pow, isRootNegative ? -root : root);
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();
914            int start = pos.getIndex();
915            int i = start;
916            while ((++i < length) && isUnitIdentifierPart(csq.charAt(i))) {
917            }
918            pos.setIndex(i);
919            return csq.subSequence(start, i).toString();
920        }
921
922        // //////////////////////////
923        // Formatting.
924
925        @Override
926        public Appendable format(Unit<?> unit, Appendable appendable) throws IOException {
927            String name = nameFor(unit);
928            if (name != null) {
929                return appendable.append(name);
930            }
931            if (!(unit instanceof ProductUnit)) {
932                throw new IllegalArgumentException("Cannot format given Object as a Unit");
933            }
934
935            // Product unit.
936            ProductUnit<?> productUnit = (ProductUnit<?>) unit;
937
938            // Special case: self-powered product unit
939            if (productUnit.getUnitCount() == 1 && productUnit.getUnit(0) instanceof ProductUnit) {
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;
948
949            // Write positive exponents first.
950            boolean start = true;
951            for (int i = 0; i < productUnit.getUnitCount(); i++) {
952                int pow = productUnit.getUnitPow(i);
953                if (pow >= 0) {
954                    if (!start) {
955                        appendable.append(MIDDLE_DOT); // Separator.
956                    }
957                    name = nameFor(productUnit.getUnit(i));
958                    int root = productUnit.getUnitRoot(i);
959                    append(appendable, name, pow, root);
960                    start = false;
961                } else {
962                    invNbr++;
963                }
964            }
965
966            // Write negative exponents.
967            if (invNbr != 0) {
968                if (start) {
969                    appendable.append('1'); // e.g. 1/s
970                }
971                appendable.append('/');
972                if (invNbr > 1) {
973                    appendable.append('(');
974                }
975                start = true;
976                for (int i = 0; i < productUnit.getUnitCount(); i++) {
977                    int pow = productUnit.getUnitPow(i);
978                    if (pow < 0) {
979                        name = nameFor(productUnit.getUnit(i));
980                        int root = productUnit.getUnitRoot(i);
981                        if (!start) {
982                            appendable.append(MIDDLE_DOT); // Separator.
983                        }
984                        append(appendable, name, -pow, root);
985                        start = false;
986                    }
987                }
988                if (invNbr > 1) {
989                    appendable.append(')');
990                }
991            }
992            return appendable;
993        }
994
995        private static void append(Appendable appendable, CharSequence symbol, int pow, int root) throws IOException {
996            appendable.append(symbol);
997            if ((pow != 1) || (root != 1)) {
998                // Write exponent.
999                if ((pow == 2) && (root == 1)) {
1000                    appendable.append('\u00b2'); // Square
1001                } else if ((pow == 3) && (root == 1)) {
1002                    appendable.append('\u00b3'); // Cubic
1003                } else {
1004                    // Use general exponent form.
1005                    appendable.append('^');
1006                    appendable.append(String.valueOf(pow));
1007                    if (root != 1) {
1008                        appendable.append(':');
1009                        appendable.append(String.valueOf(root));
1010                    }
1011                }
1012            }
1013        }
1014
1015        // private static final long serialVersionUID = 1L;
1016
1017        @Override
1018        public Unit<?> parse(CharSequence csq) throws MeasurementParseException {
1019            return parse(csq, 0);
1020        }
1021
1022        protected Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException {
1023            return parse(csq, new ParsePosition(index));
1024        }
1025
1026        @Override
1027        public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException {
1028            return parseObject(csq.toString(), cursor);
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++) {
1045                Unit<?> si = METRIC_UNITS[i];
1046                String symbol = (si instanceof BaseUnit) ? ((BaseUnit<?>) si).getSymbol() : ((AlternateUnit<?>) si).getSymbol();
1047                if (isAllAscii(symbol))
1048                    label(si, symbol);
1049                for (int j = 0; j < METRIC_PREFIX_SYMBOLS.length; j++) {
1050                    Unit<?> u = si.prefix(MetricPrefix.values()[j]);
1051                    if ( "µ".equals(METRIC_PREFIX_SYMBOLS[j]) ) {
1052                        label(u, "micro" + asciiSymbol(symbol));
1053                    }
1054                } // TODO what about BINARY_PREFIX here?
1055            }
1056
1057            // -- GRAM/KILOGRAM
1058
1059            label(Units.GRAM, "g");
1060            for(MetricPrefix prefix : MetricPrefix.values()) {
1061                switch (prefix) {
1062                case KILO:
1063                    label(Units.KILOGRAM, "kg");
1064                    break;
1065                case MICRO:
1066                    label(MICRO(Units.GRAM), "microg"); // instead of 'µg' -> 'microg'
1067                    break;
1068                default:
1069                    label(Units.GRAM.prefix(prefix), prefix.getSymbol()+"g");
1070                    break;
1071                }
1072            }
1073
1074            // ASCIIFormat for Ohm
1075            labelWithAsciiPrefixes(Units.OHM, "Ohm");
1076
1077            // Special case for DEGREE_CELSIUS.
1078            labelWithAsciiPrefixes(Units.CELSIUS, "Celsius");
1079            aliasWithAsciiPrefixes(Units.CELSIUS, "Cel");
1080
1081            label(Units.METRE, "m");
1082            label(Units.SECOND, "s");
1083            label(Units.KILOMETRE_PER_HOUR, "km/h");
1084            alias(Units.SQUARE_METRE, "m2");
1085            alias(Units.CUBIC_METRE, "m3");
1086
1087            // -- LITRE
1088
1089            label(Units.LITRE, "l");
1090            for(Prefix prefix : MetricPrefix.values()) {
1091                if(prefix==MICRO) {
1092                    label(MICRO(Units.LITRE), "microL"); // instead of 'µL' -> 'microL'
1093                } else {
1094                    label(Units.LITRE.prefix(prefix), prefix.getSymbol()+"L");
1095                }
1096            }
1097            label(Units.NEWTON, "N");
1098            label(Units.RADIAN, "rad");
1099
1100            label(AbstractUnit.ONE, "one");
1101
1102            return this;
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);
1110            if (name != null)
1111                return name;
1112            // Else returns default name.
1113            return DEFAULT_INSTANCE.nameFor(unit);
1114        }
1115
1116        @Override
1117        protected Unit<?> unitFor(String name) {
1118            // First search if specific ASCII_INSTANCE name.
1119            Unit<?> unit = nameToUnit.get(name);
1120            if (unit != null)
1121                return unit;
1122            // Else returns default mapping.
1123            return DEFAULT_INSTANCE.unitFor(name);
1124        }
1125
1126        @Override
1127        public String toString() {
1128            return "SimpleUnitFormat - ASCII";
1129        }
1130
1131        @Override
1132        public Appendable format(Unit<?> unit, Appendable appendable) throws IOException {
1133            String name = nameFor(unit);
1134            if (name != null)
1135                return appendable.append(name);
1136            if (!(unit instanceof ProductUnit))
1137                throw new IllegalArgumentException("Cannot format given Object as a Unit");
1138
1139            ProductUnit<?> productUnit = (ProductUnit<?>) unit;
1140            for (int i = 0; i < productUnit.getUnitCount(); i++) {
1141                if (i != 0) {
1142                    appendable.append('*'); // Separator.
1143                }
1144                name = nameFor(productUnit.getUnit(i));
1145                int pow = productUnit.getUnitPow(i);
1146                int root = productUnit.getUnitRoot(i);
1147                appendable.append(name);
1148                if ((pow != 1) || (root != 1)) {
1149                    // Use general exponent form.
1150                    appendable.append('^');
1151                    appendable.append(String.valueOf(pow));
1152                    if (root != 1) {
1153                        appendable.append(':');
1154                        appendable.append(String.valueOf(root));
1155                    }
1156                }
1157            }
1158            return appendable;
1159        }
1160
1161        @Override
1162        protected boolean isValidIdentifier(String name) {
1163            if ((name == null) || (name.length() == 0))
1164                return false;
1165            // label must not begin with a digit or mathematical operator
1166            return isUnitIdentifierPart(name.charAt(0)) && isAllAscii(name);
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)) {
1181                        alias(unit, alias);
1182                    for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1183                        alias(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + alias);
1184                    }
1185                    for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1186                        alias(unit.prefix(BinaryPrefix.values()[i]), asciiPrefix(BINARY_PREFIX_SYMBOLS[i]) + alias);
1187                    }
1188                }
1189        }
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)) {
1199                        label(unit, label);
1200                    for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
1201                        label(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + label);
1202                    }
1203                    for (int i = 0; i < BINARY_PREFIX_SYMBOLS.length; i++) {
1204                        label(unit.prefix(BinaryPrefix.values()[i]), asciiPrefix(BINARY_PREFIX_SYMBOLS[i]) + label);
1205                    }
1206                }
1207        }
1208        
1209        private static String asciiPrefix(String prefix) {
1210            return "µ".equals(prefix) ? "micro" : prefix;
1211        }
1212
1213        private static String asciiSymbol(String s) {
1214            return "Ω".equals(s) ? "Ohm" : s;
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;
1220            for (int i = 0; i < input.length(); i++) {
1221                int c = input.charAt(i);
1222                if (c > 0x7F) {
1223                    isASCII = false;
1224                    break;
1225                }
1226            }
1227            return isASCII;
1228        }
1229    }
1230}