001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2020, Units of Measurement project.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package tech.units.indriya.format;
031
032import java.io.IOException;
033import java.text.ParsePosition;
034
035import javax.measure.MeasurementException;
036import javax.measure.Unit;
037import javax.measure.format.UnitFormat;
038
039import tech.units.indriya.AbstractUnit;
040
041/**
042 * <p>
043 * This class provides the interface for formatting and parsing {@link Unit units}.
044 * </p>
045 *
046 * <p>
047 * For all metric units, the 20 <b>SI prefixes</b> used to form decimal multiples and sub-multiples of SI units are recognized.<br>
048 * As well as the 8 <b>Binary prefixes</b>.<br>
049 * For example:<code>
050 *        AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS))
051 *        AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT))</code>
052 * </p>
053 *
054 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
055 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
056 * @version 1.2, $Date: 2019-02-23 $
057 * @since 1.0
058 * 
059 */
060public abstract class AbstractUnitFormat implements UnitFormat {
061
062  /**
063   * Returns the {@link SymbolMap} for this unit format.
064   *
065   * @return the symbol map used by this format.
066   */
067  protected abstract SymbolMap getSymbols();
068
069  /**
070   * Formats the specified unit.
071   *
072   * @param unit
073   *          the unit to format.
074   * @param appendable
075   *          the appendable destination.
076   * @return The appendable destination passed in as {@code appendable}, with formatted text appended.
077   * @throws IOException
078   *           if an error occurs.
079   */
080  public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException;
081
082  /**
083   * Formats an object to produce a string. This is equivalent to <blockquote> {@link #format(Unit, StringBuilder) format}<code>(unit,
084   *         new StringBuilder()).toString();</code> </blockquote>
085   *
086   * @param obj
087   *          The object to format
088   * @return Formatted string.
089   * @exception IllegalArgumentException
090   *              if the Format cannot format the given object
091   */
092  public final String format(Unit<?> unit) {
093    if (unit instanceof AbstractUnit) {
094      return format((AbstractUnit<?>) unit, new StringBuilder()).toString();
095    }
096
097    try {
098      return (this.format(unit, new StringBuilder())).toString();
099    } catch (IOException ex) {
100      throw new MeasurementException(ex); // Should never happen.
101    }
102  }
103
104  @Override
105  public void label(Unit<?> unit, String label) {
106    // do nothing, if subclasses want to use it, override there
107  }
108
109  /**
110   * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a unit. If there is no unit to parse
111   * {@link AbstractUnit#ONE} is returned.
112   *
113   * @param csq
114   *          the <code>CharSequence</code> to parse.
115   * @param cursor
116   *          the cursor holding the current parsing index.
117   * @return the unit parsed from the specified character sub-sequence.
118   * @throws IllegalArgumentException
119   *           if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
120   */
121  public abstract Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException;
122
123  /**
124   * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a unit. If there is no unit to parse
125   * {@link AbstractUnit#ONE} is returned.
126   *
127   * @param csq
128   *          the <code>CharSequence</code> to parse.
129   * @param index
130   *          the current parsing index.
131   * @return the unit parsed from the specified character sub-sequence.
132   * @throws IllegalArgumentException
133   *           if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
134   */
135  protected abstract Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException;
136
137  /**
138   * Convenience method equivalent to {@link #format(AbstractUnit, Appendable)} except it does not raise an IOException.
139   *
140   * @param unit
141   *          the unit to format.
142   * @param dest
143   *          the appendable destination.
144   * @return the specified <code>StringBuilder</code>.
145   */
146  private final StringBuilder format(AbstractUnit<?> unit, StringBuilder dest) {
147    try {
148      return (StringBuilder) this.format(unit, (Appendable) dest);
149    } catch (IOException ex) {
150      throw new MeasurementException(ex); // Can never happen.
151    }
152  }
153
154  /**
155   * serialVersionUID
156   */
157  // private static final long serialVersionUID = -2046025267890654321L;
158}