Search in sources :

Example 11 with Dimension

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().
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) HashMap(java.util.HashMap) Fraction(org.apache.sis.math.Fraction) Dimension(javax.measure.Dimension) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 12 with Dimension

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());
}
Also used : Dimension(javax.measure.Dimension)

Example 13 with Dimension

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;
}
Also used : UnitConverter(javax.measure.UnitConverter) Dimension(javax.measure.Dimension)

Example 14 with Dimension

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);
}
Also used : Fraction(org.apache.sis.math.Fraction) Dimension(javax.measure.Dimension) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) UnsupportedImplementationException(org.apache.sis.util.UnsupportedImplementationException)

Example 15 with Dimension

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;
}
Also used : Dimension(javax.measure.Dimension) QuantityDimension(tec.uom.se.quantity.QuantityDimension)

Aggregations

Dimension (javax.measure.Dimension)15 Test (org.junit.Test)7 Map (java.util.Map)3 QuantityDimension (tech.units.indriya.quantity.QuantityDimension)3 Fraction (org.apache.sis.math.Fraction)2 QuantityDimension (tec.uom.se.quantity.QuantityDimension)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 UnitConverter (javax.measure.UnitConverter)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 UnsupportedImplementationException (org.apache.sis.util.UnsupportedImplementationException)1 Test (org.junit.jupiter.api.Test)1