001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2018, 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 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; 033import java.text.ParsePosition; 034 035import javax.measure.Unit; 036 037/** 038 * Formats instances of {@link Unit} to a {@link String} or {@link Appendable} and parses a {@link CharSequence} to a {@link Unit}. 039 * 040 * <h1><a name="synchronization">Synchronization</a></h1> 041 * <p> 042 * Instances of this class are not required to be thread-safe. It is recommended to use separate format instances for each thread. If multiple threads 043 * access a format concurrently, it must be synchronized externally. 044 * <p> 045 * 046 * @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a> 047 * @author <a href="mailto:[email protected]">Werner Keil</a> 048 * 049 * @version 1.3, August 8, 2018 050 * @since 1.0 051 * 052 * @see Unit 053 */ 054public interface UnitFormat { 055 /** 056 * Formats the specified {@link Unit}. 057 * 058 * @param unit 059 * the {@link Unit} to format, not {@code null} 060 * @param appendable 061 * the appendable destination. 062 * @return the appendable destination passed in with formatted text appended. 063 * @throws IOException 064 * if an error occurs while writing to the destination. 065 */ 066 Appendable format(Unit<?> unit, Appendable appendable) throws IOException; 067 068 /** 069 * Formats the specified {@link Unit}. 070 * 071 * @param unit 072 * the {@link Unit} to format, not {@code null} 073 * @return the string representation using the settings of this {@link UnitFormat}. 074 */ 075 String format(Unit<?> unit); 076 077 /** 078 * Attaches a system-wide label to the specified unit. 079 * <p> 080 * If the specified label is already associated to a unit the previous association can be discarded or ignored. Depending on the {@link UnitFormat} 081 * implementation, this call may be ignored if the particular unit already has a label. 082 * </p> 083 * If a {@link UnitFormat} implementation is explicitly <b>immutable</b>, similar to e.g. the result of <tt>Collections.unmodifiableList()</tt>, 084 * then an {@linkplain UnsupportedOperationException} may be thrown upon this call. 085 * <p> 086 * Since <tt>UnitFormat</tt> implementations often apply the Singleton pattern, <b>system-wide</b> means, the label applies to every instance of 087 * <tt>UnitFormatA</tt> implementing <tt>UnitFormat</tt> in this case, but not every instance of <tt>UnitFormatB</tt> or <tt>UnitFormatC</tt> both 088 * also implementing <tt>UnitFormat</tt>. If a <tt>UnitFormat</tt> #isLocaleSensitive() it is up to the implementation, whether the label is 089 * ignored, applied in a local-neutral manner (in addition to its local-sensitive information) or locale-specific. 090 * </p> 091 * 092 * @param unit 093 * the unit being labeled. 094 * @param label 095 * the new label for this unit. 096 * @throws IllegalArgumentException 097 * if the label is not a valid identifier. This may include characters not supported by a particular {@link UnitFormat} implementation 098 * (e.g. only <b>ASCII</b> characters for certain devices) 099 * @throws UnsupportedOperationException 100 * if the <tt>label</tt> operation is not supported by this {@link UnitFormat} 101 */ 102 void label(Unit<?> unit, String label); 103 104 /** 105 * Returns <code>true</code> if this {@link UnitFormat} depends on a <code>Locale</code> to perform its tasks. 106 * <p> 107 * In environments that do not support a <code>Locale</code>, e.g. Java ME, this usually returns <code>false</code>. 108 * </p> 109 * 110 * @return Whether this format depends on the locale. 111 */ 112 default boolean isLocaleSensitive() { 113 return false; 114 } 115 116 /** 117 * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a {@link Unit}. If parsing succeeds, then 118 * the index of the <code>cursor</code> argument is updated to the index after the last character used. 119 * 120 * @param csq 121 * the <code>CharSequence</code> to parse. 122 * @param cursor 123 * the cursor holding the current parsing index. 124 * @return the unit parsed from the specified character sub-sequence. 125 * @throws IllegalArgumentException 126 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 127 */ 128 Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException, MeasurementParseException; 129 130 /** 131 * Parses the text into an instance of {@link Unit}. 132 * <p> 133 * The parse must complete normally and parse the entire text. If the parse completes without reading the entire length of the text, an exception is 134 * thrown. If any other problem occurs during parsing, an exception is thrown. 135 * <p> 136 * 137 * @param csq 138 * the {@code CharSequence} to parse. 139 * @return the unit parsed from the specified character sequence. 140 * @throws MeasurementParseException 141 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 142 * @throws UnsupportedOperationException 143 * if the {@link UnitFormat} is unable to parse. 144 * @since 2.0 145 */ 146 Unit<?> parse(CharSequence csq) throws MeasurementParseException; 147}