Java Code Examples for javax.measure.Quantity#getUnit()

The following examples show how to use javax.measure.Quantity#getUnit() . 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: Quantities.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the given quantity as an instance of the specific {@code Quantity} subtype.
 * For example this method can be used for converting a {@code Quantity<Length>} to a {@link Length}.
 * If the given quantity already implements the specific interface, then it is returned as-is.
 *
 * @param  <Q>      the quantity type (e.g. {@link Length}, {@link Angle}, {@link Time}, <i>etc.</i>), or {@code null}.
 * @param  quantity the quantity to convert to the specific subtype.
 * @return the given quantity as a specific subtype (may be {@code quantity} itself), or {@code null} if the given quantity was null.
 * @throws IllegalArgumentException if the unit class associated to the given quantity is not a supported implementation.
 */
@SuppressWarnings("unchecked")
public static <Q extends Quantity<Q>> Q castOrCopy(final Quantity<Q> quantity) {
    if (quantity != null) {
        final Unit<Q> unit   = quantity.getUnit();
        final Unit<Q> system = unit.getSystemUnit();
        if (!(system instanceof SystemUnit<?>)) {
            throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedImplementation_1, unit.getClass()));
        }
        final Class<Q> type = ((SystemUnit<Q>) system).quantity;
        if (!type.isInstance(quantity)) {
            final ScalarFactory<Q> factory = ((SystemUnit<Q>) system).factory;
            final double value = AbstractConverter.doubleValue(quantity.getValue());
            if (factory != null) {
                return factory.create(value, unit);
            } else {
                return ScalarFallback.factory(value, unit, type);
            }
        }
    }
    return (Q) quantity;
}
 
Example 2
Source File: UnitUtils.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * A utility method to parse a unit symbol either directly or from a given pattern (like stateDescription or widget
 * label). In the latter case, the unit is expected to be the last part of the pattern separated by " " (e.g. "%.2f
 * °C" for °C).
 *
 * @param stringWithUnit the string to extract the unit symbol from
 * @return the unit symbol extracted from the string or {@code null} if no unit could be parsed
 *
 */
public static @Nullable Unit<?> parseUnit(@Nullable String pattern) {
    if (pattern == null || pattern.isBlank()) {
        return null;
    }

    String unitSymbol = pattern;
    int lastBlankIndex = pattern.lastIndexOf(" ");
    if (lastBlankIndex >= 0) {
        unitSymbol = pattern.substring(lastBlankIndex).trim();
    }

    if (!UNIT_PLACEHOLDER.equals(unitSymbol)) {
        if (UNIT_PERCENT_FORMAT_STRING.equals(unitSymbol)) {
            return SmartHomeUnits.PERCENT;
        }
        try {
            Quantity<?> quantity = Quantities.getQuantity("1 " + unitSymbol);
            return quantity.getUnit();
        } catch (IllegalArgumentException e) {
            // we expect this exception in case the extracted string does not match any known unit
            LOGGER.debug("Unknown unit from pattern: {}", unitSymbol);
        }
    }

    return null;
}
 
Example 3
Source File: UnitUtils.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * A utility method to parse a unit symbol either directly or from a given pattern (like stateDescription or widget
 * label). In the latter case, the unit is expected to be the last part of the pattern separated by " " (e.g. "%.2f
 * °C" for °C).
 *
 * @param stringWithUnit the string to extract the unit symbol from
 * @return the unit symbol extracted from the string or {@code null} if no unit could be parsed
 *
 */
public static @Nullable Unit<?> parseUnit(String pattern) {
    if (StringUtils.isBlank(pattern)) {
        return null;
    }

    String unitSymbol = pattern;
    int lastBlankIndex = pattern.lastIndexOf(" ");
    if (lastBlankIndex >= 0) {
        unitSymbol = pattern.substring(lastBlankIndex).trim();
    }

    if (StringUtils.isNotBlank(unitSymbol) && !unitSymbol.equals(UNIT_PLACEHOLDER)) {
        if (UNIT_PERCENT_FORMAT_STRING.equals(unitSymbol)) {
            return SmartHomeUnits.PERCENT;
        }
        try {
            Quantity<?> quantity = Quantities.getQuantity("1 " + unitSymbol);
            return quantity.getUnit();
        } catch (IllegalArgumentException e) {
            // we expect this exception in case the extracted string does not match any known unit
            LOGGER.debug("Unknown unit from pattern: {}", unitSymbol);
        }
    }

    return null;
}
 
Example 4
Source File: Scalar.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the given quantity converted to the same units of measurement than this quantity.
 */
private double doubleValue(final Quantity<Q> other) {
    double otherValue = other.getValue().doubleValue();
    final Unit<Q> otherUnit = other.getUnit();
    if (otherUnit != unit) {
        otherValue = otherUnit.getConverterTo(unit).convert(otherValue);
    }
    return otherValue;
}
 
Example 5
Source File: PersistentQuantityAndUnit.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Override
protected Object[] toConvertedColumns(Quantity<?> value) {
    return new Object[] { value.getValue().toString(), value.getUnit() };
}