tec.units.ri.quantity.NumberQuantity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unit-ri Show documentation
Show all versions of unit-ri Show documentation
Unit Standard (JSR 363) Reference Implementation.
/*
* Units of Measurement Reference Implementation
* Copyright (c) 2005-2016, 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.quantity;
import javax.measure.Quantity;
import javax.measure.Unit;
import javax.measure.UnconvertibleException;
import javax.measure.UnitConverter;
import tec.units.ri.AbstractQuantity;
import tec.units.ri.format.QuantityFormat;
/**
* An amount of quantity, consisting of a Number and a Unit. NumberQuantity objects are immutable.
*
* @see AbstractQuantity
* @see Quantity
* @author Werner Keil
* @param
* The type of the quantity.
* @version 0.13, $Date: 2016-06-21 $
*/
public class NumberQuantity> extends AbstractQuantity {
/**
*
*/
// private static final long serialVersionUID = 7312161895652321241L;
private final Number value;
/*
* (non-Javadoc)
*
* @see AbstractQuantity#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (this.getClass() == obj.getClass()) {
return super.equals(obj);
} else {
if (obj instanceof Quantity) {
@SuppressWarnings("rawtypes")
Quantity m = (Quantity) obj;
if (m.getValue().getClass() == this.getValue().getClass() && m.getUnit().getClass() == this.getUnit().getClass()) {
return super.equals(obj);
} else {
// if (this.getQuantityUnit() instanceof AbstractUnit>) {
// if
// }
return super.equals(obj);
}
}
return false;
}
}
/**
* Indicates if this quantity is exact.
*/
private final boolean isExact;
/**
* Holds the exact value (when exact) stated in this quantity's unit.
*/
// private long exactValue;
/**
* Holds the minimum value stated in this quantity's unit. For inexact measures: minimum < maximum
*/
// private double minimum;
/**
* Holds the maximum value stated in this quantity's unit. For inexact measures: maximum > minimum
*/
// private double maximum;
public NumberQuantity(Number number, Unit unit) {
super(unit);
value = number;
isExact = false;
}
/*
* (non-Javadoc)
*
* @see AbstractQuantity#doubleValue(javax.measure.Unit)
*/
@Override
public double doubleValue(Unit unit) {
Unit myUnit = getUnit();
try {
UnitConverter converter = unit.getConverterTo(myUnit);
return converter.convert(getValue().doubleValue());
} catch (UnconvertibleException e) {
throw e;
}
}
protected final int intValue(Unit unit) throws ArithmeticException {
long longValue = longValue(unit);
if ((longValue < Integer.MIN_VALUE) || (longValue > Integer.MAX_VALUE)) {
throw new ArithmeticException("Cannot convert " + longValue + " to int (overflow)");
}
return (int) longValue;
}
/*
* (non-Javadoc)
*
* @see javax.measure.Quantity#getValue()
*/
public Number getValue() {
return value;
}
/**
* Indicates if this measured quantity is exact. An exact quantity is guaranteed exact only when stated in this quantity's unit (e.g.
* this.longValue()
); stating the quantity in any other unit may introduce conversion errors.
*
* @return true
if this quantity is exact; false
otherwise.
*/
public boolean isExact() {
return isExact;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Quantity add(AbstractQuantity that) {
final Quantity thatToUnit = that.to(getUnit());
return new NumberQuantity(this.getValue().doubleValue() + thatToUnit.getValue().doubleValue(), getUnit());
}
public String toString() {
return String.valueOf(getValue()) + " " + String.valueOf(getUnit());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Quantity> multiply(Quantity> that) {
final Unit> unit = getUnit().multiply(that.getUnit());
return new NumberQuantity((getValue().doubleValue() * that.getValue().doubleValue()), unit);
}
public Quantity multiply(Number that) {
return (NumberQuantity) NumberQuantity.of((getValue().doubleValue() * that.doubleValue()), getUnit());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Quantity divide(Quantity> that) {
final Unit> unit = getUnit().divide(that.getUnit());
return new NumberQuantity((getValue().doubleValue() / that.getValue().doubleValue()), unit);
}
public Quantity divide(Number that) {
return NumberQuantity.of(getValue().doubleValue() / that.doubleValue(), getUnit());
}
public Quantity inverse() {
@SuppressWarnings({ "rawtypes", "unchecked" })
final Quantity m = new NumberQuantity(1d / getValue().doubleValue(), getUnit().inverse());
return m;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Quantity subtract(Quantity that) {
final Quantity thatToUnit = (Quantity) that.to(getUnit());
return new NumberQuantity(this.getValue().doubleValue() - thatToUnit.getValue().doubleValue(), getUnit());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Quantity add(Quantity that) {
final Quantity thatToUnit = (Quantity) that.to(getUnit());
return new NumberQuantity(this.getValue().doubleValue() + thatToUnit.getValue().doubleValue(), getUnit());
}
/**
* Returns the scalar quantity for the specified long
stated in the specified unit.
*
* @param longValue
* the quantity value.
* @param unit
* the measurement unit.
* @return the corresponding int
quantity.
*/
public static > AbstractQuantity of(long longValue, Unit unit) {
return new LongQuantity(longValue, unit);
}
/**
* Returns the scalar quantity for the specified int
stated in the specified unit.
*
* @param intValue
* the quantity value.
* @param unit
* the measurement unit.
* @return the corresponding int
quantity.
*/
public static > AbstractQuantity of(int intValue, Unit unit) {
return new IntegerQuantity(intValue, unit);
}
/**
* Returns the scalar quantity for the specified short
stated in the specified unit.
*
* @param value
* the quantity value.
* @param unit
* the measurement unit.
* @return the corresponding short
quantity.
*/
public static > AbstractQuantity of(short value, Unit unit) {
return new ShortQuantity(value, unit);
}
/**
* Returns the scalar quantity for the specified float
stated in the specified unit.
*
* @param floatValue
* the measurement value.
* @param unit
* the measurement unit.
* @return the corresponding float
quantity.
*/
public static > AbstractQuantity of(float floatValue, Unit unit) {
return new FloatQuantity(floatValue, unit);
}
/**
* Returns the scalar quantity for the specified double
stated in the specified unit.
*
* @param doubleValue
* the measurement value.
* @param unit
* the measurement unit.
* @return the corresponding double
quantity.
*/
public static > AbstractQuantity of(double doubleValue, Unit unit) {
return new DoubleQuantity(doubleValue, unit);
}
/**
* Returns the decimal quantity of unknown type corresponding to the specified representation. This method can be used to parse dimensionless
* quantities.
*
* Quatity proportion = AbstractQuantity.of("0.234").asType(Dimensionless.class);
*
*
*
* Note: This method handles only {@link tec.units.ri.SimpleUnitFormat.UnitFormat#getStandard standard} unit format (UCUM based). Locale-sensitive quantity formatting and parsing are handled by the {@link MeasurementFormat}
* class and its subclasses.
*
*
* @param csq
* the decimal value and its unit (if any) separated by space(s).
* @return QuantityFormat.getInstance().parse(csq)
*/
public static Quantity> parse(CharSequence csq) {
return QuantityFormat.getInstance().parse(csq);
}
}