summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/unitConverter/math/ObjectProduct.java28
-rw-r--r--src/org/unitConverter/newUnits/AbstractUnit.java7
-rw-r--r--src/org/unitConverter/newUnits/BaseUnit.java11
-rw-r--r--src/org/unitConverter/newUnits/LinearUnit.java261
-rw-r--r--src/org/unitConverter/newUnits/Unit.java2
5 files changed, 301 insertions, 8 deletions
diff --git a/src/org/unitConverter/math/ObjectProduct.java b/src/org/unitConverter/math/ObjectProduct.java
index ec4d2d6..21ab207 100644
--- a/src/org/unitConverter/math/ObjectProduct.java
+++ b/src/org/unitConverter/math/ObjectProduct.java
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
+import java.util.function.Function;
/**
* An immutable product of multiple objects of a type, such as base units. The objects can be multiplied and
@@ -177,10 +178,10 @@ public final class ObjectProduct<T> {
}
/**
- * @return true if this product is a "base", i.e. it has one exponent of one and no other nonzero exponents
+ * @return true if this product is a single object, i.e. it has one exponent of one and no other nonzero exponents
* @since 2019-10-16
*/
- public boolean isBase() {
+ public boolean isSingleObject() {
int oneCount = 0;
boolean twoOrMore = false; // has exponents of 2 or more
for (final T b : this.getBaseSet()) {
@@ -238,16 +239,29 @@ public final class ObjectProduct<T> {
@Override
public String toString() {
+ return this.toString(Object::toString);
+ }
+
+ /**
+ * Converts this product to a string. The objects that make up this product are represented by
+ * {@code objectToString}
+ *
+ * @param objectToString
+ * function to convert objects to strings
+ * @return string representation of product
+ * @since 2019-10-16
+ */
+ public String toString(final Function<T, String> objectToString) {
final List<String> positiveStringComponents = new ArrayList<>();
final List<String> negativeStringComponents = new ArrayList<>();
- // for each base dimension that makes up this dimension, add it and its exponent
- for (final T dimension : this.getBaseSet()) {
- final int exponent = this.exponents.get(dimension);
+ // for each base object that makes up this object, add it and its exponent
+ for (final T object : this.getBaseSet()) {
+ final int exponent = this.exponents.get(object);
if (exponent > 0) {
- positiveStringComponents.add(String.format("%s^%d", dimension, exponent));
+ positiveStringComponents.add(String.format("%s^%d", objectToString.apply(object), exponent));
} else if (exponent < 0) {
- negativeStringComponents.add(String.format("%s^%d", dimension, -exponent));
+ negativeStringComponents.add(String.format("%s^%d", objectToString.apply(object), -exponent));
}
}
diff --git a/src/org/unitConverter/newUnits/AbstractUnit.java b/src/org/unitConverter/newUnits/AbstractUnit.java
index 909ea8b..bc4608e 100644
--- a/src/org/unitConverter/newUnits/AbstractUnit.java
+++ b/src/org/unitConverter/newUnits/AbstractUnit.java
@@ -60,7 +60,7 @@ public abstract class AbstractUnit implements Unit {
}
@Override
- public ObjectProduct<BaseDimension> getDimension() {
+ public final ObjectProduct<BaseDimension> getDimension() {
if (this.dimension == null) {
final Map<BaseUnit, Integer> mapping = this.unitBase.exponentMap();
final Map<BaseDimension, Integer> dimensionMap = new HashMap<>();
@@ -73,4 +73,9 @@ public abstract class AbstractUnit implements Unit {
}
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 69e8b8b..b7577ff 100644
--- a/src/org/unitConverter/newUnits/BaseUnit.java
+++ b/src/org/unitConverter/newUnits/BaseUnit.java
@@ -51,6 +51,17 @@ public final class BaseUnit implements Unit {
this.symbol = Objects.requireNonNull(symbol, "symbol must not be null.");
}
+ /**
+ * Returns a {@code LinearUnit} with this unit as a base and a conversion factor of 1. This operation must be done
+ * in order to allow units to be created with operations.
+ *
+ * @return this unit as a {@code LinearUnit}
+ * @since 2019-10-16
+ */
+ public LinearUnit asLinearUnit() {
+ return LinearUnit.valueOf(this.getBase(), 1);
+ }
+
@Override
public double convertFromBase(final double value) {
return value;
diff --git a/src/org/unitConverter/newUnits/LinearUnit.java b/src/org/unitConverter/newUnits/LinearUnit.java
new file mode 100644
index 0000000..5a589b7
--- /dev/null
+++ b/src/org/unitConverter/newUnits/LinearUnit.java
@@ -0,0 +1,261 @@
+/**
+ * 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.Objects;
+
+import org.unitConverter.math.DecimalComparison;
+import org.unitConverter.math.ObjectProduct;
+
+/**
+ * A unit that can be expressed as a product of its base and a number. For example, kilometres, inches and pounds.
+ *
+ * @author Adrien Hopkins
+ * @since 2019-10-16
+ */
+public final class LinearUnit extends AbstractUnit {
+ /**
+ * Gets a {@code LinearUnit} from a unit base and a conversion factor. In other words, gets the product of
+ * {@code unitBase} and {@code conversionFactor}, expressed as a {@code LinearUnit}.
+ *
+ * @param unitBase
+ * unit base to multiply by
+ * @param conversionFactor
+ * number to multiply base by
+ * @return product of base and conversion factor
+ * @since 2019-10-16
+ */
+ public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor) {
+ return new LinearUnit(unitBase, conversionFactor);
+ }
+
+ /**
+ * The value of this unit as represented in its base form. Mathematically,
+ *
+ * <pre>
+ * this = conversionFactor * getBase()
+ * </pre>
+ *
+ * @since 2019-10-16
+ */
+ private final double conversionFactor;
+
+ /**
+ * Creates the {@code LinearUnit}.
+ *
+ * @param unitBase
+ * base of linear unit
+ * @param conversionFactor
+ * conversion factor between base and unit
+ * @since 2019-10-16
+ */
+ private LinearUnit(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor) {
+ super(unitBase);
+ 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 valueOf(this.getBase(), this.getConversionFactor() / divisor);
+ }
+
+ /**
+ * Returns the quotient of this unit and another.
+ *
+ * @param divisor
+ * unit to divide by
+ * @return quotient of two units
+ * @throws NullPointerException
+ * if {@code divisor} is null
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+ public LinearUnit dividedBy(final LinearUnit divisor) {
+ Objects.requireNonNull(divisor, "other must not be null");
+
+ // divide the units
+ final ObjectProduct<BaseUnit> base = this.getBase().dividedBy(divisor.getBase());
+ return valueOf(base, this.getConversionFactor() / divisor.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())
+ && DecimalComparison.equals(this.getConversionFactor(), other.getConversionFactor());
+ }
+
+ /**
+ * @return conversion factor
+ * @since 2019-10-16
+ */
+ public double getConversionFactor() {
+ return this.conversionFactor;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(this.getBase(), this.getConversionFactor());
+ }
+
+ /**
+ * @return whether this unit is equivalent to a {@code BaseUnit} (i.e. there is a {@code BaseUnit b} where
+ * {@code b.asLinearUnit().equals(this)} returns {@code true}.)
+ * @since 2019-10-16
+ */
+ public boolean isBase() {
+ return this.isCoherent() && this.getBase().isSingleObject();
+ }
+
+ /**
+ * @return whether this unit is coherent (i.e. has conversion factor 1)
+ * @since 2019-10-16
+ */
+ public boolean isCoherent() {
+ return this.getConversionFactor() == 1;
+ }
+
+ /**
+ * Returns the difference of this unit and another.
+ * <p>
+ * 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.
+ * </p>
+ *
+ * @param subtrahend
+ * unit to subtract
+ * @return difference of units
+ * @throws IllegalArgumentException
+ * if {@code subtrahend} is not compatible for subtraction as described above
+ * @throws NullPointerException
+ * if {@code subtrahend} is null
+ * @since 2019-03-17
+ * @since v0.2.0
+ */
+ public LinearUnit minus(final LinearUnit subtrahendend) {
+ Objects.requireNonNull(subtrahendend, "addend must not be null.");
+
+ // reject subtrahends that cannot be added to this unit
+ if (!this.getBase().equals(subtrahendend.getBase()))
+ throw new IllegalArgumentException(
+ String.format("Incompatible units for subtraction \"%s\" and \"%s\".", this, subtrahendend));
+
+ // add the units
+ return valueOf(this.getBase(), this.getConversionFactor() - subtrahendend.getConversionFactor());
+ }
+
+ /**
+ * Returns the sum of this unit and another.
+ * <p>
+ * 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.
+ * </p>
+ *
+ * @param addend
+ * unit to add
+ * @return sum of units
+ * @throws IllegalArgumentException
+ * if {@code addend} is not compatible for addition as described above
+ * @throws NullPointerException
+ * if {@code addend} is null
+ * @since 2019-03-17
+ * @since v0.2.0
+ */
+ public LinearUnit plus(final LinearUnit addend) {
+ Objects.requireNonNull(addend, "addend must not be null.");
+
+ // reject addends that cannot be added to this unit
+ if (!this.getBase().equals(addend.getBase()))
+ throw new IllegalArgumentException(
+ String.format("Incompatible units for addition \"%s\" and \"%s\".", this, addend));
+
+ // add the units
+ return valueOf(this.getBase(), this.getConversionFactor() + addend.getConversionFactor());
+ }
+
+ /**
+ * 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 valueOf(this.getBase(), this.getConversionFactor() * multiplier);
+ }
+
+ /**
+ * Returns the product of this unit and another.
+ *
+ * @param multiplier
+ * unit to multiply by
+ * @return product of two units
+ * @throws NullPointerException
+ * if {@code multiplier} is null
+ * @since 2018-12-22
+ * @since v0.1.0
+ */
+ public LinearUnit times(final LinearUnit multiplier) {
+ Objects.requireNonNull(multiplier, "other must not be null");
+
+ // multiply the units
+ final ObjectProduct<BaseUnit> base = this.getBase().times(multiplier.getBase());
+ return valueOf(base, this.getConversionFactor() * multiplier.getConversionFactor());
+ }
+
+ /**
+ * Returns this unit but to an exponent.
+ *
+ * @param exponent
+ * exponent to exponentiate unit to
+ * @return exponentiated unit
+ * @since 2019-01-15
+ * @since v0.1.0
+ */
+ public LinearUnit toExponent(final int exponent) {
+ return valueOf(this.getBase().toExponent(exponent), Math.pow(this.conversionFactor, exponent));
+ }
+
+ // returns a definition of the unit
+ @Override
+ public String toString() {
+ return Double.toString(this.conversionFactor) + " * " + this.getBase().toString(BaseUnit::getSymbol);
+ }
+
+}
diff --git a/src/org/unitConverter/newUnits/Unit.java b/src/org/unitConverter/newUnits/Unit.java
index 339afde..b50115d 100644
--- a/src/org/unitConverter/newUnits/Unit.java
+++ b/src/org/unitConverter/newUnits/Unit.java
@@ -23,6 +23,8 @@ import org.unitConverter.dimension.BaseDimension;
import org.unitConverter.math.ObjectProduct;
/**
+ * A unit described in terms of base units.
+ *
* @author Adrien Hopkins
* @since 2019-10-16
*/