001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2015, Jean-Marie Dautelle, Werner Keil, V2COM.
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-363 nor the names of its 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 javax.measure.format;
031
032import java.io.IOException;
033
034import javax.measure.Unit;
035
036/**
037 * <p>
038 * Formats instances of {@link Unit} to a {@link String} or an {@link Appendable} and parses a {@link CharSequence} to a {@link Unit}.
039 * </p>
040 * <h4><a name="synchronization">Synchronization</a></h4>
041 * <p>
042 * <p>
043 * Instances of this class are not required to be thread-safe. It is recommended to of separate
044 * format instances for each thread. If multiple threads access a format concurrently, it must be
045 * synchronized externally.
046 * <p>
047 *
048 * @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a>
049 * @author <a href="mailto:[email protected]">Werner Keil</a>
050 *
051 * @version 0.7.3, Oct 10, 2015
052 *
053 * @see Unit
054 */
055public interface UnitFormat {
056    /**
057     * Formats the specified {@link Unit}.
058     *
059     * @param  unit the {@link Unit} to format, not {@code null}
060     * @param  appendable the appendable destination.
061     * @return the appendable destination passed in with formatted text appended.
062     * @throws IOException if an error occurs while writing to the destination.
063     */
064    Appendable format(Unit<?> unit, Appendable appendable) throws IOException;
065    
066    /**
067     * Formats the specified {@link Unit}.
068     *
069     * @param  unit the {@link Unit} to format, not {@code null}
070     * @return the string representation using the settings of this {@link UnitFormat}.
071     * @throws IOException if an error occurs while writing to the destination.
072     */
073    String format(Unit<?> unit);
074    
075    /**
076     * Attaches a system-wide label to the specified unit.<p>
077     * If the specified label is already associated to a unit the previous
078     * association can be discarded or ignored. 
079     * Depending on the {@link UnitFormat} implementation, this call may be ignored if the particular unit already has a label.</p>
080     * If a {@link UnitFormat} implementation is explicitly <b>immutable</b>, similar to e.g. the result of <tt>Collections.unmodifiableList()</tt>, then an {@linkplain UnsupportedOperationException} may be thrown upon this call.<p>
081     * Since <tt>UnitFormat</tt> implementations often apply the Singleton pattern, <b>system-wide</b> means, the label applies to every instance of <tt>UnitFormatA</tt> implementing <tt>UnitFormat</tt> in this case, but not every instance of <tt>UnitFormatB</tt> or <tt>UnitFormatC</tt> both also implementing <tt>UnitFormat</tt>.  
082     * If a <tt>UnitFormat</tt> #isLocaleSensitive() it is up to the implementation, whether the label is ignored, applied in a local-neutral manner (in addition to its local-sensitive information) or locale-specific.
083     * </p>
084     * @param unit
085     *            the unit being labeled.
086     * @param label
087     *            the new label for this unit.
088     * @throws IllegalArgumentException
089     *             if the label is not a valid identifier. This may include characters not supported by a particular {@link UnitFormat} implementation (e.g. only <b>ASCII</b> characters for certain devices) 
090     * @throws UnsupportedOperationException if the <tt>label</tt> operation
091     *         is not supported by this {@link UnitFormat}
092     */
093    void label(Unit<?> unit, String label);
094    
095    /**
096     * Returns <code>true</code> if this {@link UnitFormat} depends on a <code>Locale</code> to perform its tasks.<p>
097     * In environments that do not support a <code>Locale</code>, e.g. Java ME, this usually returns <code>false</code>.</p>
098     * @return Whether this format depends on the locale.
099     */
100    boolean isLocaleSensitive();
101        
102    /**
103     * Parses a portion of the specified {@code CharSequence} from the
104     * specified position to produce a unit. If there is no unit to parse
105     * the unitary unit (dimensionless) is returned.
106     *
107     * @param  csq the {@code CharSequence} to parse.
108     * @return the unit parsed from the specified character sub-sequence.
109     * @throws ParserException if any problem occurs while parsing the
110     *         specified character sequence (e.g. illegal syntax).
111     */
112    Unit<?> parse(CharSequence csq) throws ParserException;
113}