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()); } } // Compare the unit symbols. For product units (e.g. 1km / 1h) the equality is not given in the Sets above. 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; }
private void setMeasurementSystem(@Nullable String measurementSystem) { SystemOfUnits oldMeasurementSystem = this.measurementSystem; final String ms; if (measurementSystem == null || measurementSystem.isEmpty()) { ms = ""; } else { ms = measurementSystem; } final SystemOfUnits newMeasurementSystem; switch (ms) { case "SI": newMeasurementSystem = SIUnits.getInstance(); break; case "US": newMeasurementSystem = ImperialUnits.getInstance(); break; default: logger.debug("Error setting measurement system for value '{}'.", measurementSystem); newMeasurementSystem = null; break; } this.measurementSystem = newMeasurementSystem; if (oldMeasurementSystem != null && newMeasurementSystem == null) { logger.info("Measurement system is not set, falling back to locale based system."); } else if (newMeasurementSystem != null && !newMeasurementSystem.equals(oldMeasurementSystem)) { logger.info("Measurement system set to '{}'.", newMeasurementSystem.getName()); } }
/** * Returns the dimensionless unit. This is a workaround for what seems to be a bug * in the reference implementation 1.0.1 of unit API. */ private static Unit<Dimensionless> getDimensionless(final SystemOfUnits system) { Unit<Dimensionless> unit = system.getUnit(Dimensionless.class); if (unit == null) try { unit = ((Unit<?>) Class.forName("tec.units.ri.AbstractUnit").getField("ONE").get(null)).asType(Dimensionless.class); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("Can not create a dimensionless unit from the given provider."); } return unit; }
private static void printSoU(final SystemOfUnits sou, final boolean showIndex) { int index = 0; System.out.println("Reporting " + sou.getName()); for (Unit<?> u : sou.getUnits()) { index++; if (showIndex) { System.out.println(index + "; " + u.getName() + "; " + u.getSymbol() + "; " + u); } else { System.out.println(u.getName() + "; " + u.getSymbol() + "; " + u); } } } }
/** * Returns the dimensionless unit. This is a workaround for what seems to be a bug * in the reference implementation 1.0.1 of unit API. */ private static Unit<Dimensionless> getDimensionless(final SystemOfUnits system) { Unit<Dimensionless> unit = system.getUnit(Dimensionless.class); if (unit == null) try { unit = ((Unit<?>) Class.forName("tec.units.ri.AbstractUnit").getField("ONE").get(null)).asType(Dimensionless.class); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("Can not create a dimensionless unit from the given provider."); } return unit; }
/** * Returns the dimensionless unit. This is a workaround for what seems to be a bug * in the reference implementation 1.0.1 of unit API. */ private static Unit<Dimensionless> getDimensionless(final SystemOfUnits system) { Unit<Dimensionless> unit = system.getUnit(Dimensionless.class); if (unit == null) try { unit = ((Unit<?>) Class.forName("tec.units.ri.AbstractUnit").getField("ONE").get(null)).asType(Dimensionless.class); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("Can not create a dimensionless unit from the given provider."); } return unit; }
public PersistentQuantityAndUnit() { super(); for (Unit<?> next : UNITS.getUnits()) { unitsMap.put(next.getSymbol(), next); } }
private void setMeasurementSystem(@Nullable String measurementSystem) { SystemOfUnits oldMeasurementSystem = this.measurementSystem; final String ms; if (measurementSystem == null || measurementSystem.isEmpty()) { ms = ""; } else { ms = measurementSystem; } final SystemOfUnits newMeasurementSystem; switch (ms) { case "SI": newMeasurementSystem = SIUnits.getInstance(); break; case "US": newMeasurementSystem = ImperialUnits.getInstance(); break; default: logger.debug("Error setting measurement system for value '{}'.", measurementSystem); newMeasurementSystem = null; break; } this.measurementSystem = newMeasurementSystem; if (oldMeasurementSystem != null && newMeasurementSystem == null) { logger.info("Measurement system is not set, falling back to locale based system."); } else if (newMeasurementSystem != null && !newMeasurementSystem.equals(oldMeasurementSystem)) { logger.info("Measurement system set to '{}'.", newMeasurementSystem.getName()); } }
/** * Returns the dimensionless unit. This is a workaround for what seems to be a bug * in the reference implementation 1.0.1 of unit API. */ private static Unit<Dimensionless> getDimensionless(final SystemOfUnits system) { Unit<Dimensionless> unit = system.getUnit(Dimensionless.class); if (unit == null) try { unit = ((Unit<?>) Class.forName("tec.units.ri.AbstractUnit").getField("ONE").get(null)).asType(Dimensionless.class); } catch (ReflectiveOperationException | ClassCastException e) { throw new IllegalArgumentException("Can not create a dimensionless unit from the given provider."); } return unit; }
public StringColumnUnitMapper() { for (Unit<?> next : SYSTEM_OF_UNITS_SERVICE.getSystemOfUnits().getUnits()) { unitsMap.put(next.getSymbol(), next); } }
/** * Creates a new factory which will use the given system of units. * * @param system the system of units to use for creating base units. */ protected Units(final SystemOfUnits system) { metre = system.getUnit(Length.class); radian = system.getUnit(Angle.class); second = system.getUnit(Time.class); one = getDimensionless(system); kilometre = metre .multiply(1000); degree = radian.multiply(Math.PI/180); day = second.multiply(24*60*60); ppm = one .divide(1000000); }
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()); } } // Compare the unit symbols. For product units (e.g. 1km / 1h) the equality is not given in the Sets above. 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; }
/** * Returns the dimension for the specified quantity type by aggregating the results of {@link DimensionService} or <code>null</code> if the * specified quantity is unknown. * * @param quantityType * the quantity type. * @return the dimension for the quantity type or <code>null</code>. * @since 1.0.2 */ public static <Q extends Quantity<Q>> Dimension of(Class<Q> quantityType) { // TODO: Track services and aggregate results (register custom // types) Unit<Q> siUnit = Units.getInstance().getUnit(quantityType); if (siUnit == null) logger.log(Level.FINER, "Quantity type: " + quantityType + " unknown"); // we're logging but probably FINER is // enough? return (siUnit != null) ? siUnit.getDimension() : null; }
/** * Tests the "Imperial" system of units. */ @Test public void testImperial() { final ServiceProvider provider = ServiceProvider.current(); Set<? extends Unit<?>> units = provider.getSystemOfUnitsService().getSystemOfUnits("Imperial").getUnits(); assertFalse("METRE", units.contains(Units.METRE)); assertFalse("KILOMETRE", units.contains(Units.KILOMETRE)); assertFalse("CUBIC_METRE", units.contains(Units.CUBIC_METRE)); assertFalse("METRES_PER_SECOND", units.contains(Units.METRES_PER_SECOND)); assertFalse("KILOMETRES_PER_HOUR", units.contains(Units.KILOMETRES_PER_HOUR)); assertTrue ("STATUTE_MILE", units.contains(Units.STATUTE_MILE)); assertFalse("DEGREE", units.contains(Units.DEGREE)); assertFalse("RADIAN", units.contains(Units.RADIAN)); assertFalse("GRAD", units.contains(Units.GRAD)); }
/** * Returns the dimension for the specified quantity type by aggregating the results of {@link DimensionService} or <code>null</code> if the * specified quantity is unknown. * * @param quantityType * the quantity type. * @return the dimension for the quantity type or <code>null</code>. * @since 1.0.1 */ public static <Q extends Quantity<Q>> Dimension of(Class<Q> quantityType) { // TODO: Track services and aggregate results (register custom // types) Unit<Q> siUnit = Units.getInstance().getUnit(quantityType); if (siUnit == null) logger.log(Level.FINER, "Quantity type: " + quantityType + " unknown"); // we're // logging // but // probably // FINER // is // enough? return (siUnit != null) ? siUnit.getDimension() : null; }
/** * Tests the "SI" system of units. */ @Test public void testSI() { final ServiceProvider provider = ServiceProvider.current(); Set<? extends Unit<?>> units = provider.getSystemOfUnitsService().getSystemOfUnits("SI").getUnits(); assertTrue ("METRE", units.contains(Units.METRE)); assertTrue ("KILOMETRE", units.contains(Units.KILOMETRE)); assertTrue ("CUBIC_METRE", units.contains(Units.CUBIC_METRE)); assertTrue ("METRES_PER_SECOND", units.contains(Units.METRES_PER_SECOND)); assertFalse("KILOMETRES_PER_HOUR", units.contains(Units.KILOMETRES_PER_HOUR)); assertFalse("NAUTICAL_MILE", units.contains(Units.NAUTICAL_MILE)); assertFalse("STATUTE_MILE", units.contains(Units.STATUTE_MILE)); assertFalse("DEGREE", units.contains(Units.DEGREE)); assertTrue ("RADIAN", units.contains(Units.RADIAN)); assertFalse("GRAD", units.contains(Units.GRAD)); }
/** * Returns the dimension for the specified quantity type by aggregating the results of {@link DimensionService} or <code>null</code> if the * specified quantity is unknown. * * @param quantityType * the quantity type. * @return the dimension for the quantity type or <code>null</code>. * @since 1.0.1 */ public static <Q extends Quantity<Q>> Dimension of(Class<Q> quantityType) { // TODO: Track services and aggregate results (register custom // types) Unit<Q> siUnit = Units.getInstance().getUnit(quantityType); if (siUnit == null) logger.log(Level.FINER, "Quantity type: " + quantityType + " unknown"); // we're // logging // but // probably // FINER // is // enough? return (siUnit != null) ? siUnit.getDimension() : null; }
/** * Tests the "SI + accepted" system of units. */ @Test public void testAccepted() { final ServiceProvider provider = ServiceProvider.current(); Set<? extends Unit<?>> units = provider.getSystemOfUnitsService().getSystemOfUnits("SI + accepted").getUnits(); assertTrue ("METRE", units.contains(Units.METRE)); assertTrue ("KILOMETRE", units.contains(Units.KILOMETRE)); assertTrue ("CUBIC_METRE", units.contains(Units.CUBIC_METRE)); assertTrue ("METRES_PER_SECOND", units.contains(Units.METRES_PER_SECOND)); assertTrue ("KILOMETRES_PER_HOUR", units.contains(Units.KILOMETRES_PER_HOUR)); assertFalse("NAUTICAL_MILE", units.contains(Units.NAUTICAL_MILE)); assertFalse("STATUTE_MILE", units.contains(Units.STATUTE_MILE)); assertTrue ("DEGREE", units.contains(Units.DEGREE)); assertTrue ("RADIAN", units.contains(Units.RADIAN)); assertFalse("GRAD", units.contains(Units.GRAD)); }
/** * Creates a new factory which will use the given system of units. * * @param system the system of units to use for creating base units. */ public Units(final SystemOfUnits system) { metre = system.getUnit(Length.class); radian = system.getUnit(Angle.class); second = system.getUnit(Time.class); pascal = system.getUnit(Pressure.class); one = getDimensionless(system); kilometre = metre .multiply(1000); foot = metre .multiply(0.3048); footSurveyUS = metre .multiply(12 / 39.37); degree = radian.multiply(Math.PI / 180); grad = radian.multiply(Math.PI / 200); arcSecond = radian.multiply(Math.PI / (180*60*60)); microradian = radian.divide(1E6); day = second.multiply(24*60*60); hectopascal = pascal.multiply(100); ppm = one .divide(1000000); }
/** * Tests the default system of units. */ @Test public void testDefaultSystemOfUnits() { final ServiceProvider provider = ServiceProvider.current(); Set<? extends Unit<?>> units = provider.getSystemOfUnitsService().getSystemOfUnits().getUnits(); assertTrue("METRE", units.contains(Units.METRE)); assertTrue("KILOMETRE", units.contains(Units.KILOMETRE)); assertTrue("CUBIC_METRE", units.contains(Units.CUBIC_METRE)); assertTrue("METRES_PER_SECOND", units.contains(Units.METRES_PER_SECOND)); assertTrue("KILOMETRES_PER_HOUR", units.contains(Units.KILOMETRES_PER_HOUR)); assertTrue("NAUTICAL_MILE", units.contains(Units.NAUTICAL_MILE)); assertTrue("STATUTE_MILE", units.contains(Units.STATUTE_MILE)); assertTrue("DEGREE", units.contains(Units.DEGREE)); assertTrue("RADIAN", units.contains(Units.RADIAN)); assertTrue("GRAD", units.contains(Units.GRAD)); }