use of javax.measure.UnitConverter in project indriya by unitsofmeasurement.
the class TemporalQuantity method multiply.
@Override
public ComparableQuantity<?> multiply(Quantity<?> multiplier) {
if (getUnit().equals(multiplier.getUnit())) {
return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
}
Unit<?> mulUnit = getUnit().multiply(multiplier.getUnit());
UnitConverter conv;
try {
conv = getUnit().getConverterToAny(mulUnit);
return TimeQuantities.getQuantity(value * conv.convert(multiplier.getValue()).intValue(), timeUnit);
} catch (UnconvertibleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
} catch (IncommensurableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
}
}
use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.
the class TimeUnitQuantity method divide.
/**
* @since 1.0.1
*/
@Override
public ComparableQuantity<?> divide(Quantity<?> that) {
if (getUnit().equals(that.getUnit())) {
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
Unit<?> divUnit = getUnit().divide(that.getUnit());
UnitConverter conv;
try {
conv = getUnit().getConverterToAny(divUnit);
return TimeQuantities.getQuantity(value / conv.convert(that.getValue()).intValue(), timeUnit);
} catch (UnconvertibleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
} catch (IncommensurableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
}
use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.
the class AbstractQuantity method to.
/**
* Returns this measure after conversion to specified unit. The default implementation returns <code>Measure.valueOf(doubleValue(unit), unit)</code>
* . If this measure is already stated in the specified unit, then this measure is returned and no conversion is performed.
*
* @param unit
* the unit in which the returned measure is stated.
* @return this measure or a new measure equivalent to this measure but stated in the specified unit.
* @throws ArithmeticException
* if the result is inexact and the quotient has a non-terminating decimal expansion.
*/
@Override
public ComparableQuantity<Q> to(Unit<Q> unit) {
if (unit.equals(this.getUnit())) {
return this;
}
UnitConverter t = getUnit().getConverterTo(unit);
Number convertedValue = t.convert(getValue());
return Quantities.getQuantity(convertedValue, unit);
}
use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.
the class TemporalQuantity method divide.
@Override
public ComparableQuantity<?> divide(Quantity<?> that) {
if (getUnit().equals(that.getUnit())) {
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
Unit<?> divUnit = getUnit().divide(that.getUnit());
UnitConverter conv;
try {
conv = getUnit().getConverterToAny(divUnit);
return TimeQuantities.getQuantity(value / conv.convert(that.getValue()).intValue(), timeUnit);
} catch (UnconvertibleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
} catch (IncommensurableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
}
use of javax.measure.UnitConverter in project sis by apache.
the class Verifier method ensureValidValue.
/**
* Ensures that the given value is valid according the specified parameter descriptor.
* This method ensures that {@code value} is assignable to the
* {@linkplain ParameterDescriptor#getValueClass() expected class}, is between the
* {@linkplain ParameterDescriptor#getMinimumValue() minimum} and
* {@linkplain ParameterDescriptor#getMaximumValue() maximum} values and is one of the
* {@linkplain ParameterDescriptor#getValidValues() set of valid values}.
* If the value fails any of those tests, then an exception is thrown.
*
* @param <T> the type of parameter value. The given {@code value} should typically be an instance of this class.
* This is not required by this method signature but is checked by this method implementation.
* @param descriptor the parameter descriptor to check against.
* @param value the value to check, or {@code null}.
* @param unit the unit of the value to check, or {@code null}.
* @return the given value converted to the descriptor unit if any,
* then casted to the descriptor parameterized type.
* @throws InvalidParameterValueException if the parameter value is invalid.
*/
static <T> T ensureValidValue(final ParameterDescriptor<T> descriptor, final Object value, final Unit<?> unit) throws InvalidParameterValueException {
final Class<T> valueClass = descriptor.getValueClass();
/*
* Before to verify if the given value is inside the bounds, we need to convert the value
* to the units used by the parameter descriptor. The first part of this block verifies
* the validity of the unit argument, so we execute it even if 'value' is null.
*/
UnitConverter converter = null;
Object convertedValue = value;
if (unit != null) {
Unit<?> def = descriptor.getUnit();
if (def == null) {
def = getCompatibleUnit(Parameters.getValueDomain(descriptor), unit);
if (def == null) {
final String name = getDisplayName(descriptor);
throw new InvalidParameterValueException(Resources.format(Resources.Keys.UnitlessParameter_1, name), name, unit);
}
}
if (!unit.equals(def)) {
final short expectedID = getUnitMessageID(def);
if (getUnitMessageID(unit) != expectedID) {
throw new IllegalArgumentException(Errors.format(expectedID, unit));
}
/*
* Verify the type of the user's value before to perform the unit conversion,
* because the conversion will create a new object not necessarily of the same type.
*/
if (value != null) {
if (!valueClass.isInstance(value)) {
final String name = getDisplayName(descriptor);
throw new InvalidParameterValueException(Resources.format(Resources.Keys.IllegalParameterValueClass_3, name, valueClass, value.getClass()), name, value);
}
/*
* From this point we will perform the actual unit conversion. The value may be either
* a Number instance, or an array of numbers (typically an array of type double[]). In
* the array case, we will store the converted values in a new array of the same type.
*/
try {
converter = unit.getConverterToAny(def);
} catch (IncommensurableException e) {
throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, unit, def), e);
}
Class<?> componentType = valueClass.getComponentType();
if (componentType == null) {
/*
* Usual case where the value is not an array. Convert the value directly.
* Note that the value can only be a number because the unit is associated
* to MeasurementRange, which accepts only numbers.
*/
Number n = converter.convert(((Number) value).doubleValue());
try {
convertedValue = Numbers.cast(n, valueClass.asSubclass(Number.class));
} catch (IllegalArgumentException e) {
throw new InvalidParameterValueException(e.getLocalizedMessage(), getDisplayName(descriptor), value);
}
} else {
/*
* The value is an array. Creates a new array and store the converted values
* using Array reflection.
*/
final int length = Array.getLength(value);
convertedValue = Array.newInstance(componentType, length);
componentType = Numbers.primitiveToWrapper(componentType);
for (int i = 0; i < length; i++) {
Number n = (Number) Array.get(value, i);
// Value in units that we can compare.
n = converter.convert(n.doubleValue());
try {
n = Numbers.cast(n, componentType.asSubclass(Number.class));
} catch (IllegalArgumentException e) {
throw new InvalidParameterValueException(e.getLocalizedMessage(), getDisplayName(descriptor) + '[' + i + ']', value);
}
Array.set(convertedValue, i, n);
}
}
}
}
}
/*
* At this point the user's value has been fully converted to the unit of measurement specified
* by the ParameterDescriptor. Now compare the converted value to the restriction given by the
* descriptor (set of valid values and range of value domain).
*/
if (convertedValue != null) {
final Verifier error;
final Set<T> validValues = descriptor.getValidValues();
if (descriptor instanceof DefaultParameterDescriptor<?>) {
error = ensureValidValue(valueClass, validValues, ((DefaultParameterDescriptor<?>) descriptor).getValueDomain(), convertedValue);
} else {
error = ensureValidValue(valueClass, validValues, descriptor.getMinimumValue(), descriptor.getMaximumValue(), convertedValue);
}
/*
* If we found an error, we will usually throw an exception. An exception to this rule is
* when EPSGDataAccess is creating a deprecated ProjectedCRS in which some parameters are
* known to be invalid (the CRS was deprecated precisely for that reason). In such cases,
* we will log a warning instead than throwing an exception.
*/
if (error != null) {
error.convertRange(converter);
final String name = getDisplayName(descriptor);
final String message = error.message(null, name, value);
if (!Semaphores.query(Semaphores.SUSPEND_PARAMETER_CHECK)) {
throw new InvalidParameterValueException(message, name, value);
} else {
final LogRecord record = new LogRecord(Level.WARNING, message);
record.setLoggerName(Loggers.COORDINATE_OPERATION);
Logging.log(DefaultParameterValue.class, "setValue", record);
}
}
}
return valueClass.cast(convertedValue);
}
Aggregations