diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-03-22 17:00:58 -0400 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-03-22 17:00:58 -0400 |
commit | 943496888d18b031be19ba8e7348ec188dc8eb6b (patch) | |
tree | 03b440bf7d7789be5fd88b8ce3785f900804c773 /src/org/unitConverter/unit/BaseUnit.java | |
parent | ea940f2c5b6450231ff9ce61f4b6704babdb0d9e (diff) |
Made BaseUnit a subclass of LinearUnit and made an expression parser
Diffstat (limited to 'src/org/unitConverter/unit/BaseUnit.java')
-rwxr-xr-x | src/org/unitConverter/unit/BaseUnit.java | 151 |
1 files changed, 30 insertions, 121 deletions
diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 894d338..2def48e 100755 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -28,7 +28,7 @@ import org.unitConverter.dimension.UnitDimension; * @since 2018-12-23 * @since v0.1.0 */ -public final class BaseUnit extends AbstractUnit implements OperatableUnit { +public final class BaseUnit extends LinearUnit { /** * Is this unit a full base (i.e. m, s, ... but not N, J, ...) * @@ -52,156 +52,65 @@ public final class BaseUnit extends AbstractUnit implements OperatableUnit { * @since v0.1.0 */ BaseUnit(final UnitDimension dimension, final UnitSystem system) { - super(dimension, system); + super(dimension, system, 1); this.isFullBase = dimension.isBase(); } /** - * @return this unit as a {@code LinearUnit} - * @since 2019-01-25 - * @since v0.1.0 - */ - public LinearUnit asLinearUnit() { - return this.times(1); - } - - @Override - public double convertFromBase(final double value) { - return value; - } - - @Override - public double convertToBase(final double value) { - return value; - } - - /** - * Divides this unit by another unit. + * Returns the quotient of this unit and another. + * <p> + * 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. + * </p> * - * @param other + * @param divisor * unit to divide by * @return quotient of two units * @throws IllegalArgumentException - * if this unit's system is not other's system + * if {@code divisor} is not compatible for division as described above * @throws NullPointerException - * if other is null + * if {@code divisor} is null * @since 2018-12-22 * @since v0.1.0 */ - 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()); - } + public BaseUnit dividedBy(final BaseUnit divisor) { + Objects.requireNonNull(divisor, "other must not be null."); - /** - * Divides this unit by a divisor - * - * @param divisor - * amount to divide by - * @return quotient - * @since 2018-12-23 - * @since v0.1.0 - */ - public LinearUnit dividedBy(final double divisor) { - return new LinearUnit(this, 1 / divisor); - } - - @Override - public boolean equals(final Object obj) { - if (!(obj instanceof BaseUnit)) - return false; - final BaseUnit other = (BaseUnit) obj; - return Objects.equals(this.getSystem(), other.getSystem()) - && Objects.equals(this.getDimension(), other.getDimension()); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = result * prime + this.getSystem().hashCode(); - result = result * prime + this.getDimension().hashCode(); - return result; - } - - @Override - public LinearUnit negated() { - return this.times(-1); - } - - @Override - public OperatableUnit plus(final OperatableUnit addend) { - Objects.requireNonNull(addend, "addend must not be null."); - - // reject addends that cannot be added to this unit - if (!this.getSystem().equals(addend.getSystem())) - throw new IllegalArgumentException( - String.format("Incompatible units for addition or subtraction \"%s\" and \"%s\".", this, addend)); - if (!this.getDimension().equals(addend.getDimension())) + // check that these units can be multiplied + if (!this.getSystem().equals(divisor.getSystem())) throw new IllegalArgumentException( - String.format("Incompatible units for addition or subtraction \"%s\" and \"%s\".", this, addend)); - - // add them together - if (addend instanceof BaseUnit) - return this.times(2); - else - return addend.plus(this); - } + String.format("Incompatible units for division \"%s\" and \"%s\".", this, divisor)); - @Override - public BaseUnit reciprocal() { - return this.toExponent(-1); + return new BaseUnit(this.getDimension().dividedBy(divisor.getDimension()), this.getSystem()); } /** - * Multiplies this unit by another unit. + * Returns the product of this unit and another. + * <p> + * 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. + * </p> * - * @param other + * @param multiplier * unit to multiply by * @return product of two units * @throws IllegalArgumentException - * if this unit's system is not other's system + * if {@code multiplier} is not compatible for multiplication as described above * @throws NullPointerException - * if other is null + * if {@code multiplier} is null * @since 2018-12-22 * @since v0.1.0 */ - 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-23 - * @since v0.1.0 - */ - public LinearUnit times(final double multiplier) { - return new LinearUnit(this, multiplier); - } + public BaseUnit times(final BaseUnit multiplier) { + Objects.requireNonNull(multiplier, "other must not be null"); - @Override - public OperatableUnit times(final OperatableUnit multiplier) { - Objects.requireNonNull(multiplier, "multiplier must not be null."); - - // reject multipliers that cannot be muliplied by this unit + // check that these units can be multiplied if (!this.getSystem().equals(multiplier.getSystem())) - throw new IllegalArgumentException(String - .format("Incompatible units for multiplication or division \"%s\" and \"%s\".", this, multiplier)); + throw new IllegalArgumentException( + String.format("Incompatible units for multiplication \"%s\" and \"%s\".", this, multiplier)); // multiply the units - if (multiplier instanceof BaseUnit) - return new BaseUnit(this.getDimension().times(multiplier.getDimension()), this.getSystem()); - else - return multiplier.times(this); + return new BaseUnit(this.getDimension().times(multiplier.getDimension()), this.getSystem()); } /** |