From 839a68f6160007b74cec9a2c6bd6a540b6afcce5 Mon Sep 17 00:00:00 2001 From: Parth Karia Date: Mon, 4 Sep 2017 17:17:36 +0530 Subject: [PATCH] BAEL-1106 Introduction to javax.measure (#2522) * BAEL-243 Spring Batch using Partitioner * BAEL-1106 Introduction to javax.measure * Unnecessary Committed * BAEL-1106 Move code to libraries from core-java * BAEL-1106 Move code to libraries from core-java * Update pom.xml --- libraries/pom.xml | 5 ++ .../com/baeldung/javax/measure/WaterTank.java | 26 +++++++ .../javax/measure/WaterTankTests.java | 68 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java create mode 100644 libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java diff --git a/libraries/pom.xml b/libraries/pom.xml index a53feda5fc..1d0f0db165 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -136,6 +136,11 @@ commons-text ${commons-text.version} + + javax.measure + jsr-275 + 1.0.0 + org.apache.commons commons-collections4 diff --git a/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java b/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java new file mode 100644 index 0000000000..1ab9eee53f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/javax/measure/WaterTank.java @@ -0,0 +1,26 @@ +package com.baeldung.javax.measure; + +import javax.measure.Measure; +import javax.measure.quantity.Volume; + +public class WaterTank { + + private Measure capacityMeasure; + private double capacityDouble; + + public void setCapacityMeasure(Measure capacityMeasure) { + this.capacityMeasure = capacityMeasure; + } + + public void setCapacityDouble(double capacityDouble) { + this.capacityDouble = capacityDouble; + } + + public Measure getCapacityMeasure() { + return capacityMeasure; + } + + public double getCapacityDouble() { + return capacityDouble; + } +} diff --git a/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java b/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java new file mode 100644 index 0000000000..ef54035353 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/javax/measure/WaterTankTests.java @@ -0,0 +1,68 @@ +package com.baeldung.javax.measure; + +import javax.measure.Measure; +import javax.measure.converter.UnitConverter; +import javax.measure.quantity.Duration; +import javax.measure.quantity.Length; +import javax.measure.quantity.Pressure; +import javax.measure.quantity.Volume; +import static javax.measure.unit.NonSI.HOUR; +import static javax.measure.unit.NonSI.LITRE; +import static javax.measure.unit.NonSI.MILE; +import static javax.measure.unit.NonSI.MINUTE; +import javax.measure.unit.SI; +import static javax.measure.unit.SI.KILO; +import static javax.measure.unit.SI.METER; +import static javax.measure.unit.SI.NEWTON; +import static javax.measure.unit.SI.SECOND; +import javax.measure.unit.Unit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class WaterTankTests { + + @Test + public void givenMeasure_whenGetUnitAndConvertValue_thenSuccess() { + WaterTank waterTank = new WaterTank(); + waterTank.setCapacityMeasure(Measure.valueOf(9.2, LITRE)); + assertEquals(LITRE, waterTank.getCapacityMeasure().getUnit()); + + Measure waterCapacity = waterTank.getCapacityMeasure(); + double volumeInLitre = waterCapacity.getValue().doubleValue(); + assertEquals(9.2, volumeInLitre, 0.0f); + + double volumeInMilliLitre = waterCapacity.doubleValue(SI.MILLI(LITRE)); + assertEquals(9200.0, volumeInMilliLitre, 0.0f); + + Unit Kilometer = SI.KILO(METER); + Unit Centimeter = SI.CENTI(METER); + } + + @Test + public void givenMeasure_whenAlternateMeasure_ThenGetAlternateMeasure() { + Unit PASCAL = NEWTON.divide(METER.pow(2)).alternate("Pa"); + assertTrue(Unit.valueOf("Pa").equals(PASCAL)); + } + + @Test + public void givenMeasure_whenCompoundMeasure_ThenGetCompoundMeasure() { + Unit HOUR_MINUTE_SECOND = HOUR.compound(MINUTE).compound(SECOND); + Measure duration = Measure.valueOf(12345, SECOND); + assertEquals("3h25min45s", duration.to(HOUR_MINUTE_SECOND).toString()); + } + + @Test + public void givenMiles_whenConvertToKilometer_ThenConverted() { + double distanceInMiles = 50.0; + UnitConverter mileToKilometer = MILE.getConverterTo(KILO(METER)); + double distanceInKilometers = mileToKilometer.convert(distanceInMiles); + assertEquals(80.4672, distanceInKilometers, 0.00f); + } + + @Test + public void givenSymbol_WhenCompareToSystemUnit_ThenSuccess() { + assertTrue(Unit.valueOf("kW").equals(SI.KILO(SI.WATT))); + assertTrue(Unit.valueOf("ms").equals(SI.SECOND.divide(1000))); + } +}