001/* 002 * Units of Measurement Reference Implementation 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.units.ri.format; 031 032import java.io.IOException; 033import java.util.logging.Logger; 034 035import javax.measure.Unit; 036import javax.measure.format.ParserException; 037import javax.measure.format.UnitFormat; 038 039import tec.units.ri.AbstractUnit; 040 041/** 042 * <p> 043 * This class provides the interface for formatting and parsing {@link AbstractUnit 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:<br> 048 * <code> 049 * AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS)) 050 * AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT))</code> 051 * </p> 052 * 053 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 054 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 055 * @version 1.0.1, October 6, 2016 056 * @since 1.0 057 * 058 */ 059public abstract class AbstractUnitFormat implements UnitFormat { 060 /** 061 * serialVersionUID 062 */ 063 // private static final long serialVersionUID = -2046025267890654321L; 064 065 protected static final Logger logger = Logger.getLogger(AbstractUnitFormat.class.getName()); 066 067 /** 068 * Formats an object to produce a string. This is equivalent to <blockquote> {@link #format(Unit, StringBuilder) format}<code>(unit, 069 * new StringBuilder()).toString();</code> </blockquote> 070 * 071 * @param obj 072 * The object to format 073 * @return Formatted string. 074 * @exception IllegalArgumentException 075 * if the Format cannot format the given object 076 */ 077 public final String format(Unit<?> unit) { 078 if (unit instanceof AbstractUnit) { 079 return format((AbstractUnit<?>) unit, new StringBuilder()).toString(); 080 } else { 081 try { 082 return (this.format(unit, new StringBuilder())).toString(); 083 } catch (IOException ex) { 084 throw new ParserException(ex); // Should never happen. 085 } 086 } 087 } 088 089 /** 090 * Formats the specified unit. 091 * 092 * @param unit 093 * the unit to format. 094 * @param appendable 095 * the appendable destination. 096 * @return The appendable destination passed in as {@code appendable}, with formatted text appended. 097 * @throws IOException 098 * if an error occurs. 099 */ 100 public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException; 101 102 /** 103 * Parses a portion of the specified <code>CharSequence</code> from the specified position to produce a unit. If there is no unit to parse 104 * {@link AbstractUnit#ONE} is returned. 105 * 106 * @param csq 107 * the <code>CharSequence</code> to parse. 108 * @param index 109 * the current parsing index. 110 * @return the unit parsed from the specified character sub-sequence. 111 * @throws IllegalArgumentException 112 * if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). 113 */ 114 protected abstract Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException; 115 116 public boolean isLocaleSensitive() { 117 return false; 118 } 119 120 /** 121 * Convenience method equivalent to {@link #format(AbstractUnit, Appendable)} except it does not raise an IOException. 122 * 123 * @param unit 124 * the unit to format. 125 * @param dest 126 * the appendable destination. 127 * @return the specified <code>StringBuilder</code>. 128 */ 129 final StringBuilder format(AbstractUnit<?> unit, StringBuilder dest) { 130 try { 131 return (StringBuilder) this.format(unit, (Appendable) dest); 132 } catch (IOException ex) { 133 throw new ParserException(ex); // Should never happen. 134 } 135 } 136}