javax.measure.spi.SystemOfUnits Java Examples
The following examples show how to use
javax.measure.spi.SystemOfUnits.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test // TODO consolidate asserts public void testCommonUnitSystemServiceAlias() { final ServiceProvider commonProvider = ServiceProvider.of("Common"); assertNotNull(commonProvider); final SystemOfUnitsService commonService = commonProvider.getSystemOfUnitsService(); assertNotNull(commonService); assertEquals(COMMON_SERVICE_CLASSNAME, commonService.getClass().getName()); SystemOfUnits system = commonService.getSystemOfUnits("USCustomary"); assertNotNull(system); assertEquals("systems.uom.common.USCustomary", system.getClass().getName()); assertEquals("United States Customary Units", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS_US, system.getUnits().size()); SystemOfUnits system2 = commonService.getSystemOfUnits("US"); assertEquals(system, system2); system = commonService.getSystemOfUnits("CGS"); assertNotNull(system); assertEquals("Centimetre–gram–second System of Units", system.getName()); system2 = commonService.getSystemOfUnits("Centimetre–gram–second"); assertEquals(system, system2); assertEquals(NUM_OF_UNITS_CGS, system.getUnits().size()); }
Example #2
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test // TODO consolidate asserts public void testUnitSystemServiceAlias() { final ServiceProvider cldrProvider = ServiceProvider.of("Unicode"); assertNotNull(cldrProvider); final SystemOfUnitsService cldrService = cldrProvider.getSystemOfUnitsService(); assertNotNull(cldrService); assertEquals("systems.uom.unicode.spi.CLDRSystemService", cldrService.getClass().getName()); SystemOfUnits system = cldrService.getSystemOfUnits("CLDR"); assertNotNull(system); assertEquals("systems.uom.unicode.CLDR", system.getClass().getName()); assertEquals("Unicode CLDR", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS_CLDR, system.getUnits().size()); SystemOfUnits system2 = cldrService.getSystemOfUnits("Unicode"); assertEquals(system, system2); }
Example #3
Source File: I18nProviderImpl.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #4
Source File: I18nProviderImpl.java From smarthome with Eclipse Public License 2.0 | 5 votes |
private void initDimensionMap() { Map<SystemOfUnits, Unit<? extends Quantity<?>>> temperatureMap = new HashMap<>(); temperatureMap.put(SIUnits.getInstance(), SIUnits.CELSIUS); temperatureMap.put(ImperialUnits.getInstance(), ImperialUnits.FAHRENHEIT); dimensionMap.put(Temperature.class, temperatureMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> pressureMap = new HashMap<>(); pressureMap.put(SIUnits.getInstance(), HECTO(SIUnits.PASCAL)); pressureMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH_OF_MERCURY); dimensionMap.put(Pressure.class, pressureMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> speedMap = new HashMap<>(); speedMap.put(SIUnits.getInstance(), SIUnits.KILOMETRE_PER_HOUR); speedMap.put(ImperialUnits.getInstance(), ImperialUnits.MILES_PER_HOUR); dimensionMap.put(Speed.class, speedMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> lengthMap = new HashMap<>(); lengthMap.put(SIUnits.getInstance(), SIUnits.METRE); lengthMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH); dimensionMap.put(Length.class, lengthMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> intensityMap = new HashMap<>(); intensityMap.put(SIUnits.getInstance(), SmartHomeUnits.IRRADIANCE); intensityMap.put(ImperialUnits.getInstance(), SmartHomeUnits.IRRADIANCE); dimensionMap.put(Intensity.class, intensityMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> percentMap = new HashMap<>(); percentMap.put(SIUnits.getInstance(), SmartHomeUnits.ONE); percentMap.put(ImperialUnits.getInstance(), SmartHomeUnits.ONE); dimensionMap.put(Dimensionless.class, percentMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> angleMap = new HashMap<>(); angleMap.put(SIUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE); angleMap.put(ImperialUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE); dimensionMap.put(Angle.class, angleMap); }
Example #5
Source File: I18nProviderImpl.java From smarthome with Eclipse Public License 2.0 | 5 votes |
@Override public SystemOfUnits getMeasurementSystem() { final SystemOfUnits measurementSystem = this.measurementSystem; if (measurementSystem != null) { return measurementSystem; } // Only US and Liberia use the Imperial System. if (Locale.US.equals(locale) || Locale.forLanguageTag("en-LR").equals(locale)) { return ImperialUnits.getInstance(); } return SIUnits.getInstance(); }
Example #6
Source File: I18nProviderImpl.java From smarthome with Eclipse Public License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T extends Quantity<T>> @Nullable Unit<T> getUnit(@Nullable Class<T> dimension) { Map<SystemOfUnits, Unit<? extends Quantity<?>>> map = dimensionMap.get(dimension); if (map == null) { return null; } return (Unit<T>) map.get(getMeasurementSystem()); }
Example #7
Source File: I18nProviderImpl.java From smarthome with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #8
Source File: UnitUtils.java From smarthome with Eclipse Public License 2.0 | 5 votes |
/** * The name of the dimension as stated in the ChannelType configuration. * e.g. * <p> * <code> Unit: 'm' -> "Length"</code> * <p> * <code> Unit: 'kWh' -> "Energy"</code> * <p> * If the {@link Unit} can not be found in any of the available Measurement systems, it returns <code>null</code> * * @param unit The {@link Unit} to get the Dimension's name from. * @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits. */ public static @Nullable String getDimensionName(Unit<?> unit) { for (Class<? extends SystemOfUnits> system : ALL_SYSTEM_OF_UNITS) { for (Field field : system.getDeclaredFields()) { if (field.getType().isAssignableFrom(Unit.class) && Modifier.isStatic(field.getModifiers())) { Type genericType = field.getGenericType(); if (genericType instanceof ParameterizedType) { String dimension = ((Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0]) .getSimpleName(); Unit<?> systemUnit; try { systemUnit = (Unit<?>) field.get(null); if (systemUnit == null) { LOGGER.warn("Unit field points to a null value: {}", field); } else if (systemUnit.isCompatible(unit)) { return dimension; } } catch (IllegalArgumentException | IllegalAccessException e) { LOGGER.error("The unit field '{}' seems to be not accessible", field, e); } } else { LOGGER.warn("There is a unit field defined which has no generic type parametrization: {}", field); } } } } return null; }
Example #9
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testCommonUnitSystemService() { final ServiceProvider commonProvider = ServiceProvider.of("Common"); assertNotNull(commonProvider); final SystemOfUnitsService commonService = commonProvider.getSystemOfUnitsService(); assertNotNull(commonService); assertEquals("systems.uom.common.spi.CommonSystemService", commonService.getClass().getName()); SystemOfUnits system = commonService.getSystemOfUnits(); assertNotNull(system); assertEquals("systems.uom.common.USCustomary", system.getClass().getName()); assertEquals("United States Customary Units", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS_US, system.getUnits().size()); }
Example #10
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDefaultUnitSystemService() { assertNotNull(defaultService); assertEquals(DEFAULT_SERVICE_CLASSNAME, defaultService.getClass().getName()); SystemOfUnits system = defaultService.getSystemOfUnits(); assertNotNull(system); assertEquals("tech.units.indriya.unit.Units", system.getClass().getName()); assertEquals("Units", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS_DEFAULT, system.getUnits().size()); }
Example #11
Source File: CommonSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SystemOfUnits getSystemOfUnits(String name) { String alias = aliases.get(name); if (alias != null && alias.length() > 0) { return souMap.get(alias); } return souMap.get(name); }
Example #12
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test // TODO consolidate asserts public void testUnitSystemServiceAlias() { assertNotNull(defaultService); assertEquals("systems.uom.ucum.spi.UCUMSystemService", defaultService.getClass().getName()); SystemOfUnits system = defaultService.getSystemOfUnits("UCUM"); assertNotNull(system); assertEquals("systems.uom.ucum.UCUM", system.getClass().getName()); assertEquals("Unified Code for Units of Measure", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS, system.getUnits().size()); SystemOfUnits system2 = defaultService.getSystemOfUnits("Unified Code for Units of Measure"); assertEquals(system, system2); }
Example #13
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDefaultUnitSystemService() { assertNotNull(defaultService); assertEquals("systems.uom.ucum.spi.UCUMSystemService", defaultService.getClass().getName()); SystemOfUnits system = defaultService.getSystemOfUnits(); assertNotNull(system); assertEquals("systems.uom.ucum.UCUM", system.getClass().getName()); assertEquals(EXPECTED_SYSTEM_NAME, system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS, system.getUnits().size()); }
Example #14
Source File: UCUMSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SystemOfUnits getSystemOfUnits(String name) { String alias = aliases.get(name); if (alias != null && alias.length() > 0) { return souMap.get(alias); } return souMap.get(name); }
Example #15
Source File: UnitUtils.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
/** * The name of the dimension as stated in the ChannelType configuration. * e.g. * <p> * <code> Unit: 'm' -> "Length"</code> * <p> * <code> Unit: 'kWh' -> "Energy"</code> * <p> * If the {@link Unit} can not be found in any of the available Measurement systems, it returns <code>null</code> * * @param unit The {@link Unit} to get the Dimension's name from. * @return The Dimension string or null if the unit can not be found in any of the SystemOfUnits. */ public static @Nullable String getDimensionName(Unit<?> unit) { for (Class<? extends SystemOfUnits> system : ALL_SYSTEM_OF_UNITS) { for (Field field : system.getDeclaredFields()) { if (field.getType().isAssignableFrom(Unit.class) && Modifier.isStatic(field.getModifiers())) { Type genericType = field.getGenericType(); if (genericType instanceof ParameterizedType) { String dimension = ((Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0]) .getSimpleName(); Unit<?> systemUnit; try { systemUnit = (Unit<?>) field.get(null); if (systemUnit == null) { LOGGER.warn("Unit field points to a null value: {}", field); } else if (systemUnit.isCompatible(unit)) { return dimension; } } catch (IllegalArgumentException | IllegalAccessException e) { LOGGER.error("The unit field '{}' seems to be not accessible", field, e); } } else { LOGGER.warn("There is a unit field defined which has no generic type parametrization: {}", field); } } } } return null; }
Example #16
Source File: I18nProviderImpl.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T extends Quantity<T>> @Nullable Unit<T> getUnit(@Nullable Class<T> dimension) { Map<SystemOfUnits, Unit<? extends Quantity<?>>> map = dimensionMap.get(dimension); if (map == null) { return null; } return (Unit<T>) map.get(getMeasurementSystem()); }
Example #17
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testDefaultUnitSystemService() { assertNotNull(defaultService); assertEquals("tech.units.indriya.unit.DefaultSystemOfUnitsService", defaultService.getClass().getName()); SystemOfUnits system = defaultService.getSystemOfUnits(); assertNotNull(system); assertEquals("tech.units.indriya.unit.Units", system.getClass().getName()); assertEquals("Units", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS, system.getUnits().size()); }
Example #18
Source File: CLDRSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public SystemOfUnits getSystemOfUnits(String name) { String alias = aliases.get(name); if (alias != null && alias.length() > 0) { return souMap.get(alias); } return souMap.get(name); }
Example #19
Source File: I18nProviderImpl.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
@Override public SystemOfUnits getMeasurementSystem() { final SystemOfUnits measurementSystem = this.measurementSystem; if (measurementSystem != null) { return measurementSystem; } // Only US and Liberia use the Imperial System. if (Locale.US.equals(locale) || Locale.forLanguageTag("en-LR").equals(locale)) { return ImperialUnits.getInstance(); } return SIUnits.getInstance(); }
Example #20
Source File: SystemOfUnitsServiceTest.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test public void testCLDRUnitSystemService() { final ServiceProvider cldrProvider = ServiceProvider.of("Unicode"); assertNotNull(cldrProvider); final SystemOfUnitsService cldrService = cldrProvider.getSystemOfUnitsService(); assertNotNull(cldrService); assertEquals("systems.uom.unicode.spi.CLDRSystemService", cldrService.getClass().getName()); SystemOfUnits system = cldrService.getSystemOfUnits(); assertNotNull(system); assertEquals("systems.uom.unicode.CLDR", system.getClass().getName()); assertEquals("Unicode CLDR", system.getName()); assertNotNull(system.getUnits()); assertEquals(NUM_OF_UNITS_CLDR, system.getUnits().size()); }
Example #21
Source File: I18nProviderImpl.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
private void initDimensionMap() { Map<SystemOfUnits, Unit<? extends Quantity<?>>> temperatureMap = new HashMap<>(); temperatureMap.put(SIUnits.getInstance(), SIUnits.CELSIUS); temperatureMap.put(ImperialUnits.getInstance(), ImperialUnits.FAHRENHEIT); dimensionMap.put(Temperature.class, temperatureMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> pressureMap = new HashMap<>(); pressureMap.put(SIUnits.getInstance(), HECTO(SIUnits.PASCAL)); pressureMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH_OF_MERCURY); dimensionMap.put(Pressure.class, pressureMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> speedMap = new HashMap<>(); speedMap.put(SIUnits.getInstance(), SIUnits.KILOMETRE_PER_HOUR); speedMap.put(ImperialUnits.getInstance(), ImperialUnits.MILES_PER_HOUR); dimensionMap.put(Speed.class, speedMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> lengthMap = new HashMap<>(); lengthMap.put(SIUnits.getInstance(), SIUnits.METRE); lengthMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH); dimensionMap.put(Length.class, lengthMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> intensityMap = new HashMap<>(); intensityMap.put(SIUnits.getInstance(), SmartHomeUnits.IRRADIANCE); intensityMap.put(ImperialUnits.getInstance(), SmartHomeUnits.IRRADIANCE); dimensionMap.put(Intensity.class, intensityMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> percentMap = new HashMap<>(); percentMap.put(SIUnits.getInstance(), SmartHomeUnits.ONE); percentMap.put(ImperialUnits.getInstance(), SmartHomeUnits.ONE); dimensionMap.put(Dimensionless.class, percentMap); Map<SystemOfUnits, Unit<? extends Quantity<?>>> angleMap = new HashMap<>(); angleMap.put(SIUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE); angleMap.put(ImperialUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE); dimensionMap.put(Angle.class, angleMap); }
Example #22
Source File: CLDRSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Collection<SystemOfUnits> getAvailableSystemsOfUnits() { return souMap.values(); }
Example #23
Source File: CommonSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public SystemOfUnits getSystemOfUnits() { return getSystemOfUnits(DEFAULT_SYSTEM_NAME); // We assume US Customary as the more // common system here }
Example #24
Source File: CommonSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Collection<SystemOfUnits> getAvailableSystemsOfUnits() { return souMap.values(); }
Example #25
Source File: CLDRSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public SystemOfUnits getSystemOfUnits() { return getSystemOfUnits("CLDR"); }
Example #26
Source File: UCUMSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public SystemOfUnits getSystemOfUnits() { return getSystemOfUnits(DEFAULT_SYSTEM_NAME); }
Example #27
Source File: UCUMSystemService.java From uom-systems with BSD 3-Clause "New" or "Revised" License | 4 votes |
public Collection<SystemOfUnits> getAvailableSystemsOfUnits() { return souMap.values(); }
Example #28
Source File: UnitServices.java From sis with Apache License 2.0 | 3 votes |
/** * Returns the system of units having the specified name, or {@code null} if none. * The argument can be any name in the following table: * * <table class="sis"> * <caption>Available system of units</caption> * <tr><th>Name</th> <th>Examples</th></tr> * <tr><td>SI</td> <td>m, km, m³, s, m∕s, K, °C, hPa, rad, µrad</td></tr> * <tr><td>SI + accepted</td> <td>s, min, h, m∕s, km∕h, °, ′, ″, ha</td></tr> * <tr><td>Imperial</td> <td>in, ft, mi (statute mile)</td></tr> * <tr><td>SI + other</td> <td>m, m∕s, km∕h, ft, mi, M (nautical mile)</td></tr> * </table> * * The search for name is case-insensitive and ignore whitespaces. * * @param name the name of the desired system of units. * @return the system of units for the given name, or {@code null} if none. */ @Override public SystemOfUnits getSystemOfUnits(final String name) { ArgumentChecks.ensureNonEmpty("name", name); for (final UnitRegistry s : systems) { if (CharSequences.equalsFiltered(s.name, name, Characters.Filter.UNICODE_IDENTIFIER, true)) { return s; } } return null; }
Example #29
Source File: UnitServices.java From sis with Apache License 2.0 | 2 votes |
/** * Returns the list of all available systems of units. * * @return list of available systems of units. */ @Override public Collection<SystemOfUnits> getAvailableSystemsOfUnits() { return UnmodifiableArrayList.wrap(systems); }
Example #30
Source File: UnitServices.java From sis with Apache License 2.0 | 2 votes |
/** * Returns the default system of units used by Apache SIS. * This include the International System of Units (SI) together with some imperial units and other units. * This system includes at least all the constants defined in the {@link Units} class. * * @return the system of units used by Apache SIS. */ @Override public SystemOfUnits getSystemOfUnits() { return systems[systems.length - 1]; }