diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/org/unitConverter/newUnits/AbstractUnit.java | 81 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/BaseUnit.java | 14 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/FunctionalUnit.java | 2 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/LinearUnit.java | 8 | ||||
-rw-r--r-- | src/org/unitConverter/newUnits/Unit.java | 88 |
5 files changed, 79 insertions, 114 deletions
diff --git a/src/org/unitConverter/newUnits/AbstractUnit.java b/src/org/unitConverter/newUnits/AbstractUnit.java deleted file mode 100644 index bc4608e..0000000 --- a/src/org/unitConverter/newUnits/AbstractUnit.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * 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 org.unitConverter.newUnits; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -import org.unitConverter.dimension.BaseDimension; -import org.unitConverter.math.ObjectProduct; - -/** - * @author Adrien Hopkins - * @since 2019-10-16 - */ -public abstract class AbstractUnit implements Unit { - /** - * The combination of units that this unit is based on. - */ - private final ObjectProduct<BaseUnit> unitBase; - - /** - * Cache storing the result of getDimension() - */ - private transient ObjectProduct<BaseDimension> dimension = null; - - /** - * Creates the {@code AbstractUnit}. - * - * @param unitBase - * @since 2019-10-16 - * @throws NullPointerException - * if unitBase is null - */ - public AbstractUnit(final ObjectProduct<BaseUnit> unitBase) { - this.unitBase = Objects.requireNonNull(unitBase, "unitBase must not be null."); - } - - /** - * @return unitBase - * @since 2019-10-16 - */ - @Override - public final ObjectProduct<BaseUnit> getBase() { - return this.unitBase; - } - - @Override - public final ObjectProduct<BaseDimension> getDimension() { - if (this.dimension == null) { - final Map<BaseUnit, Integer> mapping = this.unitBase.exponentMap(); - final Map<BaseDimension, Integer> dimensionMap = new HashMap<>(); - - for (final BaseUnit key : mapping.keySet()) { - dimensionMap.put(key.getBaseDimension(), mapping.get(key)); - } - - this.dimension = ObjectProduct.fromExponentMapping(dimensionMap); - } - return this.dimension; - } - - @Override - public String toString() { - return "Unit derived from base " + this.getBase().toString(); - } -} diff --git a/src/org/unitConverter/newUnits/BaseUnit.java b/src/org/unitConverter/newUnits/BaseUnit.java index b7577ff..2c4d748 100644 --- a/src/org/unitConverter/newUnits/BaseUnit.java +++ b/src/org/unitConverter/newUnits/BaseUnit.java @@ -19,7 +19,6 @@ package org.unitConverter.newUnits; import java.util.Objects; import org.unitConverter.dimension.BaseDimension; -import org.unitConverter.math.ObjectProduct; /** * A unit that other units are defined by. @@ -27,7 +26,7 @@ import org.unitConverter.math.ObjectProduct; * @author Adrien Hopkins * @since 2019-10-16 */ -public final class BaseUnit implements Unit { +public final class BaseUnit extends Unit { private final BaseDimension dimension; private final String name; private final String symbol; @@ -46,6 +45,7 @@ public final class BaseUnit implements Unit { * @since 2019-10-16 */ private BaseUnit(final BaseDimension dimension, final String name, final String symbol) { + super(); this.dimension = Objects.requireNonNull(dimension, "dimension must not be null."); this.name = Objects.requireNonNull(name, "name must not be null."); this.symbol = Objects.requireNonNull(symbol, "symbol must not be null."); @@ -72,11 +72,6 @@ public final class BaseUnit implements Unit { return value; } - @Override - public ObjectProduct<BaseUnit> getBase() { - return ObjectProduct.oneOf(this); - } - /** * @return dimension * @since 2019-10-16 @@ -85,11 +80,6 @@ public final class BaseUnit implements Unit { return this.dimension; } - @Override - public ObjectProduct<BaseDimension> getDimension() { - return ObjectProduct.oneOf(this.getBaseDimension()); - } - /** * @return name * @since 2019-10-16 diff --git a/src/org/unitConverter/newUnits/FunctionalUnit.java b/src/org/unitConverter/newUnits/FunctionalUnit.java index 99ff833..6bff3e8 100644 --- a/src/org/unitConverter/newUnits/FunctionalUnit.java +++ b/src/org/unitConverter/newUnits/FunctionalUnit.java @@ -27,7 +27,7 @@ import org.unitConverter.math.ObjectProduct; * @author Adrien Hopkins * @since 2019-05-22 */ -final class FunctionalUnit extends AbstractUnit { +final class FunctionalUnit extends Unit { /** * Returns a unit from its base and the functions it uses to convert to and from its base. * diff --git a/src/org/unitConverter/newUnits/LinearUnit.java b/src/org/unitConverter/newUnits/LinearUnit.java index 7600dad..c8c610e 100644 --- a/src/org/unitConverter/newUnits/LinearUnit.java +++ b/src/org/unitConverter/newUnits/LinearUnit.java @@ -27,7 +27,7 @@ import org.unitConverter.math.ObjectProduct; * @author Adrien Hopkins * @since 2019-10-16 */ -public final class LinearUnit extends AbstractUnit { +public final class LinearUnit extends Unit { /** * Gets a {@code LinearUnit} from a unit and a value. For example, converts '59 °F' to a linear unit with the value * of '288.15 K' @@ -39,7 +39,7 @@ public final class LinearUnit extends AbstractUnit { * @return value expressed as a {@code LinearUnit} * @since 2019-10-16 */ - public static LinearUnit fromValue(final Unit unit, final double value) { + public static LinearUnit fromUnitValue(final Unit unit, final double value) { return new LinearUnit(unit.getBase(), unit.convertToBase(value)); } @@ -84,12 +84,12 @@ public final class LinearUnit extends AbstractUnit { } @Override - public double convertFromBase(final double value) { + protected double convertFromBase(final double value) { return value / this.getConversionFactor(); } @Override - public double convertToBase(final double value) { + protected double convertToBase(final double value) { return value * this.getConversionFactor(); } diff --git a/src/org/unitConverter/newUnits/Unit.java b/src/org/unitConverter/newUnits/Unit.java index b50115d..feeb25e 100644 --- a/src/org/unitConverter/newUnits/Unit.java +++ b/src/org/unitConverter/newUnits/Unit.java @@ -19,16 +19,15 @@ package org.unitConverter.newUnits; import java.util.Objects; import java.util.function.DoubleUnaryOperator; -import org.unitConverter.dimension.BaseDimension; import org.unitConverter.math.ObjectProduct; /** - * A unit described in terms of base units. + * A unit that is composed of base units. * * @author Adrien Hopkins * @since 2019-10-16 */ -public interface Unit { +public abstract class Unit { /** * Returns a unit from its base and the functions it uses to convert to and from its base. * @@ -57,6 +56,44 @@ public interface Unit { } /** + * The combination of units that this unit is based on. + * + * @since 2019-10-16 + */ + private final ObjectProduct<BaseUnit> unitBase; + + // /** + // * Cache storing the result of getDimension() + // * + // * @since 2019-10-16 + // */ + // private transient ObjectProduct<BaseDimension> dimension = null; + + /** + * A constructor that constructs {@code BaseUnit} instances. + * + * @since 2019-10-16 + */ + Unit() { + if (this instanceof BaseUnit) { + this.unitBase = ObjectProduct.oneOf((BaseUnit) this); + } else + throw new AssertionError(); + } + + /** + * Creates the {@code AbstractUnit}. + * + * @param unitBase + * @since 2019-10-16 + * @throws NullPointerException + * if unitBase is null + */ + protected Unit(final ObjectProduct<BaseUnit> unitBase) { + this.unitBase = Objects.requireNonNull(unitBase, "unitBase must not be null."); + } + + /** * Checks if a value expressed in this unit can be converted to a value expressed in {@code other} * * @param other @@ -67,7 +104,7 @@ public interface Unit { * @throws NullPointerException * if other is null */ - default boolean canConvertTo(final Unit other) { + public final boolean canConvertTo(final Unit other) { Objects.requireNonNull(other, "other must not be null."); return Objects.equals(this.getBase(), other.getBase()); } @@ -88,7 +125,7 @@ public interface Unit { * @since 2018-12-22 * @since v0.1.0 */ - double convertFromBase(double value); + protected abstract double convertFromBase(double value); /** * Converts a value expressed in this unit to a value expressed in {@code other}. @@ -101,11 +138,11 @@ public interface Unit { * @since 2019-05-22 * @throws IllegalArgumentException * if {@code other} is incompatible for conversion with this unit (as tested by - * {@link Unit#canConvertTo}). + * {@link IUnit#canConvertTo}). * @throws NullPointerException * if other is null */ - default double convertTo(final Unit other, final double value) { + public final double convertTo(final Unit other, final double value) { Objects.requireNonNull(other, "other must not be null."); if (this.canConvertTo(other)) return other.convertFromBase(this.convertToBase(value)); @@ -129,19 +166,38 @@ public interface Unit { * @since 2018-12-22 * @since v0.1.0 */ - double convertToBase(double value); + protected abstract double convertToBase(double value); /** * @return combination of units that this unit is based on * @since 2018-12-22 * @since v0.1.0 */ - ObjectProduct<BaseUnit> getBase(); + public final ObjectProduct<BaseUnit> getBase() { + return this.unitBase; + } - /** - * @return dimension measured by this unit - * @since 2018-12-22 - * @since v0.1.0 - */ - ObjectProduct<BaseDimension> getDimension(); -}
\ No newline at end of file + // /** + // * @return dimension measured by this unit + // * @since 2018-12-22 + // * @since v0.1.0 + // */ + // private final ObjectProduct<BaseDimension> getDimension() { + // if (this.dimension == null) { + // final Map<BaseUnit, Integer> mapping = this.unitBase.exponentMap(); + // final Map<BaseDimension, Integer> dimensionMap = new HashMap<>(); + // + // for (final BaseUnit key : mapping.keySet()) { + // dimensionMap.put(key.getBaseDimension(), mapping.get(key)); + // } + // + // this.dimension = ObjectProduct.fromExponentMapping(dimensionMap); + // } + // return this.dimension; + // } + + @Override + public String toString() { + return "Unit derived from base " + this.getBase().toString(); + } +} |