* this = conversionFactor * getBase() ** * @since 2019-10-16 */ 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 */ 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 subtrahendend) { Objects.requireNonNull(subtrahendend, "addend must not be null."); // reject subtrahends that cannot be added to this unit if (!this.getBase().equals(subtrahendend.getBase())) throw new IllegalArgumentException( String.format("Incompatible units for subtraction \"%s\" and \"%s\".", this, subtrahendend)); // add the units return valueOf(this.getBase(), this.getConversionFactor() - subtrahendend.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 ObjectProduct