/**
* Copyright (C) 2018 Adrien Hopkins
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see
* {@code BaseUnit} does not have any public constructors or static factories. There are two ways to obtain * {@code BaseUnit} instances. *
* BaseUnit JOULE = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); ** *
* BaseUnit JOULE = SI.SI.getBaseUnit(StandardDimensions.ENERGY); ** *
* Two units can be divided if they are part of the same unit system. If {@code divisor} does not meet this * condition, an {@code IllegalArgumentException} should be thrown. *
* * @param divisor * unit to divide by * @return quotient of two units * @throws IllegalArgumentException * if {@code divisor} is not compatible for division as described above * @throws NullPointerException * if {@code divisor} is null * @since 2018-12-22 * @since v0.1.0 */ public BaseUnit dividedBy(final BaseUnit divisor) { Objects.requireNonNull(divisor, "other must not be null."); // check that these units can be multiplied if (!this.getSystem().equals(divisor.getSystem())) throw new IllegalArgumentException( String.format("Incompatible units for division \"%s\" and \"%s\".", this, divisor)); return new BaseUnit(this.getDimension().dividedBy(divisor.getDimension()), this.getSystem()); } /** * @return true if the unit is a "full base" unit like the metre or second. * @since 2019-04-10 */ public final boolean isFullBase() { return this.isFullBase; } /** * Returns the product of this unit and another. ** Two units can be multiplied if they are part of the same unit system. If {@code multiplier} does not meet this * condition, an {@code IllegalArgumentException} should be thrown. *
* * @param multiplier * unit to multiply by * @return product of two units * @throws IllegalArgumentException * if {@code multiplier} is not compatible for multiplication as described above * @throws NullPointerException * if {@code multiplier} is null * @since 2018-12-22 * @since v0.1.0 */ public BaseUnit times(final BaseUnit multiplier) { Objects.requireNonNull(multiplier, "other must not be null"); // check that these units can be multiplied if (!this.getSystem().equals(multiplier.getSystem())) throw new IllegalArgumentException( String.format("Incompatible units for multiplication \"%s\" and \"%s\".", this, multiplier)); // multiply the units return new BaseUnit(this.getDimension().times(multiplier.getDimension()), this.getSystem()); } /** * Returns this unit, but to an exponent. * * @param exponent * exponent * @return result of exponentiation * @since 2019-01-15 * @since v0.1.0 */ @Override public BaseUnit toExponent(final int exponent) { return this.getSystem().getBaseUnit(this.getDimension().toExponent(exponent)); } @Override public String toString() { return String.format("%s base unit of%s dimension %s", this.getSystem(), this.isFullBase ? " base" : "", this.getDimension()); } }