summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/newUnits/Unit.java
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-16 15:35:30 -0400
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-16 15:35:30 -0400
commitabe715a30844537693ae186308adcab62c66f121 (patch)
tree1b285a083f91c9db125a6dcc22b6da7a0e9b0d66 /src/org/unitConverter/newUnits/Unit.java
parent9eba2e72ecba1f33c73d358eb509f0a0816aa810 (diff)
Made Unit an abstract class.
The abstract unit conversion methods are now protected.
Diffstat (limited to 'src/org/unitConverter/newUnits/Unit.java')
-rw-r--r--src/org/unitConverter/newUnits/Unit.java88
1 files changed, 72 insertions, 16 deletions
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();
+ }
+}