diff options
Diffstat (limited to 'src/org/unitConverter/unit/UnitPrefix.java')
-rw-r--r-- | src/org/unitConverter/unit/UnitPrefix.java | 104 |
1 files changed, 89 insertions, 15 deletions
diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java index 9f9645d..514fa1c 100644 --- a/src/org/unitConverter/unit/UnitPrefix.java +++ b/src/org/unitConverter/unit/UnitPrefix.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018 Adrien Hopkins + * Copyright (C) 2019 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 @@ -16,14 +16,57 @@ */ package org.unitConverter.unit; +import org.unitConverter.math.DecimalComparison; + /** - * A prefix that can be attached onto the front of any unit, which multiplies it by a certain value + * A prefix that can be applied to a {@code LinearUnit} to multiply it by some value * * @author Adrien Hopkins - * @since 2019-01-14 - * @since v0.1.0 + * @since 2019-10-16 */ -public interface UnitPrefix { +public final class UnitPrefix { + /** + * Gets a {@code UnitPrefix} from a multiplier + * + * @param multiplier + * multiplier of prefix + * @return prefix + * @since 2019-10-16 + */ + public static UnitPrefix valueOf(final double multiplier) { + return new UnitPrefix(multiplier); + } + + /** + * The number that this prefix multiplies units by + * + * @since 2019-10-16 + */ + private final double multiplier; + + /** + * Creates the {@code DefaultUnitPrefix}. + * + * @param multiplier + * @since 2019-01-14 + * @since v0.2.0 + */ + private UnitPrefix(final double multiplier) { + this.multiplier = multiplier; + } + + /** + * Divides this prefix by a scalar + * + * @param divisor + * number to divide by + * @return quotient of prefix and scalar + * @since 2019-10-16 + */ + public UnitPrefix dividedBy(final double divisor) { + return valueOf(this.getMultiplier() / divisor); + } + /** * Divides this prefix by {@code other}. * @@ -33,16 +76,42 @@ public interface UnitPrefix { * @since 2019-04-13 * @since v0.2.0 */ - default UnitPrefix dividedBy(final UnitPrefix other) { - return new DefaultUnitPrefix(this.getMultiplier() / other.getMultiplier()); + public UnitPrefix dividedBy(final UnitPrefix other) { + return valueOf(this.getMultiplier() / other.getMultiplier()); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof UnitPrefix)) + return false; + final UnitPrefix other = (UnitPrefix) obj; + return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier()); + } + + public double getMultiplier() { + return this.multiplier; + } + + @Override + public int hashCode() { + return DecimalComparison.hash(this.getMultiplier()); } /** - * @return this prefix's multiplier - * @since 2019-01-14 - * @since v0.1.0 + * Multiplies this prefix by a scalar + * + * @param multiplicand + * number to multiply by + * @return product of prefix and scalar + * @since 2019-10-16 */ - double getMultiplier(); + public UnitPrefix times(final double multiplicand) { + return valueOf(this.getMultiplier() * multiplicand); + } /** * Multiplies this prefix by {@code other}. @@ -53,8 +122,8 @@ public interface UnitPrefix { * @since 2019-04-13 * @since v0.2.0 */ - default UnitPrefix times(final UnitPrefix other) { - return new DefaultUnitPrefix(this.getMultiplier() * other.getMultiplier()); + public UnitPrefix times(final UnitPrefix other) { + return valueOf(this.getMultiplier() * other.getMultiplier()); } /** @@ -66,7 +135,12 @@ public interface UnitPrefix { * @since 2019-04-13 * @since v0.2.0 */ - default UnitPrefix toExponent(final double exponent) { - return new DefaultUnitPrefix(Math.pow(getMultiplier(), exponent)); + public UnitPrefix toExponent(final double exponent) { + return valueOf(Math.pow(this.getMultiplier(), exponent)); + } + + @Override + public String toString() { + return String.format("Unit prefix equal to %s", this.multiplier); } } |