From 45286c30f96f152f821098d878ede64ecbabe48a Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 16 Oct 2019 13:53:29 -0400 Subject: Added the LinearUnit to the new units definition. --- src/org/unitConverter/math/ObjectProduct.java | 28 ++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/org/unitConverter/math') 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 { } /** - * @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 { @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 objectToString) { final List positiveStringComponents = new ArrayList<>(); final List 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)); } } -- cgit v1.2.3