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