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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations