UnitUtils { public static boolean isDifferentMeasurementSystem(Unit<? extends Quantity<?>> thisUnit, Unit<?> thatUnit) { Set<? extends Unit<?>> siUnits = SIUnits.getInstance().getUnits(); Set<? extends Unit<?>> usUnits = ImperialUnits.getInstance().getUnits(); boolean differentSystems = (siUnits.contains(thisUnit) && usUnits.contains(thatUnit)) || (siUnits.contains(thatUnit) && usUnits.contains(thisUnit)); if (!differentSystems) { if (thisUnit instanceof TransformedUnit && isMetricConversion(((TransformedUnit<?>) thisUnit).getConverter())) { return isDifferentMeasurementSystem(((TransformedUnit<?>) thisUnit).getParentUnit(), thatUnit); } if (thatUnit instanceof TransformedUnit && isMetricConversion(((TransformedUnit<?>) thatUnit).getConverter())) { return isDifferentMeasurementSystem(thisUnit, ((TransformedUnit<?>) thatUnit).getParentUnit()); } } if (!differentSystems) { Set<String> siSymbols = siUnits.stream().map(Unit::getSymbol).collect(toSet()); Set<String> usSymbols = usUnits.stream().map(Unit::getSymbol).collect(toSet()); differentSystems = (siSymbols.contains(thisUnit.getSymbol()) && usSymbols.contains(thatUnit.getSymbol())) || (siSymbols.contains(thatUnit.getSymbol()) && usSymbols.contains(thisUnit.getSymbol())); } return differentSystems; }  static @Nullable Class<? extends Quantity<?>> parseDimension(String dimension); static @Nullable String getDimensionName(Unit<?> unit); static @Nullable Unit<?> parseUnit(@Nullable String pattern); static boolean isDifferentMeasurementSystem(Unit<? extends Quantity<?>> thisUnit, Unit<?> thatUnit); static final String UNIT_PLACEHOLDER; static final String UNIT_PERCENT_FORMAT_STRING; }
@Test public void forBaseUnitsOfDifferentSystemsShouldBeTrue() { assertTrue(UnitUtils.isDifferentMeasurementSystem(SIUnits.CELSIUS, ImperialUnits.FAHRENHEIT)); assertTrue(UnitUtils.isDifferentMeasurementSystem(ImperialUnits.MILES_PER_HOUR, SIUnits.KILOMETRE_PER_HOUR)); }
@Test public void forBaseUnitsOfSameSystemShouldBeFalse() { assertFalse(UnitUtils.isDifferentMeasurementSystem(CENTI(SIUnits.METRE), SIUnits.METRE)); assertFalse(UnitUtils.isDifferentMeasurementSystem(SIUnits.METRE, MILLI(SIUnits.METRE))); assertFalse(UnitUtils.isDifferentMeasurementSystem(CENTI(SIUnits.METRE), MILLI(SIUnits.METRE))); assertFalse(UnitUtils.isDifferentMeasurementSystem(ImperialUnits.MILE, ImperialUnits.INCH)); assertFalse(UnitUtils.isDifferentMeasurementSystem(HECTO(SIUnits.PASCAL), SIUnits.PASCAL)); }
@Test public void forDerivedUnitsOfDifferentSystemsShouldBeTrue() { assertTrue(UnitUtils.isDifferentMeasurementSystem(CENTI(SIUnits.METRE), ImperialUnits.INCH)); assertTrue(UnitUtils.isDifferentMeasurementSystem(ImperialUnits.MILE, KILO(SIUnits.METRE))); }