001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2018, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
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-385 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 usually 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 1.1, March 25, 2018
044 * @since 1.0
045 *
046 * @see Unit
047 * @see <a href="http://en.wikipedia.org/wiki/Conversion_of_units"> Wikipedia: Conversion of units</a>
048 */
049public interface UnitConverter {
050
051  /**
052   * Indicates if this converter is an identity converter. The identity converter returns its input argument ({@code convert(x) == x}).
053   *
054   * @return {@code true} if this converter is an identity converter.
055   */
056  boolean isIdentity();
057
058  /**
059   * Indicates if this converter is linear. A converter is linear if:
060   *
061   * <ul>
062   * <li>{@code convert(u + v) == convert(u) + convert(v)}</li>
063   * <li>{@code convert(r * u) == r * convert(u)}</li>
064   * </ul>
065   *
066   * <p>
067   * For linear converters the following property always hold:
068   * </p>
069   *
070   * <code>
071   *   y1 = c1.convert(x1);
072   *   y2 = c2.convert(x2);
073   *   assert y1*y2 == c1.concatenate(c2).convert(x1*x2);
074   * </code>
075   *
076   * @return {@code true} if this converter is linear; {@code false} otherwise.
077   */
078  boolean isLinear();
079
080  /**
081   * 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
082   * computer arithmetic.
083   *
084   * @return the inverse of this converter.
085   */
086  UnitConverter inverse();
087
088  /**
089   * Converts a {@code Number} value.
090   *
091   * @param value
092   *          the {@code Number} value to convert.
093   * @return the {@code Number} value after conversion.
094   */
095  Number convert(Number value);
096
097  /**
098   * Converts a {@code double} value.
099   *
100   * @param value
101   *          the numeric value to convert.
102   * @return the {@code double} value after conversion.
103   */
104  double convert(double value);
105
106  /**
107   * Concatenates this converter with another converter. The resulting converter is equivalent to first converting by the specified converter (right
108   * converter), and then converting by this converter (left converter).
109   *
110   * @param converter
111   *          the other converter to concatenate with this converter.
112   * @return the concatenation of this converter with the other converter.
113   */
114  UnitConverter concatenate(UnitConverter converter);
115
116  /**
117   * <p>
118   * Returns the steps of fundamental converters making up this converter or {@code this} if the converter is a fundamental converter.
119   * </p>
120   * <p>
121   * For example, {@code converter1.getConversionSteps()} returns {@code converter1} while
122   * {@code converter1.concatenate(converter2).getConversionSteps()} returns {@code converter1, converter2}.
123   * </p>
124   *
125   * @return the list of fundamental converters which concatenated make up this converter.
126   */
127  List<? extends UnitConverter> getConversionSteps();
128}