unit-api examples (#2566)

* unit-api examples

* fix conflict
This commit is contained in:
lor6 2017-09-06 08:49:55 +03:00 committed by Grzegorz Piwowarek
parent 7916882668
commit 4b9ec46ca3
4 changed files with 95 additions and 76 deletions

View File

@ -136,9 +136,9 @@
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>jsr-275</artifactId>
<version>1.0.0</version>
<groupId>tec.units</groupId>
<artifactId>unit-ri</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
@ -632,5 +632,6 @@
<joda-time.version>2.9.9</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
<protonpack.version>1.14</protonpack.version>
<unit-ri.version>1.0.3</unit-ri.version>
</properties>
</project>

View File

@ -1,14 +1,14 @@
package com.baeldung.javax.measure;
package com.baeldung.measurement;
import javax.measure.Measure;
import javax.measure.Quantity;
import javax.measure.quantity.Volume;
public class WaterTank {
private Measure<Volume> capacityMeasure;
private Quantity<Volume> capacityMeasure;
private double capacityDouble;
public void setCapacityMeasure(Measure<Volume> capacityMeasure) {
public void setCapacityMeasure(Quantity<Volume> capacityMeasure) {
this.capacityMeasure = capacityMeasure;
}
@ -16,7 +16,7 @@ public class WaterTank {
this.capacityDouble = capacityDouble;
}
public Measure<Volume> getCapacityMeasure() {
public Quantity<Volume> getCapacityMeasure() {
return capacityMeasure;
}

View File

@ -1,68 +0,0 @@
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)));
}
}

View File

@ -0,0 +1,86 @@
package com.baeldung.measurement;
import javax.measure.Quantity;
import javax.measure.quantity.Area;
import javax.measure.quantity.Length;
import javax.measure.quantity.Pressure;
import javax.measure.quantity.Volume;
import javax.measure.Unit;
import javax.measure.UnitConverter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.baeldung.measurement.WaterTank;
import tec.units.ri.format.SimpleUnitFormat;
import tec.units.ri.quantity.Quantities;
import tec.units.ri.unit.MetricPrefix;
import static tec.units.ri.unit.Units.*;
public class WaterTankTests {
@Test
public void givenQuantity_whenGetUnitAndConvertValue_thenSuccess() {
WaterTank waterTank = new WaterTank();
waterTank.setCapacityMeasure(Quantities.getQuantity(9.2, LITRE));
assertEquals(LITRE, waterTank.getCapacityMeasure().getUnit());
Quantity<Volume> waterCapacity = waterTank.getCapacityMeasure();
double volumeInLitre = waterCapacity.getValue().doubleValue();
assertEquals(9.2, volumeInLitre, 0.0f);
double volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(LITRE)).getValue().doubleValue();
assertEquals(9200.0, volumeInMilliLitre, 0.0f);
// compilation error
// volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(KILOGRAM));
Unit<Length> Kilometer = MetricPrefix.KILO(METRE);
// compilation error
// Unit<Length> Centimeter = MetricPrefix.CENTI(LITRE);
}
@Test
public void givenUnit_whenAlternateUnit_ThenGetAlternateUnit() {
Unit<Pressure> PASCAL = NEWTON.divide(METRE.pow(2)).alternate("Pa").asType(Pressure.class);
assertTrue(SimpleUnitFormat.getInstance().parse("Pa").equals(PASCAL));
}
@Test
public void givenUnit_whenProduct_ThenGetProductUnit() {
Unit<Area> squareMetre = METRE.multiply(METRE).asType(Area.class);
Quantity<Length> line = Quantities.getQuantity(2, METRE);
assertEquals(line.multiply(line).getUnit(), squareMetre);
}
@Test
public void givenMeters_whenConvertToKilometer_ThenConverted() {
double distanceInMeters = 50.0;
UnitConverter metreToKilometre = METRE.getConverterTo(MetricPrefix.KILO(METRE));
double distanceInKilometers = metreToKilometre.convert(distanceInMeters);
assertEquals(0.05, distanceInKilometers, 0.00f);
}
@Test
public void givenSymbol_WhenCompareToSystemUnit_ThenSuccess() {
assertTrue(SimpleUnitFormat.getInstance().parse("kW").equals(MetricPrefix.KILO(WATT)));
assertTrue(SimpleUnitFormat.getInstance().parse("ms").equals(SECOND.divide(1000)));
}
@Test
public void givenUnits_WhenAdd_ThenSuccess() {
Quantity<Length> total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, METRE));
assertEquals(total.getValue().intValue(), 5);
// compilation error
// Quantity<Length> total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, LITRE));
Quantity<Length> totalKm = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, MetricPrefix.KILO(METRE)));
assertEquals(totalKm.getValue().intValue(), 3002);
}
}