001/* 002 * Units of Measurement Implementation for Java SE 003 * Copyright (c) 2005-2017, 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 tec.uom.se.format; 031 032import java.io.IOException; 033import java.text.ParsePosition; 034 035import javax.measure.Unit; 036import javax.measure.format.ParserException; 037import javax.measure.format.UnitFormat; 038 039import tec.uom.se.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 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized. For example:<code> 048 * AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS)) 049 * AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT))</code> 050 * </p> 051 * 052 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 053 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 054 * @version 1.0.2, $Date: 2017-02-25 $ 055 * @since 1.0 056 * 057 */ 058public abstract class AbstractUnitFormat implements UnitFormat { 059 060 /** 061 * Returns the {@link SymbolMap} for this unit format. 062 * 063 * @return the symbol map used by this format. 064 */ 065 protected abstract SymbolMap getSymbols(); 066 067 /** 068 * Formats the specified unit. 069 * 070 * @param unit 071 * the unit to format. 072 * @param appendable 073 * the appendable destination. 074 * @return The appendable destination passed in as {@code appendable}, with formatted text appended. 075 * @throws IOException 076 * if an error occurs. 077 */ 078 public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException; 079 080 /** 081 * Formats an object to produce a string. This is equivalent to <blockquote> {@link #format(Unit, StringBuilder) format}<code>(unit, 082 * new StringBuilder()).toString();</code> </blockquote> 083 * 084 * @param obj 085 * The object to format 086 * @return Formatted string. 087 * @exception IllegalArgumentException 088 * if the Format cannot format the given object 089 */ 090 public final String format(Unit<?> unit) { 091 if (unit instanceof AbstractUnit) { 092 return format((AbstractUnit<?>) unit, new StringBuilder()).toString(); 093 } else { 094 try { 095 return (this.format(unit, new StringBuilder())).toString(); 096 } catch (IOException ex) { 097 throw new ParserException(ex); // Should never happen. 098 } 099 } 100 } 101 102 @Override 103 public void label(Unit<?> unit, String label) { 104 // do nothing, if subclasses want to use it, override there 105 } 106 107 /** 108 * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a unit. If there is no unit to parse 109 * {@link AbstractUnit#ONE} is returned. 110 * 111 * @param csq 112 * the <code>CharSequence</code> to parse. 113 * @param cursor 114 * the cursor holding the current parsing index. 115 * @return the unit parsed from the specified character sub-sequence. 116 * @throws IllegalArgumentException 117 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 118 */ 119 protected abstract Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException; 120 121 /** 122 * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a unit. If there is no unit to parse 123 * {@link AbstractUnit#ONE} is returned. 124 * 125 * @param csq 126 * the <code>CharSequence</code> to parse. 127 * @param index 128 * the current parsing index. 129 * @return the unit parsed from the specified character sub-sequence. 130 * @throws IllegalArgumentException 131 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 132 */ 133 protected abstract Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException; 134 135 /** 136 * Convenience method equivalent to {@link #format(AbstractUnit, Appendable)} except it does not raise an IOException. 137 * 138 * @param unit 139 * the unit to format. 140 * @param dest 141 * the appendable destination. 142 * @return the specified <code>StringBuilder</code>. 143 */ 144 final StringBuilder format(AbstractUnit<?> unit, StringBuilder dest) { 145 try { 146 return (StringBuilder) this.format(unit, (Appendable) dest); 147 } catch (IOException ex) { 148 throw new Error(ex); // Can never happen. 149 } 150 } 151 152 /** 153 * serialVersionUID 154 */ 155 // private static final long serialVersionUID = -2046025267890654321L; 156}