All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tec.units.ri.AbstractConverter Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*
 * Units of Measurement Reference Implementation
 * Copyright (c) 2005-2015, Jean-Marie Dautelle, Werner Keil, V2COM.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package tec.units.ri;

import java.util.ArrayList;
import java.util.List;

import javax.measure.UnitConverter;

/**
 * 

The base class for our {@link UnitConverter} implementations.

* * @author Werner Keil * @version 0.7.3, $Date: 2015-05-20 $ */ public abstract class AbstractConverter implements UnitConverter { /** * */ //private static final long serialVersionUID = 5790242858468427131L; /** * The ratio of the circumference of a circle to its diameter. **/ protected static final double PI = 3.1415926535897932384626433832795; /** * Holds identity converter. */ public static final AbstractConverter IDENTITY = new Identity(); /** * Default constructor. */ protected AbstractConverter() { } /** * Concatenates this physics converter with another unit converter. * The resulting converter is equivalent to first converting by the * specified converter (right converter), and then converting by * this converter (left converter). * * @param that the other converter. * @return the concatenation of this converter with that converter. */ public AbstractConverter concatenate(AbstractConverter that) { return (that == IDENTITY) ? this : new Pair(this, that); } @Override public boolean isIdentity() { return false; } @Override public abstract boolean equals(Object cvtr); @Override public abstract int hashCode(); @Override public abstract AbstractConverter inverse(); @Override public UnitConverter concatenate(UnitConverter converter) { return (converter == IDENTITY) ? this : new Pair(this, converter); } @Override public List getConversionSteps() { final List steps = new ArrayList(); steps.add(this); return steps; } @Override public Number convert(Number value) { return convert(value.doubleValue()); // TODO null check? } public abstract double convert(double value); /** * This class represents the identity converter (singleton). */ private static final class Identity extends AbstractConverter { @Override public boolean isIdentity() { return true; } @Override public Identity inverse() { return this; } @Override public double convert(double value) { return value; } @Override public UnitConverter concatenate(UnitConverter converter) { return converter; } @Override public boolean equals(Object cvtr) { return (cvtr instanceof Identity); } @Override public int hashCode() { return 0; } @Override public boolean isLinear() { return true; } } /** * This class represents converters made up of two or more separate * converters (in matrix notation [pair] = [left] x [right]). */ public static final class Pair extends AbstractConverter { /** * Holds the first converter. */ private final UnitConverter left; /** * Holds the second converter. */ private final UnitConverter right; /** * Creates a compound converter resulting from the combined * transformation of the specified converters. * * @param left the left converter. * @param right the right converter. */ public Pair(UnitConverter left, UnitConverter right) { this.left = left; this.right = right; } @Override public boolean isLinear() { return left.isLinear() && right.isLinear(); } @Override public boolean isIdentity() { return false; } @Override public List getConversionSteps() { final List steps = new ArrayList(); List leftCompound = left.getConversionSteps(); List rightCompound = right.getConversionSteps(); steps.addAll(leftCompound); steps.addAll(rightCompound); return steps; } @Override public Pair inverse() { return new Pair(right.inverse(), left.inverse()); } @Override public double convert(double value) { return left.convert(right.convert(value)); } @Override public boolean equals(Object cvtr) { if (this == cvtr) return true; if (!(cvtr instanceof Pair)) return false; Pair that = (Pair) cvtr; return (this.left.equals(that.left)) && (this.right.equals(that.right)); } @Override public int hashCode() { return left.hashCode() + right.hashCode(); } public UnitConverter getLeft() { return left; } public UnitConverter getRight() { return right; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy