All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tec.units.ri.format.AbstractUnitFormat Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * Units of Measurement Reference Implementation
 * Copyright (c) 2005-2015, Jean-Marie Dautelle, Werner Keil, V2COM.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package tec.units.ri.format;

import java.io.IOException;
import java.util.logging.Logger;

import javax.measure.Unit;
import javax.measure.format.ParserException;
import javax.measure.format.UnitFormat;

import tec.units.ri.AbstractUnit;

/**
 * 

* This class provides the interface for formatting and parsing * {@link AbstractUnit units}. *

* *

* For all metric units, the 20 SI prefixes used to form decimal multiples and * sub-multiples of SI units are recognized. For example:
* AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS)) * AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT)) *

* * @author Jean-Marie Dautelle * @author Werner Keil * @version 0.7.5 $Date: 2015-10-29 $ * */ public abstract class AbstractUnitFormat implements UnitFormat { /** * serialVersionUID */ // private static final long serialVersionUID = -2046025267890654321L; protected static final Logger logger = Logger.getLogger(AbstractUnitFormat.class .getName()); /** * Returns the {@link SymbolMap} for this unit format. * * @return the symbol map used by this format. */ //protected abstract SymbolMap getSymbols(); /** * Formats an object to produce a string. This is equivalent to
* {@link #format(Unit, StringBuilder) format}(unit, * new StringBuilder()).toString(); *
* * @param obj * The object to format * @return Formatted string. * @exception IllegalArgumentException * if the Format cannot format the given object */ public final String format(Unit unit) { if (unit instanceof AbstractUnit) { return format((AbstractUnit) unit, new StringBuilder()) .toString(); } else { try { return (this.format(unit, new StringBuilder())) .toString(); } catch (IOException ex) { throw new ParserException(ex); // Should never happen. } } } /** * Formats the specified unit. * * @param unit * the unit to format. * @param appendable * the appendable destination. * @return The appendable destination passed in as {@code appendable}, with * formatted text appended. * @throws IOException * if an error occurs. */ public abstract Appendable format(Unit unit, Appendable appendable) throws IOException; /** * Parses a portion of the specified CharSequence from the * specified position to produce a unit. If there is no unit to parse * {@link AbstractUnit#ONE} is returned. * * @param csq * the CharSequence to parse. * @param index * the current parsing index. * @return the unit parsed from the specified character sub-sequence. * @throws IllegalArgumentException * if any problem occurs while parsing the specified character * sequence (e.g. illegal syntax). */ protected abstract Unit parse(CharSequence csq, int index) throws IllegalArgumentException; public boolean isLocaleSensitive() { return false; } /** * Convenience method equivalent to * {@link #format(AbstractUnit, Appendable)} except it does not raise an * IOException. * * @param unit * the unit to format. * @param dest * the appendable destination. * @return the specified StringBuilder. */ final StringBuilder format(AbstractUnit unit, StringBuilder dest) { try { return (StringBuilder) this.format(unit, (Appendable) dest); } catch (IOException ex) { throw new ParserException(ex); // Should never happen. } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy