diff options
Diffstat (limited to 'src/unitConverter/unit')
-rwxr-xr-x | src/unitConverter/unit/BaseUnit.java | 76 | ||||
-rw-r--r-- | src/unitConverter/unit/LinearUnit.java | 72 |
2 files changed, 147 insertions, 1 deletions
diff --git a/src/unitConverter/unit/BaseUnit.java b/src/unitConverter/unit/BaseUnit.java index 339b7bf..46316bf 100755 --- a/src/unitConverter/unit/BaseUnit.java +++ b/src/unitConverter/unit/BaseUnit.java @@ -16,6 +16,8 @@ */ package unitConverter.unit; +import java.util.Objects; + import unitConverter.dimension.UnitDimension; /** @@ -61,6 +63,80 @@ public final class BaseUnit extends AbstractUnit { return value; } + /** + * Divides this unit by another unit. + * + * @param other + * unit to divide by + * @return quotient of two units + * @throws IllegalArgumentException + * if this unit's system is not other's system + * @throws NullPointerException + * if other is null + * @since 2018-12-22 + */ + public BaseUnit dividedBy(final BaseUnit other) { + Objects.requireNonNull(other, "other must not be null."); + if (!this.getSystem().equals(other.getSystem())) + throw new IllegalArgumentException("Incompatible base units for division."); + return new BaseUnit(this.getDimension().dividedBy(other.getDimension()), this.getSystem()); + } + + /** + * Divides this unit by a divisor + * + * @param divisor + * amount to divide by + * @return quotient + * @since 2018-12-23 + */ + public LinearUnit dividedBy(final double divisor) { + return new LinearUnit(this, 1 / divisor); + } + + /** + * Multiplies this unit by another unit. + * + * @param other + * unit to multiply by + * @return product of two units + * @throws IllegalArgumentException + * if this unit's system is not other's system + * @throws NullPointerException + * if other is null + * @since 2018-12-22 + */ + public BaseUnit times(final BaseUnit other) { + Objects.requireNonNull(other, "other must not be null."); + if (!this.getSystem().equals(other.getSystem())) + throw new IllegalArgumentException("Incompatible base units for multiplication."); + return new BaseUnit(this.getDimension().times(other.getDimension()), this.getSystem()); + } + + /** + * Multiplies this unit by a multiplier. + * + * @param multiplier + * amount to multiply by + * @return product + * @since 2018-12-23B + */ + public LinearUnit times(final double multiplier) { + return new LinearUnit(this, multiplier); + } + + /** + * Returns this unit, but to an exponent. + * + * @param exponent + * exponent + * @return result of exponentiation + * @since 2019-01-15 + */ + public BaseUnit toExponent(final int exponent) { + return this.toExponent(exponent); + } + @Override public String toString() { return String.format("%s base unit of%s dimension %s", this.getSystem(), this.isFullBase ? " base" : "", diff --git a/src/unitConverter/unit/LinearUnit.java b/src/unitConverter/unit/LinearUnit.java index 229710c..e2c9eb2 100644 --- a/src/unitConverter/unit/LinearUnit.java +++ b/src/unitConverter/unit/LinearUnit.java @@ -16,6 +16,8 @@ */ package unitConverter.unit; +import java.util.Objects; + import unitConverter.dimension.UnitDimension; /** @@ -24,7 +26,7 @@ import unitConverter.dimension.UnitDimension; * @author Adrien Hopkins * @since 2019-01-25 */ -public class LinearUnit extends AbstractUnit { +public final class LinearUnit extends AbstractUnit { /** * The value of one of this unit in this unit's base unit * @@ -72,6 +74,34 @@ public class LinearUnit extends AbstractUnit { } /** + * Divides this unit by a scalar. + * + * @param divisor + * scalar to divide by + * @return quotient + * @since 2018-12-23 + */ + public LinearUnit dividedBy(final double divisor) { + return new LinearUnit(this.getBase(), this.getConversionFactor() / divisor); + } + + /** + * Divides this unit by another unit. + * + * @param other + * unit to divide by + * @return quotient of two units + * @throws NullPointerException + * if other is null + * @since 2018-12-22 + */ + public LinearUnit dividedBy(final LinearUnit other) { + Objects.requireNonNull(other, "other must not be null"); + final BaseUnit base = this.getBase().dividedBy(other.getBase()); + return new LinearUnit(base, this.getConversionFactor() / other.getConversionFactor()); + } + + /** * @return conversionFactor * @since 2019-01-25 */ @@ -79,6 +109,46 @@ public class LinearUnit extends AbstractUnit { return this.conversionFactor; } + /** + * Multiplies this unit by a scalar. + * + * @param multiplier + * scalar to multiply by + * @return product + * @since 2018-12-23 + */ + public LinearUnit times(final double multiplier) { + return new LinearUnit(this.getBase(), this.getConversionFactor() * multiplier); + } + + /** + * Multiplies this unit by another unit. + * + * @param other + * unit to multiply by= + * @return product of two units + * @throws NullPointerException + * if other is null + * @since 2018-12-22 + */ + public LinearUnit times(final LinearUnit other) { + Objects.requireNonNull(other, "other must not be null"); + final BaseUnit base = this.getBase().times(other.getBase()); + return new LinearUnit(base, this.getConversionFactor() * other.getConversionFactor()); + } + + /** + * Returns this unit but to an exponent. + * + * @param exponent + * exponent to exponientate unit to + * @return exponientated unit + * @since 2019-01-15 + */ + public LinearUnit toExponent(final int exponent) { + return new LinearUnit(this.getBase().toExponent(exponent), Math.pow(this.conversionFactor, exponent)); + } + @Override public String toString() { return super.toString() + String.format(" (equal to %s * base)", this.getConversionFactor()); |