diff options
Diffstat (limited to 'src/unitConverter/unit')
-rw-r--r-- | src/unitConverter/unit/AbstractUnit.java | 172 | ||||
-rwxr-xr-x | src/unitConverter/unit/BaseUnit.java | 180 | ||||
-rwxr-xr-x | src/unitConverter/unit/DefaultUnitPrefix.java | 67 | ||||
-rw-r--r-- | src/unitConverter/unit/LinearUnit.java | 184 | ||||
-rwxr-xr-x | src/unitConverter/unit/NonlinearUnits.java | 57 | ||||
-rw-r--r-- | src/unitConverter/unit/SI.java | 74 | ||||
-rwxr-xr-x | src/unitConverter/unit/SIPrefix.java | 54 | ||||
-rwxr-xr-x | src/unitConverter/unit/Unit.java | 9 | ||||
-rwxr-xr-x | src/unitConverter/unit/UnitPrefix.java | 33 | ||||
-rwxr-xr-x | src/unitConverter/unit/UnitSystem.java | 24 | ||||
-rwxr-xr-x | src/unitConverter/unit/UnitTest.java | 47 |
11 files changed, 899 insertions, 2 deletions
diff --git a/src/unitConverter/unit/AbstractUnit.java b/src/unitConverter/unit/AbstractUnit.java new file mode 100644 index 0000000..24814e8 --- /dev/null +++ b/src/unitConverter/unit/AbstractUnit.java @@ -0,0 +1,172 @@ +/** + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import java.util.Objects; + +import unitConverter.dimension.UnitDimension; + +/** + * The default abstract implementation of the {@code Unit} interface. + * + * @author Adrien Hopkins + * @since 2018-12-22 + * @since v0.1.0 + */ +public abstract class AbstractUnit implements Unit { + /** + * The number of units created, including base units. + * + * @since 2019-01-02 + * @since v0.1.0 + */ + private static long unitCount = 0; + + /** + * The number of base units created. + * + * @since 2019-01-02 + * @since v0.1.0 + */ + private static long baseUnitCount = 0; + + /** + * @return number of base units created + * @since 2019-01-02 + * @since v0.1.0 + */ + public static final long getBaseUnitCount() { + return baseUnitCount; + } + + /** + * @return number of units created + * @since 2019-01-02 + * @since v0.1.0 + */ + public static final long getUnitCount() { + return unitCount; + } + + /** + * Increments the number of base units. + * + * @since 2019-01-15 + * @since v0.1.0 + */ + public static final void incrementBaseUnitCounter() { + baseUnitCount++; + } + + /** + * Increments the number of units. + * + * @since 2019-01-15 + * @since v0.1.0 + */ + public static final void incrementUnitCounter() { + unitCount++; + } + + /** + * The dimension, or what the unit measures. + * + * @since 2018-12-22 + * @since v0.1.0 + */ + private final UnitDimension dimension; + + /** + * The unit's base unit. Values converted by {@code convertFromBase} and {@code convertToBase} are expressed in this + * unit. + * + * @since 2018-12-22 + * @since v0.1.0 + */ + private final BaseUnit base; + + /** + * The system that this unit is a part of. + * + * @since 2018-12-23 + * @since v0.1.0 + */ + private final UnitSystem system; + + /** + * Creates the {@code AbstractUnit}. + * + * @param base + * unit's base + * @throws NullPointerException + * if name, symbol or base is null + * @since 2018-12-22 + * @since v0.1.0 + */ + public AbstractUnit(final BaseUnit base) { + this.base = Objects.requireNonNull(base, "base must not be null."); + this.dimension = this.base.getDimension(); + this.system = this.base.getSystem(); + } + + /** + * Creates the {@code AbstractUnit} using a unique dimension. This constructor is for making base units and should + * only be used by {@code BaseUnit}. + * + * @param dimension + * dimension measured by unit + * @param system + * system that unit is a part of + * @throws AssertionError + * if this constructor is not run by {@code BaseUnit} or a subclass + * @throws NullPointerException + * if name, symbol or dimension is null + * @since 2018-12-23 + * @since v0.1.0 + */ + AbstractUnit(final UnitDimension dimension, final UnitSystem system) { + // try to set this as a base unit + if (this instanceof BaseUnit) { + this.base = (BaseUnit) this; + } else + throw new AssertionError(); + + this.dimension = Objects.requireNonNull(dimension, "dimension must not be null."); + this.system = Objects.requireNonNull(system, "system must not be null."); + } + + @Override + public final BaseUnit getBase() { + return this.base; + } + + @Override + public final UnitDimension getDimension() { + return this.dimension; + } + + @Override + public final UnitSystem getSystem() { + return this.system; + } + + // TODO document and revise units' toString methods + @Override + public String toString() { + return String.format("%s-derived unit of dimension %s", this.getSystem(), this.getDimension()); + } +} diff --git a/src/unitConverter/unit/BaseUnit.java b/src/unitConverter/unit/BaseUnit.java new file mode 100755 index 0000000..fe36c45 --- /dev/null +++ b/src/unitConverter/unit/BaseUnit.java @@ -0,0 +1,180 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import java.util.Objects; + +import unitConverter.dimension.UnitDimension; + +/** + * A unit that is the base for its dimension. It does not have to be for a base dimension, so units like the Newton and + * Joule are still base units. + * + * @author Adrien Hopkins + * @since 2018-12-23 + * @since v0.1.0 + */ +public final class BaseUnit extends AbstractUnit { + /** + * Is this unit a full base (i.e. m, s, ... but not N, J, ...) + * + * @since 2019-01-15 + * @since v0.1.0 + */ + private final boolean isFullBase; + + /** + * Creates the {@code BaseUnit}. + * + * @param dimension + * dimension measured by unit + * @param system + * system that unit is a part of + * @param name + * name of unit + * @param symbol + * symbol of unit + * @since 2018-12-23 + * @since v0.1.0 + */ + BaseUnit(final UnitDimension dimension, final UnitSystem system) { + super(dimension, system); + 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. + * + * @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 + * @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()); + } + + /** + * 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; + } + + /** + * 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 + * @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); + } + + /** + * Returns this unit, but to an exponent. + * + * @param exponent + * exponent + * @return result of exponentiation + * @since 2019-01-15 + * @since v0.1.0 + */ + public BaseUnit toExponent(final int exponent) { + return this.getSystem().getBaseUnit(this.getDimension().toExponent(exponent)); + } + + @Override + public String toString() { + return String.format("%s base unit of%s dimension %s", this.getSystem(), this.isFullBase ? " base" : "", + this.getDimension()); + } +} diff --git a/src/unitConverter/unit/DefaultUnitPrefix.java b/src/unitConverter/unit/DefaultUnitPrefix.java new file mode 100755 index 0000000..d19161b --- /dev/null +++ b/src/unitConverter/unit/DefaultUnitPrefix.java @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import java.util.Objects; + +/** + * The default implementation of {@code UnitPrefix}, which contains a multiplier and nothing else. + * + * @author Adrien Hopkins + * @since 2019-01-14 + * @since v0.1.0 + */ +public final class DefaultUnitPrefix implements UnitPrefix { + private final double multiplier; + + /** + * Creates the {@code DefaultUnitPrefix}. + * + * @param multiplier + * @since 2019-01-14 + */ + public DefaultUnitPrefix(final double multiplier) { + this.multiplier = multiplier; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof DefaultUnitPrefix)) + return false; + final DefaultUnitPrefix other = (DefaultUnitPrefix) obj; + return Double.doubleToLongBits(this.multiplier) == Double.doubleToLongBits(other.multiplier); + } + + @Override + public double getMultiplier() { + return this.multiplier; + } + + @Override + public int hashCode() { + return Objects.hash(this.multiplier); + } + + @Override + public String toString() { + return String.format("Unit prefix equal to %s", this.multiplier); + } +} diff --git a/src/unitConverter/unit/LinearUnit.java b/src/unitConverter/unit/LinearUnit.java new file mode 100644 index 0000000..b786b3b --- /dev/null +++ b/src/unitConverter/unit/LinearUnit.java @@ -0,0 +1,184 @@ +/** + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import java.util.Objects; + +import unitConverter.dimension.UnitDimension; + +/** + * A unit that is equal to a certain number multiplied by its base. + * + * @author Adrien Hopkins + * @since 2018-12-22 + * @since v0.1.0 + */ +public final class LinearUnit extends AbstractUnit { + /** + * The value of one of this unit in this unit's base unit + * + * @since 2018-12-22 + * @since v0.1.0 + */ + private final double conversionFactor; + + /** + * + * Creates the {@code LinearUnit}. + * + * @param base + * unit's base + * @param conversionFactor + * value of one of this unit in its base + * @since 2018-12-23 + * @since v0.1.0 + */ + LinearUnit(final BaseUnit base, final double conversionFactor) { + super(base); + this.conversionFactor = conversionFactor; + } + + /** + * Creates the {@code LinearUnit} as a base unit. + * + * @param dimension + * dimension measured by unit + * @param system + * system unit is part of + * @since 2019-01-25 + * @since v0.1.0 + */ + LinearUnit(final UnitDimension dimension, final UnitSystem system, final double conversionFactor) { + super(dimension, system); + this.conversionFactor = conversionFactor; + } + + @Override + public double convertFromBase(final double value) { + return value / this.getConversionFactor(); + } + + @Override + public double convertToBase(final double value) { + return value * this.getConversionFactor(); + } + + /** + * Divides this unit by a scalar. + * + * @param divisor + * scalar to divide by + * @return quotient + * @since 2018-12-23 + * @since v0.1.0 + */ + 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 + * @since v0.1.0 + */ + 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()); + } + + @Override + public boolean equals(final Object obj) { + if (!(obj instanceof LinearUnit)) + return false; + final LinearUnit other = (LinearUnit) obj; + return Objects.equals(this.getBase(), other.getBase()) + && Objects.equals(this.getConversionFactor(), other.getConversionFactor()); + } + + /** + * @return conversionFactor + * @since 2018-12-22 + * @since v0.1.0 + */ + public final double getConversionFactor() { + return this.conversionFactor; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = result * prime + this.getBase().hashCode(); + result = result * prime + Double.hashCode(this.getConversionFactor()); + return result; + } + + /** + * 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 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 + * @since v0.1.0 + */ + 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 + * @since v0.1.0 + */ + 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()); + } +} diff --git a/src/unitConverter/unit/NonlinearUnits.java b/src/unitConverter/unit/NonlinearUnits.java new file mode 100755 index 0000000..ec1874c --- /dev/null +++ b/src/unitConverter/unit/NonlinearUnits.java @@ -0,0 +1,57 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +/** + * Some major nonlinear units. + * + * @author Adrien Hopkins + * @since 2019-01-14 + * @since v0.1.0 + */ +public final class NonlinearUnits { + public static final Unit CELSIUS = new AbstractUnit(SI.KELVIN) { + + @Override + public double convertFromBase(final double value) { + return value - 273.15; + } + + @Override + public double convertToBase(final double value) { + return value + 273.15; + } + }; + + public static final Unit FAHRENHEIT = new AbstractUnit(SI.KELVIN) { + + @Override + public double convertFromBase(final double value) { + return 1.8 * value - 459.67; + } + + @Override + public double convertToBase(final double value) { + return (value + 459.67) / 1.8; + } + }; + + // You may NOT get a NonlinearUnits instance. + private NonlinearUnits() { + throw new AssertionError(); + } +} diff --git a/src/unitConverter/unit/SI.java b/src/unitConverter/unit/SI.java new file mode 100644 index 0000000..54eb4c5 --- /dev/null +++ b/src/unitConverter/unit/SI.java @@ -0,0 +1,74 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import unitConverter.dimension.StandardDimensions; +import unitConverter.dimension.UnitDimension; + +/** + * The SI, which holds all SI units + * + * @author Adrien Hopkins + * @since 2018-12-23 + * @since v0.1.0 + */ +public enum SI implements UnitSystem { + SI; + + /** + * This system's base units. + * + * @since 2019-01-25 + * @since v0.1.0 + */ + private static final Set<BaseUnit> baseUnits = new HashSet<>(); + + // base units + public static final BaseUnit METRE = SI.getBaseUnit(StandardDimensions.LENGTH); + public static final BaseUnit KILOGRAM = SI.getBaseUnit(StandardDimensions.MASS); + public static final BaseUnit SECOND = SI.getBaseUnit(StandardDimensions.TIME); + public static final BaseUnit AMPERE = SI.getBaseUnit(StandardDimensions.ELECTRIC_CURRENT); + public static final BaseUnit KELVIN = SI.getBaseUnit(StandardDimensions.TEMPERATURE); + public static final BaseUnit MOLE = SI.getBaseUnit(StandardDimensions.QUANTITY); + public static final BaseUnit CANDELA = SI.getBaseUnit(StandardDimensions.LUMINOUS_INTENSITY); + + @Override + public BaseUnit getBaseUnit(final UnitDimension dimension) { + // try to find an existing unit before creating a new one + + Objects.requireNonNull(dimension, "dimension must not be null."); + for (final BaseUnit unit : baseUnits) { + // it will be equal since the conditions for equality are dimension and system, + // and system is always SI. + if (unit.getDimension().equals(dimension)) + return unit; + } + // could not find an existing base unit + final BaseUnit unit = new BaseUnit(dimension, this); + baseUnits.add(unit); + return unit; + } + + @Override + public String getName() { + return "SI"; + } +} diff --git a/src/unitConverter/unit/SIPrefix.java b/src/unitConverter/unit/SIPrefix.java new file mode 100755 index 0000000..54625fb --- /dev/null +++ b/src/unitConverter/unit/SIPrefix.java @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +/** + * The SI prefixes. + * + * @author Adrien Hopkins + * @since 2019-01-14 + * @since v0.1.0 + */ +public enum SIPrefix implements UnitPrefix { + DECA(10), HECTO(100), KILO(1e3), MEGA(1e6), GIGA(1e9), TERA(1e12), PETA(1e15), EXA(1e18), ZETTA(1e21), YOTTA( + 1e24), DECI(0.1), CENTI(0.01), MILLI( + 1e-3), MICRO(1e-6), NANO(1e-9), PICO(1e-12), FEMTO(1e-15), ATTO(1e-18), ZEPTO(1e-21), YOCTO(1e-24); + + private final double multiplier; + + /** + * Creates the {@code SIPrefix}. + * + * @param multiplier + * prefix's multiplier + * @since 2019-01-14 + * @since v0.1.0 + */ + private SIPrefix(final double multiplier) { + this.multiplier = multiplier; + } + + /** + * @return value + * @since 2019-01-14 + * @since v0.1.0 + */ + @Override + public final double getMultiplier() { + return this.multiplier; + } +} diff --git a/src/unitConverter/unit/Unit.java b/src/unitConverter/unit/Unit.java index 3e7f9da..54f1423 100755 --- a/src/unitConverter/unit/Unit.java +++ b/src/unitConverter/unit/Unit.java @@ -25,6 +25,7 @@ import unitConverter.dimension.UnitDimension; * * @author Adrien Hopkins * @since 2018-12-22 + * @since v0.1.0 */ public interface Unit { /** @@ -34,6 +35,7 @@ public interface Unit { * unit to test with * @return true if the units are compatible * @since 2019-01-13 + * @since v0.1.0 */ default boolean canConvertTo(final Unit other) { return Objects.equals(this.getBase(), other.getBase()); @@ -53,6 +55,7 @@ public interface Unit { * value expressed in <b>base</b> unit * @return value expressed in <b>this</b> unit * @since 2018-12-22 + * @since v0.1.0 */ double convertFromBase(double value); @@ -70,6 +73,7 @@ public interface Unit { * value expressed in <b>this</b> unit * @return value expressed in <b>base</b> unit * @since 2018-12-22 + * @since v0.1.0 */ double convertToBase(double value); @@ -86,18 +90,21 @@ public interface Unit { * * @return base unit associated with this unit * @since 2018-12-22 + * @since v0.1.0 */ - Unit getBase(); + BaseUnit getBase(); /** * @return dimension measured by this unit * @since 2018-12-22 + * @since v0.1.0 */ UnitDimension getDimension(); /** * @return system that this unit is a part of * @since 2018-12-23 + * @since v0.1.0 */ UnitSystem getSystem(); } diff --git a/src/unitConverter/unit/UnitPrefix.java b/src/unitConverter/unit/UnitPrefix.java new file mode 100755 index 0000000..a0c1b7c --- /dev/null +++ b/src/unitConverter/unit/UnitPrefix.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +/** + * A prefix that can be attached onto the front of any unit, which multiplies it by a certain value + * + * @author Adrien Hopkins + * @since 2019-01-14 + * @since v0.1.0 + */ +public interface UnitPrefix { + /** + * @return this prefix's multiplier + * @since 2019-01-14 + * @since v0.1.0 + */ + double getMultiplier(); +} diff --git a/src/unitConverter/unit/UnitSystem.java b/src/unitConverter/unit/UnitSystem.java index 2e3a5d8..ce8c249 100755 --- a/src/unitConverter/unit/UnitSystem.java +++ b/src/unitConverter/unit/UnitSystem.java @@ -16,16 +16,38 @@ */ package unitConverter.unit; +import java.util.Objects; + +import unitConverter.dimension.UnitDimension; + /** * A system of units. Each unit should be aware of its system. * * @author Adrien Hopkins * @since 2018-12-23 + * @since v0.1.0 */ public interface UnitSystem { /** - * @return name of system + * Gets a base unit for this system and the provided dimension. + * + * @param dimension + * dimension used by base unit + * @return base unit + * @throws NullPointerException + * if dimension is null * @since 2019-01-25 + * @since v0.1.0 + */ + default BaseUnit getBaseUnit(final UnitDimension dimension) { + Objects.requireNonNull(dimension, "dimension must not be null."); + return new BaseUnit(dimension, this); + } + + /** + * @return name of system + * @since 2018-12-23 + * @since v0.1.0 */ String getName(); } diff --git a/src/unitConverter/unit/UnitTest.java b/src/unitConverter/unit/UnitTest.java new file mode 100755 index 0000000..c3237eb --- /dev/null +++ b/src/unitConverter/unit/UnitTest.java @@ -0,0 +1,47 @@ +/** + * Copyright (C) 2018 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package unitConverter.unit; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import unitConverter.dimension.StandardDimensions; + +/** + * Testing the various Unit classes + * + * @author Adrien Hopkins + * @since 2018-12-22 + */ +class UnitTest { + @Test + void testConversion() { + final BaseUnit metre = SI.METRE; + final Unit inch = metre.times(0.0254); + + assertEquals(1.9, inch.convertToBase(75), 0.01); + } + + @Test + void testEquals() { + final BaseUnit metre = SI.METRE; + final Unit meter = SI.SI.getBaseUnit(StandardDimensions.LENGTH); + + assertEquals(metre, meter); + } +} |