001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2023, 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.test;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import javax.measure.UnitConverter;
036
037/**
038 * <p>
039 * Test class for our {@link UnitConverter} implementations.
040 * </p>
041 *
042 * @author <a href="mailto:werner@uom.technology">Werner Keil</a>
043 * @version 0.8.1, $Date: 2016-02-11 $
044 */
045public abstract class TestConverter implements UnitConverter {
046
047    /**
048     * Holds identity converter.
049     */
050    public static final TestConverter IDENTITY = new Identity();
051
052    /**
053     * Default constructor.
054     */
055    protected TestConverter() {
056    }
057
058    /**
059     * Concatenates this physics converter with another unit converter. The resulting converter is equivalent to first converting by the specified
060     * converter (right converter), and then converting by this converter (left converter).
061     *
062     * @param that
063     *          the other converter.
064     * @return the concatenation of this converter with that converter.
065     */
066    public TestConverter concatenate(TestConverter that) {
067        return (IDENTITY.equals(that)) ? this : new Pair(this, that);
068    }
069
070    public boolean isIdentity() {
071        return false;
072    }
073
074    @Override
075    public abstract boolean equals(Object cvtr);
076
077    @Override
078    public abstract int hashCode();
079
080    @Override
081    public abstract TestConverter inverse();
082
083    @Override
084    public UnitConverter concatenate(UnitConverter converter) {
085        return (IDENTITY.equals(converter)) ? this : new Pair(this, converter);
086    }
087
088    @Override
089    public List<? extends UnitConverter> getConversionSteps() {
090        final List<TestConverter> steps = new ArrayList<TestConverter>();
091        steps.add(this);
092        return steps;
093    }
094
095    /**
096     * @throws IllegalArgumentException
097     *           if the value is <code>null</code>.
098     */
099    public Number convert(Number value) {
100        if (value != null) {
101            return convert(value.doubleValue());
102        } else {
103            throw new IllegalArgumentException("Value cannot be null");
104        }
105    }
106
107    public abstract double convert(double value);
108
109    /**
110     * This class represents the identity converter (singleton).
111     */
112    private static final class Identity extends TestConverter {
113
114        @Override
115        public boolean isIdentity() {
116            return true;
117        }
118
119        @Override
120        public Identity inverse() {
121            return this;
122        }
123
124        @Override
125        public double convert(double value) {
126            return value;
127        }
128
129        @Override
130        public UnitConverter concatenate(UnitConverter converter) {
131            return converter;
132        }
133
134        @Override
135        public boolean equals(Object cvtr) {
136            return (cvtr instanceof Identity);
137        }
138
139        @Override
140        public int hashCode() {
141            return 0;
142        }
143
144        public boolean isLinear() {
145            return true;
146        }
147    }
148
149    /**
150     * This class represents converters made up of two or more separate converters (in matrix notation <code>[pair] = [left] x [right]</code>).
151     */
152    public static final class Pair extends TestConverter {
153
154        /**
155         * Holds the first converter.
156         */
157        private final UnitConverter left;
158
159        /**
160         * Holds the second converter.
161         */
162        private final UnitConverter right;
163
164        /**
165         * Creates a compound converter resulting from the combined transformation of the specified converters.
166         *
167         * @param left
168         *          the left converter.
169         * @param right
170         *          the right converter.
171         * @throws IllegalArgumentException
172         *           if either the left or right converter are <code>null</code>
173         */
174        public Pair(UnitConverter left, UnitConverter right) {
175            this.left = left;
176            this.right = right;
177        }
178
179        public boolean isLinear() {
180            return left.isLinear() && right.isLinear();
181        }
182
183        @Override
184        public boolean isIdentity() {
185            return false;
186        }
187
188        @Override
189        public List<UnitConverter> getConversionSteps() {
190            final List<UnitConverter> steps = new ArrayList<UnitConverter>();
191            List<? extends UnitConverter> leftCompound = left.getConversionSteps();
192            List<? extends UnitConverter> rightCompound = right.getConversionSteps();
193            steps.addAll(leftCompound);
194            steps.addAll(rightCompound);
195            return steps;
196        }
197
198        @Override
199        public Pair inverse() {
200            return new Pair(right.inverse(), left.inverse());
201        }
202
203        @Override
204        public double convert(double value) {
205            return left.convert(right.convert(value));
206        }
207
208        @Override
209        public boolean equals(Object cvtr) {
210            if (this == cvtr)
211                return true;
212            if (!(cvtr instanceof Pair))
213                return false;
214            Pair that = (Pair) cvtr;
215            return (this.left.equals(that.left)) && (this.right.equals(that.right));
216        }
217
218        @Override
219        public int hashCode() {
220            return left.hashCode() + right.hashCode();
221        }
222
223        public UnitConverter getLeft() {
224            return left;
225        }
226
227        public UnitConverter getRight() {
228            return right;
229        }
230    }
231}