/** * Gets a UnitConverter between two units, wrapping any raised exception in a * IllegalArgumentException. * * @param fromUnit * @param toUnit * @return * @throws IllegalArgumentException if unit1 can't be converter to unit2 */ public static UnitConverter getConverterToAny(Unit<?> fromUnit, Unit<?> toUnit) { try { return fromUnit.getConverterToAny(toUnit); } catch (UnconvertibleException | IncommensurableException e) { throw new IllegalArgumentException("Can't convert to the candidate unit", e); } }
@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(); }
longitudeAxis = i; longitudeConverter = unit.getConverterToAny(NonSI.DEGREE_ANGLE); latitudeConverter = unit.getConverterToAny(NonSI.DEGREE_ANGLE); continue; heightConverter = unit.getConverterToAny(SI.METRE); continue;
append(unit.getConverterToAny(base).convert(1));
/** * Convert this QuantityType to a new {@link QuantityType} using the given target unit. * * @param targetUnit the unit to which this {@link QuantityType} will be converted to. * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a */ @SuppressWarnings("unchecked") public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) { if (!targetUnit.equals(getUnit())) { try { UnitConverter uc = getUnit().getConverterToAny(targetUnit); Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit); return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit); } catch (UnconvertibleException | IncommensurableException e) { logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit); return null; } } return this; }
UnitConverter fromDegrees; try { fromDegrees = NonSI.DEGREE_ANGLE.getConverterToAny(unit); } catch (UnconvertibleException | IncommensurableException e) { throw new IllegalArgumentException(
converter = units.getConverterToAny(targetUnits); } catch (UnconvertibleException | IncommensurableException e) { throw new IllegalArgumentException(e);
for (int i = 0; i < converters.length; i++) { try { converters[i] = getAxis(i).getUnit().getConverterToAny(unit); } catch (UnconvertibleException | IndexOutOfBoundsException
/** * Returns the converter from the given source unit (which may be {@code null}) to the * given target unit, or {@code null} if none or incompatible. */ private UnitConverter getConverterTo(final Unit<?> source, final Unit<?> target) { if (source != null) try { return source.getConverterToAny(target); } catch (IncommensurableException e) { warning(e); } return null; }
/** * Converts the given angle from degrees to the angular units used by this axis. * * @param angle the angle to convert. * @return the converted angle. * @throws IllegalStateException if this axis does not use angular units. */ private double toAngularUnit(final double angle) throws IllegalStateException { try { return DEGREE.getConverterToAny(unit).convert(angle); } catch (IncommensurableException e) { throw new IllegalStateException(e); } }
/** * Sets the parameter to the given value, after conversion to metres or decimal degrees. */ @Override public void setValue(double value, final Unit<?> unit) { final Unit<?> standardUnit = getStandardUnit(unit); if (standardUnit != null) try { value = unit.getConverterToAny(standardUnit).convert(value); } catch (IncommensurableException e) { throw new IllegalArgumentException(e); // Should never happen actually. } this.value = value; }
/** * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super() * call in constructors"). */ private static UnitConverter getConverter(final Unit<?> source) throws IncommensurableException { if (source == null) { throw new IncommensurableException(Errors.format(Errors.Keys.NoUnit)); } return source.getConverterToAny(Units.MILLISECOND); }
/** * Gets the unit given by {@link #getIdentifiedObject()}, * then creates and returns the converter from that unit to the base unit. * * @throws FactoryException if an error occurred while creating the unit from the EPSG code. */ private UnitConverter createConverter() throws FactoryException { final Unit<?> unit = getIdentifiedObject(); assertNotNull("Unit", unit); final UnitConverter converter; try { converter = unit.getConverterToAny(baseUnit); } catch (IncommensurableException e) { throw new AssertionError("Can not convert “" + name + "” from “" + unit + "” to “" + baseUnit + "”.", e); } return converter; }
/** * Returns the numeric value of the operation parameter in the specified unit of measure. * This convenience method applies unit conversion from metres or decimal degrees as needed. * * @param unit the unit of measure for the value to be returned. * @return the numeric value represented by this parameter after conversion to {@code unit}. */ @Override public double doubleValue(final Unit<?> unit) { double c = value; final Unit<?> standardUnit = getStandardUnit(unit); if (standardUnit != null) try { c = standardUnit.getConverterToAny(unit).convert(c); } catch (IncommensurableException e) { throw new IllegalArgumentException(e); // Should never happen actually. } return c; }
/** * Returns the converter to be used by {@link #doubleValue(Unit)} and {@link #doubleValueList(Unit)}. */ private UnitConverter getConverterTo(final Unit<?> unit) { final Unit<?> source = getUnit(); if (source == null) { throw new IllegalStateException(Resources.format(Resources.Keys.UnitlessParameter_1, Verifier.getDisplayName(descriptor))); } ensureNonNull("unit", unit); final short expectedID = Verifier.getUnitMessageID(source); if (Verifier.getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } try { return source.getConverterToAny(unit); } catch (IncommensurableException e) { throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, source, unit), e); } }
/** * Returns the converter to be used by {@link #doubleValue(Unit)} and {@link #doubleValueList(Unit)}. */ private UnitConverter getConverterTo(final Unit<?> unit) { final Unit<?> source = getUnit(); if (source == null) { throw new IllegalStateException(Resources.format(Resources.Keys.UnitlessParameter_1, Verifier.getDisplayName(descriptor))); } ensureNonNull("unit", unit); final short expectedID = Verifier.getUnitMessageID(source); if (Verifier.getUnitMessageID(unit) != expectedID) { throw new IllegalArgumentException(Errors.format(expectedID, unit)); } try { return source.getConverterToAny(unit); } catch (IncommensurableException e) { throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2, source, unit), e); } }
/** * Verifies the conversion factory of {@link Units#PSU}. * * @see <a href="https://issues.apache.org/jira/browse/SIS-413">SIS-413</a> * * @throws IncommensurableException if the conversion can not be applied. */ @Test public void testSalinityConversionFactor() throws IncommensurableException { assertEquals(0.001, PSU.getConverterToAny(UNITY) .convert(1), STRICT); assertEquals(0.1, PSU.getConverterToAny(PERCENT).convert(1), STRICT); }
@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); } }
@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); } }
/** * Verifies that the given units derived from litres ({@code u1}) is equivalent to the given units derived * from cubic metres ({@code u2}). The conversion between those two units is expected to be identity. */ private static void assertEquivalent(final String s1, final Unit<Volume> u1, final String s2, final Unit<Volume> u2) throws IncommensurableException { assertEquals("unit1.symbol", s1, u1.getSymbol()); assertEquals("unit2.symbol", s2, u2.getSymbol()); assertTrue("getConverterTo(…).isIdentity", u1.getConverterTo(u2).isIdentity()); assertTrue("getConverterTo(…).isIdentity", u2.getConverterTo(u1).isIdentity()); assertTrue("getConverterTo(…).isIdentity", u1.getConverterToAny(u2).isIdentity()); assertTrue("getConverterTo(…).isIdentity", u2.getConverterToAny(u1).isIdentity()); }