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
This commit is contained in:
parent
2b293e51e0
commit
839a68f616
|
@ -136,6 +136,11 @@
|
|||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.measure</groupId>
|
||||
<artifactId>jsr-275</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.javax.measure;
|
||||
|
||||
import javax.measure.Measure;
|
||||
import javax.measure.quantity.Volume;
|
||||
|
||||
public class WaterTank {
|
||||
|
||||
private Measure<Volume> capacityMeasure;
|
||||
private double capacityDouble;
|
||||
|
||||
public void setCapacityMeasure(Measure<Volume> capacityMeasure) {
|
||||
this.capacityMeasure = capacityMeasure;
|
||||
}
|
||||
|
||||
public void setCapacityDouble(double capacityDouble) {
|
||||
this.capacityDouble = capacityDouble;
|
||||
}
|
||||
|
||||
public Measure<Volume> getCapacityMeasure() {
|
||||
return capacityMeasure;
|
||||
}
|
||||
|
||||
public double getCapacityDouble() {
|
||||
return capacityDouble;
|
||||
}
|
||||
}
|
|
@ -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<Volume> 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<Length> Kilometer = SI.KILO(METER);
|
||||
Unit<Length> Centimeter = SI.CENTI(METER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMeasure_whenAlternateMeasure_ThenGetAlternateMeasure() {
|
||||
Unit<Pressure> PASCAL = NEWTON.divide(METER.pow(2)).alternate("Pa");
|
||||
assertTrue(Unit.valueOf("Pa").equals(PASCAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMeasure_whenCompoundMeasure_ThenGetCompoundMeasure() {
|
||||
Unit<Duration> HOUR_MINUTE_SECOND = HOUR.compound(MINUTE).compound(SECOND);
|
||||
Measure<Duration> 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)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue