@Test public void testLength() { Quantity<Length> l = DefaultQuantityFactory.getInstance(Length.class).create(23.5, METRE); // 23.0 km assertEquals(Double.valueOf(23.5d), l.getValue()); assertEquals(METRE, l.getUnit()); assertEquals("m", l.getUnit().getSymbol()); }
@Test @Ignore public void testMass() { Quantity<Mass> m = DefaultQuantityFactory.getInstance(Mass.class).create(10, KILOGRAM); // 10 kg assertEquals(Integer.valueOf(10), m.getValue()); assertEquals(KILOGRAM, m.getUnit()); assertEquals("kg", m.getUnit().getSymbol()); assertEquals("10 kg", m.toString()); }
@Test public void testTime() { Quantity<Time> t = DefaultQuantityFactory.getInstance(Time.class).create(40, MINUTE); // 40 min assertEquals(Integer.valueOf(40), t.getValue()); assertEquals(MINUTE, t.getUnit()); assertNull(t.getUnit().getSymbol()); // assertEquals("s", t.getUnit().getSymbol()); // FIXME this should be "min", tweak for // TransformedUnit assertEquals("40 min", t.toString()); }
public static void main(String[] args) { Unit<Mass> atomicMassUnit = UNIFIED_ATOMIC_MASS; System.out.println( atomicMassUnit + " (" + atomicMassUnit.getName() + "; " + atomicMassUnit.getSymbol() + ")"); AbstractQuantity<Mass> mass = NumberQuantity.of(10, atomicMassUnit); System.out.println(mass); Quantity<Mass> massInKg = mass.to(KILOGRAM); System.out.println(massInKg); System.out.println(WATT_PER_STERADIAN); System.out.println(WATT_PER_STERADIAN_PER_SQUARE_METRE); Quantity<Angle> angle = Quantities.getQuantity(Math.PI, SI.RADIAN); System.out.println(angle.to(NonSI.DEGREE_ANGLE)); }
/** @param args */ @SuppressWarnings({"unchecked", "rawtypes"}) public static void main(String[] args) { Quantity<Length> length = Quantities.getQuantity(10, SI.METRE); // LengthAmount length = new LengthAmount(10, SI.KILOGRAM); // this won't work ;-) System.out.println(length); Unit<Length> lenUnit = length.getUnit(); System.out.println(lenUnit); System.out.println(((AbstractQuantity) length).doubleValue(USCustomary.FOOT)); // System.out.println(length.doubleValue(USCustomary.POUND)); // this won't work either. // UnitConverter footConv = lenUnit.getConverterTo(USCustomary.INCH); System.out.print(((AbstractQuantity<Length>) length).doubleValue(USCustomary.INCH)); System.out.println(" " + USCustomary.FOOT); Quantity<Mass> mass = Quantities.getQuantity(1000, SI.GRAM); Quantity<Mass> mass2 = NumberQuantity.of(1, SI.KILOGRAM); System.out.println(mass.equals(mass2)); }
@Test public void getQuantityTest() { Quantity<Pressure> bigDecimalQuantity = Quantities.getQuantity(BigDecimal.ONE, PASCAL); Quantity<Pressure> bigIntegerQuantity = Quantities.getQuantity(BigInteger.ONE, PASCAL); Quantity<Pressure> shortQuantity = Quantities.getQuantity(Short.valueOf("2"), PASCAL); Quantity<Pressure> byteQuantity = Quantities.getQuantity(Byte.valueOf("2"), PASCAL); Quantity<Pressure> longQuantity = Quantities.getQuantity(Long.valueOf("2"), PASCAL); Quantity<Pressure> intQuantity = Quantities.getQuantity(Integer.valueOf("2"), PASCAL); Quantity<Pressure> floatQuantity = Quantities.getQuantity(Float.valueOf("2"), PASCAL); Quantity<Pressure> doubleQuantity = Quantities.getQuantity(Double.valueOf("2"), PASCAL); assertTrue(Short.class.isInstance(shortQuantity.getValue())); assertTrue(Byte.class.isInstance(byteQuantity.getValue())); assertTrue(Long.class.isInstance(longQuantity.getValue())); assertTrue(Integer.class.isInstance(intQuantity.getValue())); assertTrue(Float.class.isInstance(floatQuantity.getValue())); assertTrue(Double.class.isInstance(doubleQuantity.getValue())); assertTrue(BigDecimal.class.isInstance(bigIntegerQuantity.getValue())); assertTrue(BigDecimal.class.isInstance(bigDecimalQuantity.getValue())); assertTrue(NumberQuantity.class.isInstance(shortQuantity)); assertTrue(NumberQuantity.class.isInstance(byteQuantity)); assertTrue(NumberQuantity.class.isInstance(longQuantity)); assertTrue(NumberQuantity.class.isInstance(intQuantity)); assertTrue(NumberQuantity.class.isInstance(floatQuantity)); assertTrue(DoubleQuantity.class.isInstance(doubleQuantity)); assertTrue(DecimalQuantity.class.isInstance(bigIntegerQuantity)); assertTrue(DecimalQuantity.class.isInstance(bigDecimalQuantity)); }
@Test public void testOf() { Quantity<Pressure> pressure = Quantities.getQuantity(BigDecimal.ONE, PASCAL); assertEquals(PASCAL, pressure.getUnit()); }