use of javax.measure.Dimension in project sis by apache.
the class UnitDimensionTest method testRationalPower.
/**
* Tests a dimension with rational power. This tests use the specific detectivity, which dimension is T^2.5 / (M⋅L).
*/
@Test
@DependsOnMethod({ "testMultiply", "testDivide", "testPow", "testRoot" })
public void testRationalPower() {
final Dimension dim = specificDetectivity();
final Map<Dimension, Fraction> expected = new HashMap<>(4);
assertNull(expected.put(TIME, new Fraction(5, 2)));
assertNull(expected.put(MASS, new Fraction(-1, 1)));
assertNull(expected.put(LENGTH, new Fraction(-1, 1)));
assertMapEquals(expected, ((UnitDimension) dim).components);
try {
dim.getBaseDimensions().toString();
fail("Mapping from Fraction to Integer should not be allowed.");
} catch (UnconvertibleObjectException e) {
final String message = e.getMessage();
assertTrue(message, message.contains("Integer"));
}
// 'toString()' formatting tested in UnitFormatTest.testRationalPower().
}
use of javax.measure.Dimension in project sis by apache.
the class UnitDimensionTest method verifyEqualsAndHashCode.
/**
* Verifies that the test for equality between two dimensions produce the expected result.
* This method expects {@link Unit} instances instead than {@link Dimension} for convenience,
* but only the dimensions will be compared.
*
* @param message the message to show in case of failure.
* @param expected the expected result of {@link UnitDimension#equals(Object)}.
* @param a {@link Units} constant from which to get the first dimension to compare.
* @param b {@link Units} constant from which to get the first dimension to compare.
*/
private static void verifyEqualsAndHashCode(final String message, final boolean expected, final Unit<?> a, final Unit<?> b) {
final Dimension da = a.getDimension();
final Dimension db = b.getDimension();
assertEquals(message, expected, da.equals(db));
assertEquals(message, expected, db.equals(da));
assertEquals(message, expected, da.hashCode() == db.hashCode());
}
use of javax.measure.Dimension in project sis by apache.
the class SystemUnit method combine.
/**
* Implementation of {@link #multiply(Unit)} and {@link #divide(Unit)} methods.
*/
private <T extends Quantity<T>> Unit<?> combine(final Unit<T> other, final boolean divide) {
final Unit<T> step = other.getSystemUnit();
final Dimension dim = step.getDimension();
Unit<?> result = create(divide ? dimension.divide(dim) : dimension.multiply(dim));
if (step != other) {
UnitConverter c = other.getConverterTo(step);
if (!c.isLinear()) {
throw new IllegalArgumentException(Errors.format(Errors.Keys.NonRatioUnit_1, other));
}
if (divide)
c = c.inverse();
result = result.transform(c);
}
return result;
}
use of javax.measure.Dimension in project sis by apache.
the class UnitDimension method combine.
/**
* Returns the product or the quotient of this dimension with the specified one.
*
* @param other the dimension by which to multiply or divide this dimension.
* @param divide {@code false} for a multiplication, {@code true} for a division.
* @return the product or division of this dimension by the given dimension.
*/
private UnitDimension combine(final Dimension other, final boolean divide) {
final Map<UnitDimension, Fraction> product = new LinkedHashMap<>(components);
for (final Map.Entry<? extends Dimension, Fraction> entry : getBaseDimensions(other).entrySet()) {
final Dimension dim = entry.getKey();
Fraction p = entry.getValue();
if (divide) {
p = p.negate();
}
if (dim instanceof UnitDimension) {
product.merge((UnitDimension) dim, p, (sum, toAdd) -> {
sum = sum.add(toAdd);
return (sum.numerator != 0) ? sum : null;
});
} else if (p.numerator != 0) {
throw new UnsupportedImplementationException(Errors.format(Errors.Keys.UnsupportedImplementation_1, dim.getClass()));
}
}
return create(product);
}
use of javax.measure.Dimension in project uom-se by unitsofmeasurement.
the class ProductUnit method getDimension.
@Override
public Dimension getDimension() {
Dimension dimension = QuantityDimension.NONE;
for (int i = 0; i < this.getUnitCount(); i++) {
Unit<?> unit = this.getUnit(i);
if (this.elements != null && unit.getDimension() != null) {
Dimension d = unit.getDimension().pow(this.getUnitPow(i)).root(this.getUnitRoot(i));
dimension = dimension.multiply(d);
}
}
return dimension;
}
Aggregations