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 an {@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 0.8, Nov 25, 2015 049 * 050 * @see Unit 051 */ 052public interface UnitFormat { 053 /** 054 * Formats the specified {@link Unit}. 055 * 056 * @param unit 057 * the {@link Unit} to format, not {@code null} 058 * @param appendable 059 * the appendable destination. 060 * @return the appendable destination passed in with formatted text appended. 061 * @throws IOException 062 * 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 070 * the {@link Unit} to format, not {@code null} 071 * @return the string representation using the settings of this {@link UnitFormat}. 072 */ 073 String format(Unit<?> unit); 074 075 /** 076 * Attaches a system-wide label to the specified unit. 077 * <p> 078 * If the specified label is already associated to a unit the previous association can be discarded or ignored. Depending on the {@link UnitFormat} 079 * implementation, this call may be ignored if the particular unit already has a label. 080 * </p> 081 * If a {@link UnitFormat} implementation is explicitly <b>immutable</b>, similar to e.g. the result of <tt>Collections.unmodifiableList()</tt>, 082 * then an {@linkplain UnsupportedOperationException} may be thrown upon this call. 083 * <p> 084 * Since <tt>UnitFormat</tt> implementations often apply the Singleton pattern, <b>system-wide</b> means, the label applies to every instance of 085 * <tt>UnitFormatA</tt> implementing <tt>UnitFormat</tt> in this case, but not every instance of <tt>UnitFormatB</tt> or <tt>UnitFormatC</tt> both 086 * also implementing <tt>UnitFormat</tt>. If a <tt>UnitFormat</tt> #isLocaleSensitive() it is up to the implementation, whether the label is 087 * ignored, applied in a local-neutral manner (in addition to its local-sensitive information) or locale-specific. 088 * </p> 089 * 090 * @param unit 091 * the unit being labeled. 092 * @param label 093 * the new label for this unit. 094 * @throws IllegalArgumentException 095 * if the label is not a valid identifier. This may include characters not supported by a particular {@link UnitFormat} implementation 096 * (e.g. only <b>ASCII</b> characters for certain devices) 097 * @throws UnsupportedOperationException 098 * if the <tt>label</tt> operation is not supported by this {@link UnitFormat} 099 */ 100 void label(Unit<?> unit, String label); 101 102 /** 103 * Returns <code>true</code> if this {@link UnitFormat} depends on a <code>Locale</code> to perform its tasks. 104 * <p> 105 * In environments that do not support a <code>Locale</code>, e.g. Java ME, this usually returns <code>false</code>. 106 * </p> 107 * 108 * @return Whether this format depends on the locale. 109 */ 110 boolean isLocaleSensitive(); 111 112 /** 113 * Parses the text into an instance of {@link Unit}. 114 * <p> 115 * 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 116 * thrown. If any other problem occurs during parsing, an exception is thrown. 117 * <p> 118 * 119 * @param csq 120 * the {@code CharSequence} to parse. 121 * @return the unit parsed from the specified character sequence. 122 * @throws ParserException 123 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 124 * @throws UnsupportedOperationException 125 * if the {@link UnitFormat} is unable to parse. 126 */ 127 Unit<?> parse(CharSequence csq) throws ParserException; 128}