From 54ab9c05234b09547e2a01b1eab812420c6a3dda Mon Sep 17 00:00:00 2001
From: Adrien Hopkins
Date: Thu, 17 Oct 2019 14:25:17 -0400
Subject: Implemented the new Units system
Fahrenheit has temporarily been removed; it will be back.
---
src/org/unitConverter/unit/Unit.java | 105 ++++++++++++++++++++++++-----------
1 file changed, 73 insertions(+), 32 deletions(-)
(limited to 'src/org/unitConverter/unit/Unit.java')
diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java
index 54f0ab5..d4eb86e 100644
--- a/src/org/unitConverter/unit/Unit.java
+++ b/src/org/unitConverter/unit/Unit.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,19 +16,20 @@
*/
package org.unitConverter.unit;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
-import org.unitConverter.dimension.UnitDimension;
+import org.unitConverter.math.ObjectProduct;
/**
- * A unit that has an associated base unit, and can convert a value expressed in it to and from that base.
+ * A unit that is composed of base units.
*
* @author Adrien Hopkins
- * @since 2018-12-22
- * @since v0.1.0
+ * @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.
*
@@ -51,11 +52,49 @@ public interface Unit {
* @throws NullPointerException
* if any argument is null
*/
- public static Unit fromConversionFunctions(final BaseUnit base, final DoubleUnaryOperator converterFrom,
- final DoubleUnaryOperator converterTo) {
+ public static Unit fromConversionFunctions(final ObjectProduct base,
+ final DoubleUnaryOperator converterFrom, final DoubleUnaryOperator converterTo) {
return FunctionalUnit.valueOf(base, converterFrom, converterTo);
}
+ /**
+ * The combination of units that this unit is based on.
+ *
+ * @since 2019-10-16
+ */
+ private final ObjectProduct unitBase;
+
+ /**
+ * Cache storing the result of getDimension()
+ *
+ * @since 2019-10-16
+ */
+ private transient ObjectProduct 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 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}
*
@@ -67,7 +106,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 +127,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 +140,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,36 +168,38 @@ public interface Unit {
* @since 2018-12-22
* @since v0.1.0
*/
- double convertToBase(double value);
+ protected abstract double convertToBase(double value);
/**
- *
- * Returns the base unit associated with this unit.
- *
- *
- * The dimension of this unit must be equal to the dimension of the returned unit.
- *
- *
- * If this unit is a base unit, this method should return this unit.\
- *
- *
- * @return base unit associated with this unit
+ * @return combination of units that this unit is based on
* @since 2018-12-22
* @since v0.1.0
*/
- BaseUnit getBase();
+ public final ObjectProduct getBase() {
+ return this.unitBase;
+ }
/**
* @return dimension measured by this unit
* @since 2018-12-22
* @since v0.1.0
*/
- UnitDimension getDimension();
+ public final ObjectProduct getDimension() {
+ if (this.dimension == null) {
+ final Map mapping = this.unitBase.exponentMap();
+ final Map dimensionMap = new HashMap<>();
- /**
- * @return system that this unit is a part of
- * @since 2018-12-23
- * @since v0.1.0
- */
- UnitSystem getSystem();
+ 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();
+ }
}
--
cgit v1.2.3
From 1bf43ad95e70019a69e91e09ff74f677082ed6f5 Mon Sep 17 00:00:00 2001
From: Adrien Hopkins
Date: Mon, 21 Oct 2019 15:17:50 -0400
Subject: Made improvements and corrections to the documentation.
---
src/org/unitConverter/converterGUI/UnitConverterGUI.java | 2 +-
src/org/unitConverter/converterGUI/package-info.java | 2 +-
src/org/unitConverter/math/package-info.java | 3 ++-
src/org/unitConverter/unit/LinearUnit.java | 8 ++++----
src/org/unitConverter/unit/Unit.java | 14 ++++++++++++--
src/org/unitConverter/unit/package-info.java | 3 ++-
6 files changed, 22 insertions(+), 10 deletions(-)
(limited to 'src/org/unitConverter/unit/Unit.java')
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
index 4598971..511e47b 100644
--- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java
+++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
@@ -47,8 +47,8 @@ import org.unitConverter.unit.BaseDimension;
import org.unitConverter.unit.LinearUnit;
import org.unitConverter.unit.SI;
import org.unitConverter.unit.Unit;
-import org.unitConverter.unit.UnitPrefix;
import org.unitConverter.unit.UnitDatabase;
+import org.unitConverter.unit.UnitPrefix;
/**
* @author Adrien Hopkins
diff --git a/src/org/unitConverter/converterGUI/package-info.java b/src/org/unitConverter/converterGUI/package-info.java
index 1555291..d85ecab 100644
--- a/src/org/unitConverter/converterGUI/package-info.java
+++ b/src/org/unitConverter/converterGUI/package-info.java
@@ -15,7 +15,7 @@
* along with this program. If not, see .
*/
/**
- * All classes that work to convert units.
+ * The GUI interface of the Unit Converter.
*
* @author Adrien Hopkins
* @since 2019-01-25
diff --git a/src/org/unitConverter/math/package-info.java b/src/org/unitConverter/math/package-info.java
index 65d6b23..65727e4 100644
--- a/src/org/unitConverter/math/package-info.java
+++ b/src/org/unitConverter/math/package-info.java
@@ -15,9 +15,10 @@
* along with this program. If not, see .
*/
/**
- * A module that is capable of parsing expressions of things, like mathematical expressions or unit expressions.
+ * Supplementary classes that are not related to units, but are necessary for their function.
*
* @author Adrien Hopkins
* @since 2019-03-14
+ * @since v0.2.0
*/
package org.unitConverter.math;
\ No newline at end of file
diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java
index c397250..1918d6b 100644
--- a/src/org/unitConverter/unit/LinearUnit.java
+++ b/src/org/unitConverter/unit/LinearUnit.java
@@ -167,8 +167,8 @@ public final class LinearUnit extends Unit {
/**
* Returns the difference of this unit and another.
*
- * Two units can be subtracted if they have the same base. If {@code subtrahend} does not meet this condition, an
- * {@code IllegalArgumentException} will be thrown.
+ * Two units can be subtracted if they have the same base. Note that {@link #canConvertTo} can be used to determine
+ * this. If {@code subtrahend} does not meet this condition, an {@code IllegalArgumentException} will be thrown.
*
*
* @param subtrahend
@@ -196,8 +196,8 @@ public final class LinearUnit extends Unit {
/**
* Returns the sum of this unit and another.
*
- * Two units can be added if they have the same base. If {@code addend} does not meet this condition, an
- * {@code IllegalArgumentException} will be thrown.
+ * Two units can be added if they have the same base. Note that {@link #canConvertTo} can be used to determine this.
+ * If {@code addend} does not meet this condition, an {@code IllegalArgumentException} will be thrown.
*
*
* @param addend
diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java
index d4eb86e..7971a41 100644
--- a/src/org/unitConverter/unit/Unit.java
+++ b/src/org/unitConverter/unit/Unit.java
@@ -52,7 +52,7 @@ public abstract class Unit {
* @throws NullPointerException
* if any argument is null
*/
- public static Unit fromConversionFunctions(final ObjectProduct base,
+ public static final Unit fromConversionFunctions(final ObjectProduct base,
final DoubleUnaryOperator converterFrom, final DoubleUnaryOperator converterTo) {
return FunctionalUnit.valueOf(base, converterFrom, converterTo);
}
@@ -121,6 +121,9 @@ public abstract class Unit {
* If this unit is a base unit, this method should return {@code value}.
*
*
+ * @implSpec This method is used by {@link #convertTo}, and its behaviour affects the behaviour of
+ * {@code convertTo}.
+ *
* @param value
* value expressed in base unit
* @return value expressed in this unit
@@ -132,6 +135,10 @@ public abstract class Unit {
/**
* Converts a value expressed in this unit to a value expressed in {@code other}.
*
+ * @implSpec If unit conversion is possible, this implementation returns
+ * {@code other.convertFromBase(this.convertToBase(value))}. Therefore, overriding either of those methods
+ * will change the output of this method.
+ *
* @param other
* unit to convert to
* @param value
@@ -140,7 +147,7 @@ public abstract class Unit {
* @since 2019-05-22
* @throws IllegalArgumentException
* if {@code other} is incompatible for conversion with this unit (as tested by
- * {@link IUnit#canConvertTo}).
+ * {@link Unit#canConvertTo}).
* @throws NullPointerException
* if other is null
*/
@@ -162,6 +169,9 @@ public abstract class Unit {
* If this unit is a base unit, this method should return {@code value}.
*
*
+ * @implSpec This method is used by {@link #convertTo}, and its behaviour affects the behaviour of
+ * {@code convertTo}.
+ *
* @param value
* value expressed in this unit
* @return value expressed in base unit
diff --git a/src/org/unitConverter/unit/package-info.java b/src/org/unitConverter/unit/package-info.java
index 2d83e1f..2f0e097 100644
--- a/src/org/unitConverter/unit/package-info.java
+++ b/src/org/unitConverter/unit/package-info.java
@@ -15,9 +15,10 @@
* along with this program. If not, see .
*/
/**
- * The new definition for units.
+ * Everything to do with the units that make up Unit Converter.
*
* @author Adrien Hopkins
* @since 2019-10-16
+ * @since v0.1.0
*/
package org.unitConverter.unit;
\ No newline at end of file
--
cgit v1.2.3