* this = conversionFactor * getBase() ** * @since 2019-10-16 * @since v0.3.0 */ private final double conversionFactor; /** * Creates the {@code LinearUnit}. * * @param unitBase base of linear unit * @param conversionFactor conversion factor between base and unit * @since 2019-10-16 * @since v0.3.0 */ private LinearUnit(final ObjectProduct
* Two units can be subtracted if they have the same base. Note that * {@link #canConvertTo} can be used to determine this. If {@code subtrahend} * does not meet this condition, an {@code IllegalArgumentException} will be * thrown. *
* * @param subtrahend unit to subtract * @return difference of units * @throws IllegalArgumentException if {@code subtrahend} is not compatible * for subtraction as described above * @throws NullPointerException if {@code subtrahend} is null * @since 2019-03-17 * @since v0.2.0 */ public LinearUnit minus(final LinearUnit subtrahend) { Objects.requireNonNull(subtrahend, "addend must not be null."); // reject subtrahends that cannot be added to this unit if (!this.getBase().equals(subtrahend.getBase())) throw new IllegalArgumentException(String.format( "Incompatible units for subtraction \"%s\" and \"%s\".", this, subtrahend)); // subtract the units return valueOf(this.getBase(), this.getConversionFactor() - subtrahend.getConversionFactor()); } /** * Returns the sum of this unit and another. ** Two units can be added if they have the same base. Note that * {@link #canConvertTo} can be used to determine this. If {@code addend} * does not meet this condition, an {@code IllegalArgumentException} will be * thrown. *
* * @param addend unit to add * @return sum of units * @throws IllegalArgumentException if {@code addend} is not compatible for * addition as described above * @throws NullPointerException if {@code addend} is null * @since 2019-03-17 * @since v0.2.0 */ public LinearUnit plus(final LinearUnit addend) { Objects.requireNonNull(addend, "addend must not be null."); // reject addends that cannot be added to this unit if (!this.getBase().equals(addend.getBase())) throw new IllegalArgumentException(String.format( "Incompatible units for addition \"%s\" and \"%s\".", this, addend)); // add the units return valueOf(this.getBase(), this.getConversionFactor() + addend.getConversionFactor()); } /** * Multiplies this unit by a scalar. * * @param multiplier scalar to multiply by * @return product * @since 2018-12-23 * @since v0.1.0 */ public LinearUnit times(final double multiplier) { return valueOf(this.getBase(), this.getConversionFactor() * multiplier); } /** * Returns the product of this unit and another. * * @param multiplier unit to multiply by * @return product of two units * @throws NullPointerException if {@code multiplier} is null * @since 2018-12-22 * @since v0.1.0 */ public LinearUnit times(final LinearUnit multiplier) { Objects.requireNonNull(multiplier, "other must not be null"); // multiply the units final var base = this.getBase().times(multiplier.getBase()); return valueOf(base, this.getConversionFactor() * multiplier.getConversionFactor()); } @Override public String toDefinitionString() { return Double.toString(this.conversionFactor) + (this.getBase().equals(ObjectProduct.empty()) ? "" : " " + this.getBase().toString(BaseUnit::getShortName)); } /** * Returns this unit but to an exponent. * * @param exponent exponent to exponentiate unit to * @return exponentiated unit * @since 2019-01-15 * @since v0.1.0 */ public LinearUnit toExponent(final int exponent) { return valueOf(this.getBase().toExponent(exponent), Math.pow(this.conversionFactor, exponent)); } /** * Returns this unit to an exponent, rounding the resulting dimensions to the * nearest integer. * * @param exponent exponent to raise unit to * @return result of rounded exponentation * @since 2024-08-22 * @since v1.0.0 * @see ObjectProduct#toExponentRounded */ public LinearUnit toExponentRounded(final double exponent) { return valueOf(this.getBase().toExponentRounded(exponent), Math.pow(this.conversionFactor, exponent)); } @Override public LinearUnit withName(final NameSymbol ns) { return valueOf(this.getBase(), this.getConversionFactor(), ns); } /** * Returns the result of applying {@code prefix} to this unit. *
* If this unit and the provided prefix have a primary name, the returned
* unit will have a primary name (prefix's name + unit's name).
* If this unit and the provided prefix have a symbol, the returned unit will
* have a symbol.
* This method ignores alternate names of both this unit and the provided
* prefix.
*
* @param prefix prefix to apply
* @return unit with prefix
* @since 2019-03-18
* @since v0.2.0
* @throws NullPointerException if prefix is null
*/
public LinearUnit withPrefix(final UnitPrefix prefix) {
final var unit = this.times(prefix.getMultiplier());
// create new name and symbol, if possible
final String name;
if (this.getPrimaryName().isPresent()
&& prefix.getPrimaryName().isPresent()) {
name = prefix.getPrimaryName().get() + this.getPrimaryName().get();
} else {
name = null;
}
final String symbol;
if (this.getSymbol().isPresent() && prefix.getSymbol().isPresent()) {
symbol = prefix.getSymbol().get() + this.getSymbol().get();
} else {
symbol = null;
}
return unit.withName(NameSymbol.ofNullable(name, symbol));
}
}