Java Code Examples for javax.measure.Unit#isCompatible()

The following examples show how to use javax.measure.Unit#isCompatible() . 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: CoordinateFormat.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts the resolution units for the given coordinate system axis. This methods select the units which
 * result in the smallest absolute value of {@link #resolution}.
 *
 * @param  maxValue  the maximal absolute value that a coordinate on the axis may have.
 * @param  axisUnit  {@link CoordinateSystemAxis#getUnit()}.
 * @return whether the given axis unit is compatible with the expected unit.
 */
boolean forAxis(double maxValue, final Unit<?> axisUnit) throws IncommensurableException {
    if (!axisUnit.isCompatible(unit)) {
        return false;
    }
    final UnitConverter c = unit.getConverterToAny(axisUnit);
    final double r = Math.abs(c.convert(resolution));
    if (r < resolution) {
        resolution = r;                                         // To units producing the smallest value.
        unit = axisUnit;
    } else {
        maxValue = Math.abs(c.inverse().convert(maxValue));     // From axis units to selected units.
    }
    if (maxValue > magnitude) {
        magnitude = maxValue;
    }
    return true;
}
 
Example 2
Source File: UnitUtils.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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 3
Source File: UnitUtils.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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 4
Source File: Verifier.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * If the given domain of values accepts units of incompatible dimensions, return the unit which is compatible
 * with the given units. This is a non-public mechanism handling a few parameters in the EPSG database, like
 * <cite>Coordinate 1 of evaluation point</cite> (EPSG:8617).
 */
private static Unit<?> getCompatibleUnit(final Range<?> valueDomain, final Unit<?> unit) {
    if (valueDomain instanceof EPSGParameterDomain) {
        for (final Unit<?> valid : ((EPSGParameterDomain) valueDomain).units) {
            if (unit.isCompatible(valid)) {
                return valid;
            }
        }
    }
    return null;
}