001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2019, 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 * @author <a href="mailto:[email protected]">Martin 044 * Desruisseaux</a> 045 * @author <a href="mailto:[email protected]">Thodoris Bais</a> 046 * @author <a href="mailto:[email protected]">Andi Huber</a> 047 * @version 1.4, May 12, 2019 048 * @since 1.0 049 * 050 * @see Unit 051 * @see <a href="http://en.wikipedia.org/wiki/Conversion_of_units"> Wikipedia: Conversion of units</a> 052 */ 053public interface UnitConverter { 054 055 /** 056 * Indicates if this converter is an identity converter. The identity converter returns its input argument ({@code convert(x) == x}). 057 * <p> 058 * Note: Identity converters are also always 'linear', see {@link UnitConverter#isLinear()}. 059 * </p> 060 * 061 * @return {@code true} if this converter is an identity converter. 062 */ 063 boolean isIdentity(); 064 065 /** 066 * Indicates whether this converter represents a (one-dimensional) linear transformation, that is 067 * a <a href="https://en.wikipedia.org/wiki/Linear_map">linear map (wikipedia)</a> from a one-dimensional 068 * vector space (a scalar) to a one-dimensional vector space. Typically from 'R' to 'R', with 'R' the 069 * real numbers. 070 * 071 * Given such a 'linear' converter 'A', let 'u', 'v' and 'r' be arbitrary numbers, then the following 072 * must hold by definition: 073 * 074 * <ul> 075 * <li>{@code A(u + v) == A(u) + A(v)}</li> 076 * <li>{@code A(r * u) == r * A(u)}</li> 077 * </ul> 078 * 079 * Given a second 'linear' converter 'B', commutativity of composition follows by above definition: 080 * 081 * <ul> 082 * <li>{@code (A o B) (u) == (B o A) (u)}</li> 083 * </ul> 084 * 085 * In other words, two 'linear' converters do have the property that {@code A(B(u)) == B(A(u))}, meaning 086 * for 'A' and 'B' the order of their composition does not matter. Expressed as Java code:<br> 087 * <br> 088 * 089 *{@code A.concatenate(B).convert(u) == B.concatenate(A).convert(u)}<br> 090 * <br> 091 * 092 * Note: For composing UnitConverters see also {@link UnitConverter#concatenate(UnitConverter)}. 093 * 094 * @return {@code true} if this converter represents a linear transformation; 095 * {@code false} otherwise. 096 * 097 */ 098 boolean isLinear(); 099 100 /** 101 * 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 102 * computer arithmetic. 103 * 104 * @return the inverse of this converter. 105 */ 106 UnitConverter inverse(); 107 108 /** 109 * Converts a {@code Number} value. 110 * 111 * @param value 112 * the {@code Number} value to convert. 113 * @return the {@code Number} value after conversion. 114 */ 115 Number convert(Number value); 116 117 /** 118 * Converts a {@code double} value. 119 * 120 * @param value 121 * the numeric value to convert. 122 * @return the {@code double} value after conversion. 123 */ 124 double convert(double value); 125 126 /** 127 * Concatenates this converter with another converter. The resulting converter is equivalent to first converting by the specified converter (right 128 * converter), and then converting by this converter (left converter). 129 * 130 * @param converter 131 * the other converter to concatenate with this converter. 132 * @return the concatenation of this converter with the other converter. 133 */ 134 UnitConverter concatenate(UnitConverter converter); 135 136 /** 137 * <p> 138 * Returns the steps of fundamental converters making up this converter or {@code this} if the converter is a fundamental converter. 139 * </p> 140 * <p> 141 * For example, {@code converter1.getConversionSteps()} returns {@code converter1} while 142 * {@code converter1.concatenate(converter2).getConversionSteps()} returns {@code converter1, converter2}. 143 * </p> 144 * 145 * @return the list of fundamental converters which concatenated make up this converter. 146 */ 147 List<? extends UnitConverter> getConversionSteps(); 148}