Java源码示例:javax.measure.Dimension

示例1
/**
 * 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);
}
 
示例2
/**
 * 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().
}
 
示例3
/**
 * Returns the units defined in this system having the specified dimension, or an empty set if none.
 */
@Override
public Set<Unit<?>> getUnits(final Dimension dimension) {
    ArgumentChecks.ensureNonNull("dimension", dimension);
    final Set<Unit<?>> filtered = new HashSet<>();
    for (final Unit<?> unit : getUnits()) {
        if (dimension.equals(unit.getDimension())) {
            filtered.add(unit);
        }
    }
    return filtered;
}
 
示例4
/**
 * Implementation of {@link #multiply(Unit)} and {@link #divide(Unit)} methods.
 *
 * @param  inverse  wether to use the inverse of {@code other}.
 */
private <T extends Quantity<T>> Unit<?> product(final Unit<T> other, final boolean inverse) {
    final Unit<T> intermediate = other.getSystemUnit();
    final Dimension dim = intermediate.getDimension();
    final UnitDimension newDimension;
    final char operation;
    if (inverse) {
        operation = DIVIDE;
        newDimension = dimension.divide(dim);
    } else {
        operation = MULTIPLY;
        newDimension = dimension.multiply(dim);
    }
    final boolean transformed = (intermediate != other);
    Unit<?> result = create(newDimension, operation, transformed ? null : other);
    if (transformed) {
        UnitConverter c = other.getConverterTo(intermediate);
        if (!c.isLinear()) {
            throw new IllegalArgumentException(Errors.format(Errors.Keys.NonRatioUnit_1, other));
        }
        if (!c.isIdentity()) {
            if (inverse) c = c.inverse();
            result = result.transform(c);
            /*
             * If the system unit product is an Apache SIS implementation, try to infer a unit symbol
             * to be given to our customized 'transform' method. Otherwise fallback on standard API.
             */
            result = inferSymbol(result, operation, other);
        }
    }
    return result;
}
 
示例5
/**
 * Returns {@code true} if the given dimension has no components.
 */
static boolean isDimensionless(final Dimension dim) {
    if (dim instanceof UnitDimension) {
        return ((UnitDimension) dim).isDimensionless();
    } else if (dim != null) {
        // Fallback for foreigner implementations.
        final Map<? extends Dimension, Integer> bases = dim.getBaseDimensions();
        if (bases != null) return bases.isEmpty();
    }
    return false;       // Unit is a base unit (not a product of existing units).
}
 
示例6
/**
 * Tests {@link UnitDimension#multiply(Dimension)}.
 */
@Test
@DependsOnMethod("testEqualsAndHashCode")
public void testMultiply() {
    assertSame(LENGTH, LENGTH.multiply(DIMENSIONLESS));
    assertSame(AREA,   LENGTH.multiply(LENGTH));
    assertSame(VOLUME, LENGTH.multiply(AREA));
    assertSame(VOLUME, AREA  .multiply(LENGTH));

    final Map<Dimension,Integer> expected = new HashMap<>(4);
    assertNull(expected.put(LENGTH, 1));
    assertNull(expected.put(TIME,   1));
    assertMapEquals(expected, LENGTH.multiply(TIME).getBaseDimensions());
}
 
示例7
/**
 * Tests {@link UnitDimension#getBaseDimensions()}. This method indirectly tests the results
 * of {@link UnitDimension#multiply(Dimension)}, {@link UnitDimension#divide(Dimension)} and
 * {@link UnitDimension#pow(int)} since this test uses constants that were created with above
 * operations.
 */
@Test
public void testGetBaseDimensions() {
    assertNull("LENGTH",        LENGTH       .getBaseDimensions());     // Null value as per JSR-363 specification.
    assertNull("TIME",          TIME         .getBaseDimensions());
    assertTrue("DIMENSIONLESS", DIMENSIONLESS.getBaseDimensions().isEmpty());
    assertMapEquals(Collections.singletonMap(LENGTH, 3), VOLUME.getBaseDimensions());

    final Map<Dimension,Integer> expected = new HashMap<>(4);
    assertNull(expected.put(MASS,    1));
    assertNull(expected.put(LENGTH,  1));
    assertNull(expected.put(TIME,   -2));
    assertMapEquals(expected, FORCE.getBaseDimensions());
}
 
示例8
@Override
public Dimension getDimension() {
	return null;
}
 
示例9
public Dimension getDimension() {
    return getUnit().getDimension();
}
 
示例10
public Dimension getDimension() {
    return getUnit().getDimension();
}
 
示例11
/**
 * Returns the specific detectivity dimension, which is T^2.5 / (M⋅L).
 */
static Dimension specificDetectivity() {
    return TIME.pow(2).divide(MASS.multiply(LENGTH)).multiply(TIME.root(2));
}
 
示例12
/**
 * 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());
}
 
示例13
/**
 * Returns the dimension of this unit.
 * Two units {@code u1} and {@code u2} are {@linkplain #isCompatible(Unit) compatible}
 * if and only if {@code u1.getDimension().equals(u2.getDimension())}.
 *
 * @return the dimension of this unit.
 *
 * @see #isCompatible(Unit)
 */
@Override
public Dimension getDimension() {
    return dimension;
}
 
示例14
/**
 * Returns the system unit for the given dimension, or {@code null} if none.
 * Note that this method can not distinguish the different kinds of dimensionless units.
 * If the symbol or the quantity type is known, use {@link #get(String)} or {@link #get(Class)} instead.
 *
 * <p><b>Implementation note:</b> this method must be defined in this {@code Units} class
 * in order to force a class initialization before use.</p>
 */
static SystemUnit<?> get(final Dimension dim) {
    return (SystemUnit<?>) UnitRegistry.get(dim);
}
 
示例15
/**
 * Returns the dimension of this unit.
 * Two units {@code u1} and {@code u2} are {@linkplain #isCompatible(Unit) compatible}
 * if and only if {@code u1.getDimension().equals(u2.getDimension())}.
 *
 * @return the dimension of this unit.
 *
 * @see #isCompatible(Unit)
 */
@Override
public Dimension getDimension() {
    return target.getDimension();
}
 
示例16
/**
 * Returns the product of this dimension with the one specified.
 *
 * @param  multiplicand  the dimension by which to multiply this dimension.
 * @return {@code this} × {@code multiplicand}
 */
@Override
public UnitDimension multiply(final Dimension multiplicand) {
    return combine(multiplicand, false);
}
 
示例17
/**
 * Returns the quotient of this dimension with the one specified.
 *
 * @param  divisor  the dimension by which to divide this dimension.
 * @return {@code this} ∕ {@code divisor}
 */
@Override
public UnitDimension divide(final Dimension divisor) {
    return combine(divisor, true);
}