@Override public int hashCode() { if (unit instanceof TransformedUnit<?>) { Unit<?> systemUnit = unit.getSystemUnit(); try { float factor1 = (float) unit.getConverterToAny(systemUnit).convert(1.0); return Objects.hash(systemUnit, Float.floatToIntBits(factor1)); } catch (UnconvertibleException | IncommensurableException e) { } catch (Throwable e) { } } return unit.hashCode(); }
@Override public int compareTo(QuantityType<T> o) { if (quantity.getUnit().isCompatible(o.quantity.getUnit())) { QuantityType<T> v1 = this.toUnit(getUnit().getSystemUnit()); QuantityType<?> v2 = o.toUnit(o.getUnit().getSystemUnit()); if (v1 != null && v2 != null) { return Double.compare(v1.doubleValue(), v2.doubleValue()); } else { throw new IllegalArgumentException("Unable to convert to system unit during compare."); } } else { throw new IllegalArgumentException("Can not compare incompatible units."); } }
TransformedUnit<?> tunit1 = (TransformedUnit<?>) unit1; TransformedUnit<?> tunit2 = (TransformedUnit<?>) unit2; if (unit1.getSystemUnit().equals(unit2.getSystemUnit())) { try { float factor =
public static final boolean isUSSurveyFoot(Unit<?> unit) { if (unit == null) { return false; } else if (USCustomary.FOOT_SURVEY.equals(unit)) { return true; } else if (unit.getSystemUnit() == SI.METRE && unit instanceof TransformedUnit<?>) { @SuppressWarnings("unchecked") TransformedUnit<Length> transformed = (TransformedUnit<Length>) unit; UnitConverter converter = transformed.getConverter(); if (converter instanceof MultiplyConverter) { MultiplyConverter multiplyConverter = (MultiplyConverter) converter; double factor = multiplyConverter.getFactor(); // 0.3048006096012192 // observed // 0.3048006096 // expected if (Math.abs(US_SURVEY_FOOT_FACTORY - factor) < US_SURVEY_FOOT_COMPARISON_EPSILON) { return true; } } } return false; }
/** * Recognize representation of NonSI.DEEGREE_ANGLE to prevent unnecessary conversion. * * @param unit * @return true if MultiplyConverter is close to PI/180.0 */ public static final boolean isDegreeAngle(Unit<?> unit) { if (unit == null) { return false; } else if (NonSI.DEGREE_ANGLE.equals(unit)) { return true; } if (unit.getSystemUnit() == SI.RADIAN && unit instanceof TransformedUnit<?>) { @SuppressWarnings("unchecked") TransformedUnit<Angle> transformed = (TransformedUnit<Angle>) unit; UnitConverter converter = transformed.getConverter(); if (converter instanceof MultiplyConverter) { MultiplyConverter multiplyConverter = (MultiplyConverter) converter; double factor = multiplyConverter.getFactor(); if (Math.abs(RADIAN_TO_DEGREE_RATIO - factor) < DEEGREE_RATIO_COMPARISON_EPSILON) { return true; } } } return false; } }
Unit<?> itemUnit = getUnit(); Unit<?> stateUnit = ((QuantityType<?>) state).getUnit(); if (itemUnit != null && (!stateUnit.getSystemUnit().equals(itemUnit.getSystemUnit()) || UnitUtils.isDifferentMeasurementSystem(itemUnit, stateUnit))) { QuantityType<?> convertedState = ((QuantityType<?>) state).toUnit(itemUnit);
/** * Creates a transformed unit from the specified parent unit. * * @param symbol * the symbol to use with this transformed unit. * @param parentUnit * the parent unit from which this unit is derived. * @param unitConverter * the converter to the parent units. */ public TransformedUnit(String symbol, Unit<Q> parentUnit, UnitConverter unitConverter) { this(symbol, parentUnit, parentUnit.getSystemUnit(), unitConverter); }
@Override public AbstractUnit<Q> toSystemUnit() { return (AbstractUnit<Q>) actualUnit.getSystemUnit(); }
/** * Returns {@code true} if the given unit is a pressure unit. * Pressure units are convertible to {@link #PASCAL}. * Those units are sometime used instead of linear units for altitude measurements. * * @param unit the unit to check (may be {@code null}). * @return {@code true} if the given unit is non-null and a pressure unit. */ public static boolean isPressure(final Unit<?> unit) { return (unit != null) && unit.getSystemUnit().equals(PASCAL); }
/** * Returns {@code true} if the given unit is a temporal unit. * Temporal units are convertible to {@link #SECOND}. * * @param unit the unit to check (may be {@code null}). * @return {@code true} if the given unit is non-null and temporal. * * @see #ensureTemporal(Unit) */ public static boolean isTemporal(final Unit<?> unit) { return (unit != null) && unit.getSystemUnit().equals(SECOND); }
/** * Returns {@code true} if the given unit is a pressure unit. * Pressure units are convertible to {@link #PASCAL}. * Those units are sometime used instead of linear units for altitude measurements. * * @param unit the unit to check (may be {@code null}). * @return {@code true} if the given unit is non-null and a pressure unit. */ public static boolean isPressure(final Unit<?> unit) { return (unit != null) && unit.getSystemUnit().equals(PASCAL); }
/** * Returns {@code true} if the given unit is a temporal unit. * Temporal units are convertible to {@link #SECOND}. * * @param unit the unit to check (may be {@code null}). * @return {@code true} if the given unit is non-null and temporal. * * @see #ensureTemporal(Unit) */ public static boolean isTemporal(final Unit<?> unit) { return (unit != null) && unit.getSystemUnit().equals(SECOND); }
/** * Returns {@code true} if the given unit is a dimensionless scale unit. * This include {@link #UNITY} and {@link #PPM}. * * @param unit the unit to check (may be {@code null}). * @return {@code true} if the given unit is non-null and a dimensionless scale. * * @see #UNITY * @see #ensureScale(Unit) */ public static boolean isScale(final Unit<?> unit) { return (unit != null) && unit.getSystemUnit().equals(UNITY); }
/** * Convenient method equivalent to {@link #to(javax.measure.unit.Unit) to(this.getUnit().toSI())}. * * @return this measure or a new measure equivalent to this measure but stated in SI units. * @throws ArithmeticException * if the result is inexact and the quotient has a non-terminating decimal expansion. */ public Quantity<Q> toSI() { return to(this.getUnit().getSystemUnit()); }
/** * Convenient method equivalent to {@link #to(javax.measure.unit.Unit) to(this.getUnit().toSI())}. * * @return this measure or a new measure equivalent to this measure but stated in SI units. * @throws ArithmeticException * if the result is inexact and the quotient has a non-terminating decimal expansion. */ public Quantity<Q> toSI() { return to(this.getUnit().getSystemUnit()); }
/** * Convenient method equivalent to {@link #to(javax.measure.unit.Unit) to(this.getUnit().toSI())}. * * @return this measure or a new measure equivalent to this measure but stated in SI units. * @throws ArithmeticException * if the result is inexact and the quotient has a non-terminating decimal expansion. */ public Quantity<Q> toSI() { return to(this.getUnit().getSystemUnit()); }
@SuppressWarnings("unchecked") @Override public Unit<Q> toSystemUnit() { Unit<?> systemUnit = AbstractUnit.ONE; for (Element element : elements) { Unit<?> unit = element.unit.getSystemUnit(); unit = unit.pow(element.pow); unit = unit.root(element.root); systemUnit = systemUnit.multiply(unit); } return (AbstractUnit<Q>) systemUnit; }
@SuppressWarnings("unchecked") @Override public AbstractUnit<Q> toSystemUnit() { Unit<?> systemUnit = AbstractUnit.ONE; for (Element element : elements) { Unit<?> unit = element.unit.getSystemUnit(); unit = unit.pow(element.pow); unit = unit.root(element.root); systemUnit = systemUnit.multiply(unit); } return (AbstractUnit<Q>) systemUnit; }
@SuppressWarnings("unchecked") @Override public AbstractUnit<Q> toSystemUnit() { Unit<?> systemUnit = AbstractUnit.ONE; for (Element element : elements) { Unit<?> unit = element.unit.getSystemUnit(); unit = unit.pow(element.pow); unit = unit.root(element.root); systemUnit = systemUnit.multiply(unit); } return (AbstractUnit<Q>) systemUnit; }
/** * Returns the converter to standard unit. */ private static <Q extends Quantity<Q>> UnitConverter toStandardUnit(final Unit<Q> unit) { return unit.getConverterTo(unit.getSystemUnit()); } }