001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2015, 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;
031
032import java.util.List;
033
034/**
035 * A converter of numeric values between different units.
036 *
037 * <p>Instances of this class are obtained through the {@link Unit#getConverterTo(Unit)} method.</p>
038 *
039 * @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a>
040 * @author <a href="mailto:[email protected]">Werner Keil</a>
041 * @version 0.7.1, 2014-09-07
042 *
043 * @see <a href="http://en.wikipedia.org/wiki/Conversion_of_units"> Wikipedia:
044 *      Conversion of units</a>
045 */
046public interface UnitConverter {
047        
048    /**
049     * Indicates if this converter is an identity converter.
050     * The identity converter returns its input argument ({@code convert(x) == x}).
051     *
052     * @return {@code true} if this converter is an identity converter.
053     */
054    boolean isIdentity();
055    
056    /**
057     * Indicates if this converter is linear. A converter is linear if:
058     *
059     * <ul>
060     *   <li>{@code convert(u + v) == convert(u) + convert(v)}</li>
061     *   <li>{@code convert(r * u) == r * convert(u)}</li>
062     * </ul>
063     *
064     * <p>For linear converters the following property always hold:</p>
065     *
066     * [code]
067     *   y1 = c1.convert(x1);
068     *   y2 = c2.convert(x2);
069     *   assert y1*y2 == c1.concatenate(c2).convert(x1*x2);
070     * [/code]
071     *
072     * @return {@code true} if this converter is linear; {@code false} otherwise.
073     */
074    boolean isLinear();
075
076    /**
077     * Returns the inverse of this converter. If {@code x} is a valid value,
078     * then {@code x == inverse().convert(convert(x))} to within the accuracy
079     * of computer arithmetic.
080     *
081     * @return the inverse of this converter.
082     */
083    UnitConverter inverse();
084    
085    /**
086     * Converts a {@code Number} value.
087     *
088     * @param  value the {@code Number} value to convert.
089     * @return the {@code Number} value after conversion.
090     */
091    Number convert(Number value);
092
093    /**
094     * Converts a {@code double} value.
095     *
096     * @param  value the numeric value to convert.
097     * @return the {@code double} value after conversion.
098     */
099    double convert(double value);
100
101    /**
102     * Concatenates this converter with another converter. The resulting
103     * converter is equivalent to first converting by the specified converter
104     * (right converter), and then converting by this converter (left converter).
105     *
106     * @param  converter the other converter to concatenate with this converter.
107     * @return the concatenation of this converter with the other converter.
108     */
109    UnitConverter concatenate(UnitConverter converter);
110
111    /**
112     * <p>Returns the steps of fundamental converters making up this converter or
113     * {@code this} if the converter is a fundamental converter.</p>
114     * <p>
115     * For example, {@code converter1.getConversionSteps()} returns {@code {converter1} while {@code converter1.concatenate(converter2).getConversionSteps()} returns {@code {converter1, converter2}.</p>
116     * @return the list of fundamental converters which concatenated make up this converter.
117     */
118    List<? extends UnitConverter> getConversionSteps();
119}