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