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

com.anrisoftware.globalpom.measurement.AbstractMeasure Maven / Gradle / Ivy

/*
 * Copyright 2016 Erwin Müller 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.anrisoftware.globalpom.measurement;

import java.io.Serializable;
import java.math.BigInteger;

import javax.measure.quantity.Quantity;
import javax.measure.unit.Unit;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * Defines a measurement with a physical unit.
 *
 * @param 
 *            the {@link Quantity} of the unit.
 *
 * @author Erwin Mueller, [email protected]
 * @since 2.4
 */
@SuppressWarnings({ "serial", "unchecked" })
public abstract class AbstractMeasure implements
        Measure, Serializable {

    private final Unit unit;

    private final Value value;

    private transient ValueFactory valueFactory;

    private transient MeasureFactory measureFactory;

    /**
     * @see MeasureFactory#create(Measure, ValueFactory)
     */
    protected AbstractMeasure(Measure measure,
            ValueFactory valueFactory, MeasureFactory measureFactory) {
        this(measure.getUnit(), measure.getMeasureValue(), valueFactory,
                measureFactory);
    }

    /**
     * @see MeasureFactory#create(Value, Unit, ValueFactory)
     */
    protected AbstractMeasure(Unit unit, Value value,
            ValueFactory valueFactory, MeasureFactory measureFactory) {
        this.unit = unit;
        this.valueFactory = valueFactory;
        this.measureFactory = measureFactory;
        this.value = valueFactory.create(value, valueFactory);
    }

    public void setValueFactory(ValueFactory factory) {
        this.valueFactory = factory;
    }

    public void setMeasureFactory(MeasureFactory factory) {
        this.measureFactory = factory;
    }

    @Override
    public Unit getUnit() {
        return unit;
    }

    @Override
    public Value getMeasureValue() {
        return value;
    }

    @Override
    public BigInteger getMantissa() {
        return value.getMantissa();
    }

    @Override
    public int getOrder() {
        return value.getOrder();
    }

    @Override
    public int getSignificant() {
        return value.getSignificant();
    }

    @Override
    public int getDecimal() {
        return value.getDecimal();
    }

    @Override
    public double getUncertainty() {
        return value.getUncertainty();
    }

    @Override
    public double getRoundedUncertainty() {
        return value.getRoundedUncertainty();
    }

    @Override
    public boolean isExact() {
        return value.isExact();
    }

    @Override
    public double getValue() {
        return value.getValue();
    }

    @Override
    public double getRoundedValue() {
        return value.getRoundedValue();
    }

    @Override
    public double roundedValue(int sig, int dec) {
        return value.roundedValue(sig, dec);
    }

    @Override
    public Measure getMinValue() {
        Value value = this.value.getMinValue();
        return measureFactory.create(value, getUnit(), valueFactory);
    }

    @Override
    public Measure minValue(double deviation) {
        Value value = this.value.minValue(deviation);
        return measureFactory.create(value, getUnit(), valueFactory);
    }

    @Override
    public Measure getMaxValue() {
        Value value = this.value.getMaxValue();
        return measureFactory.create(value, getUnit(), valueFactory);
    }

    @Override
    public Measure maxValue(double deviation) {
        Value value = this.value.maxValue(deviation);
        return measureFactory.create(value, getUnit(), valueFactory);
    }

    @Override
    public Measure valueOf(BigInteger mantissa, int order, int sig,
            int dec, double unc) {
        Value value = this.value.valueOf(mantissa, order, sig, dec, unc);
        return measureFactory.create(value, getUnit(), valueFactory);
    }

    @Override
    public Measure valueOf(double value, int sig, int dec, double unc) {
        Value vvalue = this.value.valueOf(value, sig, dec, unc);
        return measureFactory.create(vvalue, getUnit(), valueFactory);
    }

    @Override
    public Measure valueOf(double value, int dec, double unc) {
        Value vvalue = this.value.valueOf(value, dec, unc);
        return measureFactory.create(vvalue, getUnit(), valueFactory);
    }

    @Override
    public Measure add(Value addend) {
        Value value = this.value.add(addend);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure add(double addend) {
        Value value = this.value.add(addend);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure plus(Value addend) {
        return add(addend);
    }

    @Override
    public Measure plus(double addend) {
        return add(addend);
    }

    @Override
    public Measure sub(Value subtrahend) {
        Value value = this.value.sub(subtrahend);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure sub(double subtrahend) {
        Value value = this.value.sub(subtrahend);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure minus(Value subtrahend) {
        return sub(subtrahend);
    }

    @Override
    public Measure minus(double subtrahend) {
        return sub(subtrahend);
    }

    @Override
    public Measure mul(Value factor) {
        Value value = this.value.mul(factor);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure mul(double factor) {
        Value value = this.value.mul(factor);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure multiply(Value factor) {
        return mul(factor);
    }

    @Override
    public Measure multiply(double factor) {
        return mul(factor);
    }

    @Override
    public Measure div(Value divisor) {
        Value value = this.value.div(divisor);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure div(double divisor) {
        Value value = this.value.div(divisor);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure divNum(Value numerator) {
        Value value = this.value.divNum(numerator);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure divNum(double numerator) {
        Value value = this.value.divNum(numerator);
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure log() {
        Value value = this.value.log();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure exp() {
        Value value = this.value.exp();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure reciprocal() {
        Value value = this.value.reciprocal();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure abs() {
        Value value = this.value.abs();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure square() {
        Value value = this.value.square();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public Measure cube() {
        Value value = this.value.cube();
        return measureFactory.create(value, unit, valueFactory);
    }

    @Override
    public int compareTo(Value v) {
        return value.compareTo(v);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Measure)) {
            return false;
        }
        @SuppressWarnings("rawtypes")
        Measure rhs = (Measure) obj;
        if (getUnit().equals(rhs.getUnit())) {
            return value.equals(obj);
        } else {
            return false;
        }
    }

    @Override
    public boolean isConsistent(Value rhs) {
        @SuppressWarnings("rawtypes")
        Measure mrhs = (Measure) rhs;
        if (getUnit().equals(mrhs.getUnit())) {
            return value.isConsistent(rhs);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(getUnit()).append(value)
                .toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append(value.toString())
                .append(getUnit()).toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy