Search in sources :

Example 1 with Quantity

use of javax.measure.Quantity in project smarthome by eclipse.

the class ItemStateConverterImpl method convertToAcceptedState.

@Override
@NonNull
public State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
    if (state == null) {
        logger.error("A conversion of null was requested:", new IllegalArgumentException("State must not be null."));
        return UnDefType.NULL;
    }
    if (item != null && !isAccepted(item, state)) {
        for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
            State convertedState = state.as(acceptedType);
            if (convertedState != null) {
                logger.debug("Converting {} '{}' to {} '{}' for item '{}'", state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), convertedState, item.getName());
                return convertedState;
            }
        }
    }
    if (item instanceof NumberItem && state instanceof QuantityType) {
        NumberItem numberItem = (NumberItem) item;
        if (numberItem.getDimension() != null) {
            QuantityType<?> quantityState = (QuantityType<?>) state;
            // in case the item does define a unit it takes precedence over all other conversions:
            Unit<?> itemUnit = parseItemUnit(numberItem);
            if (itemUnit != null) {
                if (!itemUnit.equals(quantityState.getUnit())) {
                    return convertOrUndef(quantityState, itemUnit);
                }
                return quantityState;
            }
            Class<? extends Quantity<?>> dimension = numberItem.getDimension();
            @SuppressWarnings({ "unchecked", "rawtypes" }) Unit<? extends Quantity<?>> conversionUnit = unitProvider.getUnit((Class<Quantity>) dimension);
            if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) {
                return convertOrUndef(quantityState, conversionUnit);
            }
            return state;
        } else {
            return state.as(DecimalType.class);
        }
    }
    return state;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Quantity(javax.measure.Quantity) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 2 with Quantity

use of javax.measure.Quantity in project com.revolsys.open by revolsys.

the class LineString method getLength.

@Override
default double getLength(final Unit<Length> unit) {
    double length = 0;
    final CoordinateSystem coordinateSystem = getCoordinateSystem();
    if (coordinateSystem instanceof GeographicCoordinateSystem) {
        final int vertexCount = getVertexCount();
        if (vertexCount > 1) {
            double lon0 = getX(0);
            double lat0 = getY(0);
            for (int i = 1; i < vertexCount; i++) {
                final double lon1 = getX(i);
                final double lat1 = getY(i);
                length += GeographicCoordinateSystem.distanceMetres(lon0, lat0, lon1, lat1);
                lon0 = lon1;
                lat0 = lat1;
            }
        }
        final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, Units.METRE);
        length = QuantityType.doubleValue(lengthMeasure, unit);
    } else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
        final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
        final Unit<Length> lengthUnit = projectedCoordinateSystem.getLengthUnit();
        length = getLength();
        final Quantity<Length> lengthMeasure = Quantities.getQuantity(length, lengthUnit);
        length = QuantityType.doubleValue(lengthMeasure, unit);
    } else {
        length = getLength();
    }
    return length;
}
Also used : Length(javax.measure.quantity.Length) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) Quantity(javax.measure.Quantity) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) Unit(javax.measure.Unit)

Example 3 with Quantity

use of javax.measure.Quantity in project com.revolsys.open by revolsys.

the class LinearRing method getPolygonArea.

default double getPolygonArea(final Unit<Area> unit) {
    double area = 0;
    final CoordinateSystem coordinateSystem = getCoordinateSystem();
    if (coordinateSystem instanceof GeographicCoordinateSystem) {
        // TODO better algorithm than converting to world mercator
        final GeometryFactory geometryFactory = GeometryFactory.worldMercator();
        final LinearRing ring = as2d(geometryFactory);
        return ring.getPolygonArea(unit);
    } else if (coordinateSystem instanceof ProjectedCoordinateSystem) {
        final ProjectedCoordinateSystem projectedCoordinateSystem = (ProjectedCoordinateSystem) coordinateSystem;
        final Unit<Length> lengthUnit = projectedCoordinateSystem.getLengthUnit();
        @SuppressWarnings("unchecked") final Unit<Area> areaUnit = (Unit<Area>) lengthUnit.multiply(lengthUnit);
        area = getPolygonArea();
        final Quantity<Area> areaMeasure = Quantities.getQuantity(area, areaUnit);
        area = QuantityType.doubleValue(areaMeasure, unit);
    } else {
        area = getPolygonArea();
    }
    return area;
}
Also used : Area(javax.measure.quantity.Area) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) ProjectedCoordinateSystem(com.revolsys.geometry.cs.ProjectedCoordinateSystem) Quantity(javax.measure.Quantity) GeographicCoordinateSystem(com.revolsys.geometry.cs.GeographicCoordinateSystem) PreparedLinearRing(com.revolsys.geometry.model.prep.PreparedLinearRing) Unit(javax.measure.Unit)

Example 4 with Quantity

use of javax.measure.Quantity in project com.revolsys.open by revolsys.

the class BaseStylePanel method newField.

@SuppressWarnings("unchecked")
protected Field newField(final String fieldName, final Class<?> fieldClass, final Object value) {
    Field field;
    if (fieldName.equals("visible")) {
        this.visibleField = new CheckBox(fieldName, value);
        field = this.visibleField;
    } else if (fieldName.equals("textFaceName")) {
        field = new FontChooserField(fieldName, (String) value);
    } else if (fieldName.endsWith("HorizontalAlignment")) {
        field = newHorizontalAlignmentField(fieldName, (String) value);
    } else if (fieldName.endsWith("VerticalAlignment")) {
        field = newVerticalAlignmentField(fieldName, (String) value);
    } else if (fieldName.equals("lineCap")) {
        field = newLineCapField((LineCap) value);
    } else if (fieldName.equals("lineJoin")) {
        field = newLineJoinField((LineJoin) value);
    } else if (fieldName.equals("lineDashArray")) {
        field = new DashField(fieldName, (List<Quantity<Length>>) value);
    } else if (fieldName.equals("queryFilter")) {
        final AbstractRecordLayer layer = getLayer();
        field = new QueryFilterField(layer, fieldName, (String) value);
        field.setFieldValue(value);
        Property.addListener(field, fieldName, this);
    } else if (fieldName.equals("marker")) {
        field = new MarkerField(fieldName, value);
    } else if (fieldName.endsWith("OrientationType")) {
        final ComboBox<String> orientationTypeField = ComboBox.newComboBox(fieldName, "auto", "none");
        orientationTypeField.setFieldValue(value);
        field = orientationTypeField;
    } else if (fieldName.equals("markerPlacementType")) {
        final ComboBox<String> placementField = ComboBox.newComboBox(fieldName, "auto", "center", "vertex(0)", "vertex(n)", "vertices", "segment(0)", "segment(n)", "segments");
        placementField.setFieldValue(value);
        field = placementField;
    } else if (fieldName.equals("textPlacementType")) {
        final ComboBox<String> placementField = ComboBox.newComboBox(fieldName, "auto", "center", "vertex(0)", "vertex(n)", "segment(0)", "segment(n)");
        placementField.setFieldValue(value);
        field = placementField;
    } else if (fieldName.endsWith("Scale")) {
        field = newScaleField(fieldName, (Long) value);
    } else if (Color.class.equals(fieldClass)) {
        field = new ColorChooserField(fieldName, (Color) value);
    } else if (Boolean.TYPE.equals(fieldClass) || Boolean.class.equals(fieldClass)) {
        field = new CheckBox(fieldName, value);
    } else if (Quantity.class.equals(fieldClass)) {
        field = new LengthMeasureTextField(fieldName, (Quantity<Length>) value, CustomUnits.PIXEL);
    } else {
        field = new TextField(fieldName, value, 40);
    }
    return field;
}
Also used : FontChooserField(com.revolsys.swing.field.FontChooserField) ComboBox(com.revolsys.swing.field.ComboBox) ColorChooserField(com.revolsys.swing.field.ColorChooserField) Color(java.awt.Color) Quantity(javax.measure.Quantity) LengthMeasureTextField(com.revolsys.swing.field.LengthMeasureTextField) FontChooserField(com.revolsys.swing.field.FontChooserField) MarkerField(com.revolsys.swing.map.component.MarkerField) JTextField(javax.swing.JTextField) Field(com.revolsys.swing.field.Field) TextField(com.revolsys.swing.field.TextField) ColorChooserField(com.revolsys.swing.field.ColorChooserField) Length(javax.measure.quantity.Length) CheckBox(com.revolsys.swing.field.CheckBox) AbstractRecordLayer(com.revolsys.swing.map.layer.record.AbstractRecordLayer) LengthMeasureTextField(com.revolsys.swing.field.LengthMeasureTextField) LengthMeasureTextField(com.revolsys.swing.field.LengthMeasureTextField) JTextField(javax.swing.JTextField) TextField(com.revolsys.swing.field.TextField) List(java.util.List) ArrayList(java.util.ArrayList) LineCap(com.revolsys.geometry.model.LineCap) MarkerField(com.revolsys.swing.map.component.MarkerField)

Example 5 with Quantity

use of javax.measure.Quantity in project uom-se by unitsofmeasurement.

the class TimeQuantitiesTest method ofLocalTimeTemporalAdjustTest.

@Test
public void ofLocalTimeTemporalAdjustTest() {
    LocalTime a = LocalTime.MIDNIGHT;
    TemporalAdjuster temporalAdjuster = (temporal) -> temporal.plus(12L, ChronoUnit.HOURS);
    Quantity<Time> time = TimeQuantities.getQuantity(a, () -> temporalAdjuster);
    Assert.assertEquals(Integer.valueOf(12), Integer.valueOf(time.getValue().intValue()));
    Assert.assertEquals(Units.HOUR, time.getUnit());
}
Also used : Unit(javax.measure.Unit) Month(java.time.Month) HOUR(tec.uom.se.unit.Units.HOUR) Test(org.junit.Test) DAY(tec.uom.se.unit.Units.DAY) ChronoUnit(java.time.temporal.ChronoUnit) Year(java.time.Year) Quantity(javax.measure.Quantity) Quantities(tec.uom.se.quantity.Quantities) TimeQuantities(tec.uom.se.quantity.time.TimeQuantities) Units(tec.uom.se.unit.Units) DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) TemporalAdjusters(java.time.temporal.TemporalAdjusters) MINUTE(tec.uom.se.unit.Units.MINUTE) SECOND(tec.uom.se.unit.Units.SECOND) TemporalAdjuster(java.time.temporal.TemporalAdjuster) Assert(org.junit.Assert) TemporalQuantity(tec.uom.se.quantity.time.TemporalQuantity) Time(javax.measure.quantity.Time) LocalTime(java.time.LocalTime) TemporalAdjuster(java.time.temporal.TemporalAdjuster) LocalTime(java.time.LocalTime) Time(javax.measure.quantity.Time) Test(org.junit.Test)

Aggregations

Quantity (javax.measure.Quantity)7 Unit (javax.measure.Unit)5 Length (javax.measure.quantity.Length)3 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)2 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)2 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)2 DayOfWeek (java.time.DayOfWeek)2 LocalDate (java.time.LocalDate)2 LocalTime (java.time.LocalTime)2 Month (java.time.Month)2 Year (java.time.Year)2 ChronoUnit (java.time.temporal.ChronoUnit)2 TemporalAdjuster (java.time.temporal.TemporalAdjuster)2 TemporalAdjusters (java.time.temporal.TemporalAdjusters)2 Time (javax.measure.quantity.Time)2 Test (org.junit.Test)2 LineCap (com.revolsys.geometry.model.LineCap)1 PreparedLinearRing (com.revolsys.geometry.model.prep.PreparedLinearRing)1 CheckBox (com.revolsys.swing.field.CheckBox)1 ColorChooserField (com.revolsys.swing.field.ColorChooserField)1