summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/math/ObjectProduct.java
diff options
context:
space:
mode:
authorAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-16 13:53:29 -0400
committerAdrien Hopkins <masterofnumbers17@gmail.com>2019-10-16 13:53:29 -0400
commit45286c30f96f152f821098d878ede64ecbabe48a (patch)
treea4920babba8be5802967f2be6d215416e95f3c06 /src/org/unitConverter/math/ObjectProduct.java
parent145f524f19b3d61cd12441c2f1a8de05d7dcfe60 (diff)
Added the LinearUnit to the new units definition.
Diffstat (limited to 'src/org/unitConverter/math/ObjectProduct.java')
-rw-r--r--src/org/unitConverter/math/ObjectProduct.java28
1 files changed, 21 insertions, 7 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));
}
}