001/**
002 * Unit-API - Units of Measurement API for Java
003 * Copyright (c) 2014 Jean-Marie Dautelle, Werner Keil, V2COM
004 * All rights reserved.
005 *
006 * See LICENSE.txt for details.
007 */
008package javax.measure;
009
010import java.util.List;
011
012/**
013 * A converter of numeric values between different units.
014 *
015 * <p>Instances of this class are obtained through the {@link Unit#getConverterTo(Unit)} method.</p>
016 *
017 * @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a>
018 * @author <a href="mailto:[email protected]">Werner Keil</a>
019 * @version 0.7.1, 2014-09-07
020 *
021 * @see <a href="http://en.wikipedia.org/wiki/Conversion_of_units"> Wikipedia:
022 *      Conversion of units</a>
023 */
024public interface UnitConverter {
025        
026    /**
027     * Indicates if this converter is an identity converter.
028     * The identity converter returns its input argument ({@code convert(x) == x}).
029     *
030     * @return {@code true} if this converter is an identity converter.
031     */
032    boolean isIdentity();
033    
034    /**
035     * Indicates if this converter is linear. A converter is linear if:
036     *
037     * <ul>
038     *   <li>{@code convert(u + v) == convert(u) + convert(v)}</li>
039     *   <li>{@code convert(r * u) == r * convert(u)}</li>
040     * </ul>
041     *
042     * <p>For linear converters the following property always hold:</p>
043     *
044     * [code]
045     *   y1 = c1.convert(x1);
046     *   y2 = c2.convert(x2);
047     *   assert y1*y2 == c1.concatenate(c2).convert(x1*x2);
048     * [/code]
049     *
050     * @return {@code true} if this converter is linear; {@code false} otherwise.
051     */
052    boolean isLinear();
053
054    /**
055     * Returns the inverse of this converter. If {@code x} is a valid value,
056     * then {@code x == inverse().convert(convert(x))} to within the accuracy
057     * of computer arithmetic.
058     *
059     * @return the inverse of this converter.
060     */
061    UnitConverter inverse();
062    
063    /**
064     * Converts a {@code Number} value.
065     *
066     * @param  value the {@code Number} value to convert.
067     * @return the {@code Number} value after conversion.
068     */
069    Number convert(Number value);
070
071    /**
072     * Converts a {@code double} value.
073     *
074     * @param  value the numeric value to convert.
075     * @return the {@code double} value after conversion.
076     */
077    double convert(double value);
078
079    /**
080     * Concatenates this converter with another converter. The resulting
081     * converter is equivalent to first converting by the specified converter
082     * (right converter), and then converting by this converter (left converter).
083     *
084     * @param  converter the other converter to concatenate with this converter.
085     * @return the concatenation of this converter with the other converter.
086     */
087    UnitConverter concatenate(UnitConverter converter);
088
089    /**
090     * <p>Returns the steps of fundamental converters making up this converter or
091     * {@code this} if the converter is a fundamental converter.</p>
092     * <p>
093     * For example, {@code converter1.getConversionSteps()} returns {@code {converter1} while {@code converter1.concatenate(converter2).getConversionSteps()} returns {@code {converter1, converter2}.</p>
094     * @return the list of fundamental converters which concatenated make up this converter.
095     */
096    List<? extends UnitConverter> getConversionSteps();
097}