/** * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super() call in * constructors"). * * @throws IncommensurableException */ private static UnitConverter getConverter(final Unit<Time> source) throws IncommensurableException { if (source == null) { throw new UnconvertibleException(Errors.format(ErrorKeys.NO_UNIT)); } return source.getConverterTo(MILLISECOND); }
@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); } }
/** * Tests {@link SystemUnit#getConverterTo(Unit)} with illegal arguments. * Those calls are not allowed by the Java compiler if parameterized types have not been erased. * But if the users nevertheless erased parameter types, {@link SystemUnit} implementation should * have some safety nets. */ @Test @SuppressWarnings("unchecked") @DependsOnMethod("testGetConverterTo") public void testIllegalGetConverterTo() { try { Units.METRE.getConverterTo((Unit) Units.SECOND); fail("Conversion should not have been allowed."); } catch (UnconvertibleException e) { final String message = e.getMessage(); assertTrue(message, message.contains("m")); // metre unit symbol assertTrue(message, message.contains("s")); // second unit symbol } /* * Following is tricker because "radian" and "unity" are compatible units, * but should nevertheless not be allowed by the 'getConverterTo' method. */ try { Units.RADIAN.getConverterTo((Unit) Units.UNITY); fail("Conversion should not have been allowed."); } catch (UnconvertibleException e) { final String message = e.getMessage(); assertTrue(message, message.contains("rad")); } }
@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); } }
public final UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException { if ((this == that) || this.equals(that)) return AbstractConverter.IDENTITY; // Shortcut. Unit<Q> thisSystemUnit = this.getSystemUnit(); Unit<Q> thatSystemUnit = that.getSystemUnit(); if (!thisSystemUnit.equals(thatSystemUnit)) try { return getConverterToAny(that); } catch (IncommensurableException e) { throw new UnconvertibleException(e); } UnitConverter thisToSI = this.getSystemConverter(); UnitConverter thatToSI = that.getConverterTo(thatSystemUnit); return thatToSI.inverse().concatenate(thisToSI); }
@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); } }
@Override public final UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException { if ((this == that) || this.equals(that)) return AbstractConverter.IDENTITY; // Shortcut. Unit<Q> thisSystemUnit = this.getSystemUnit(); Unit<Q> thatSystemUnit = that.getSystemUnit(); if (!thisSystemUnit.equals(thatSystemUnit)) try { return getConverterToAny(that); } catch (IncommensurableException e) { throw new UnconvertibleException(e); } UnitConverter thisToSI = this.getSystemConverter(); UnitConverter thatToSI = that.getConverterTo(thatSystemUnit); return thatToSI.inverse().concatenate(thisToSI); }
/** * @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); } }
private final UnitConverter internalGetConverterTo(Unit<Q> that, boolean useEquals) throws UnconvertibleException { if (useEquals) { if ((this == that) || this.equals(that)) return AbstractConverter.IDENTITY; } else { if (this == that) return AbstractConverter.IDENTITY; } Unit<Q> thisSystemUnit = this.getSystemUnit(); Unit<Q> thatSystemUnit = that.getSystemUnit(); if (!thisSystemUnit.equals(thatSystemUnit)) try { return getConverterToAny(that); } catch (IncommensurableException e) { throw new UnconvertibleException(e); } UnitConverter thisToSI = this.getSystemConverter(); UnitConverter thatToSI = that.getConverterTo(thatSystemUnit); return thatToSI.inverse().concatenate(thisToSI); }
/** * @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); } }
/** * Returns a converter of numeric values from this unit to another unit of same type. * * @param that the unit of same type to which to convert the numeric values. * @return the converter from this unit to {@code that} unit. * @throws UnconvertibleException if the converter can not be constructed. */ @Override public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException { ArgumentChecks.ensureNonNull("that", that); final Unit<Q> step = that.getSystemUnit(); if (step != this && !equalsIgnoreMetadata(step)) { // Should never occur unless parameterized type has been compromised. throw new UnconvertibleException(incompatible(that)); } if (step == that) { return LinearConverter.IDENTITY; } /* * At this point we know that the given units is not a system unit. Ask the conversion * FROM the given units (before to inverse it) instead than TO the given units because * in Apache SIS implementation, the former returns directly ConventionalUnit.toTarget * while the later implies a recursive call to this method. */ return that.getConverterTo(step).inverse(); }
@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); } }
/** * Returns a converter of numeric values from this unit to another unit of same type. * * @param unit the unit of same type to which to convert the numeric values. * @return the converter from this unit to {@code that} unit. * @throws UnconvertibleException if the converter can not be constructed. */ @Override public UnitConverter getConverterTo(final Unit<Q> unit) throws UnconvertibleException { ArgumentChecks.ensureNonNull("unit", unit); final Unit<Q> step = unit.getSystemUnit(); if (step != this && !equalsIgnoreMetadata(step)) { // Should never occur unless parameterized type has been compromised. throw new UnconvertibleException(incompatible(unit)); } if (step == unit) { return LinearConverter.IDENTITY; } /* * At this point we know that the given units is not a system unit. Ask the conversion * FROM the given units (before to inverse it) instead than TO the given units because * in Apache SIS implementation, the former returns directly ConventionalUnit.toTarget * while the later implies a recursive call to this method. */ return unit.getConverterTo(step).inverse(); }
/** * @since 1.0.1 */ @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); } }
/** * Returns a converter of numeric values from this unit to another unit of same type. * * @param that the unit of same type to which to convert the numeric values. * @return the converter from this unit to {@code that} unit. * @throws UnconvertibleException if the converter can not be constructed. */ @Override public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException { if (that == this) { return LinearConverter.IDENTITY; } ArgumentChecks.ensureNonNull("that", that); UnitConverter c = toTarget; if (target != that) { // Optimization for a common case. final Unit<Q> step = that.getSystemUnit(); if (target != step && !target.isCompatible(step)) { // Should never occur unless parameterized type has been compromised. throw new UnconvertibleException(incompatible(that)); } c = target.getConverterTo(step).concatenate(c); // Usually leave 'c' unchanged. c = step.getConverterTo(that).concatenate(c); } return c; }
/** * @since 1.0.1 */ @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); } }
/** * Returns a converter of numeric values from this unit to another unit of same type. * * @param that the unit of same type to which to convert the numeric values. * @return the converter from this unit to {@code that} unit. * @throws UnconvertibleException if the converter can not be constructed. */ @Override public UnitConverter getConverterTo(final Unit<Q> that) throws UnconvertibleException { if (that == this) { return LinearConverter.IDENTITY; } ArgumentChecks.ensureNonNull("that", that); UnitConverter c = toTarget; if (target != that) { // Optimization for a common case. final Unit<Q> step = that.getSystemUnit(); if (target != step && !target.isCompatible(step)) { // Should never occur unless parameterized type has been compromised. throw new UnconvertibleException(incompatible(that)); } c = target.getConverterTo(step).concatenate(c); // Usually leave 'c' unchanged. c = step.getConverterTo(that).concatenate(c); } return c; }