summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/utils/ObjectProduct.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/utils/ObjectProduct.java')
-rw-r--r--src/main/java/sevenUnits/utils/ObjectProduct.java48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/main/java/sevenUnits/utils/ObjectProduct.java b/src/main/java/sevenUnits/utils/ObjectProduct.java
index 5b1b739..66bb773 100644
--- a/src/main/java/sevenUnits/utils/ObjectProduct.java
+++ b/src/main/java/sevenUnits/utils/ObjectProduct.java
@@ -33,7 +33,7 @@ import java.util.function.Function;
* @author Adrien Hopkins
* @since 2019-10-16
*/
-public final class ObjectProduct<T> {
+public class ObjectProduct<T> implements Nameable {
/**
* Returns an empty ObjectProduct of a certain type
*
@@ -83,15 +83,32 @@ public final class ObjectProduct<T> {
final Map<T, Integer> exponents;
/**
- * Creates the {@code ObjectProduct}.
+ * The object's name and symbol
+ */
+ private final NameSymbol nameSymbol;
+
+ /**
+ * Creates a {@code ObjectProduct} without a name/symbol.
*
* @param exponents objects that make up this product
* @since 2019-10-16
*/
- private ObjectProduct(final Map<T, Integer> exponents) {
+ ObjectProduct(final Map<T, Integer> exponents) {
+ this(exponents, NameSymbol.EMPTY);
+ }
+
+ /**
+ * Creates the {@code ObjectProduct}.
+ *
+ * @param exponents objects that make up this product
+ * @param nameSymbol name and symbol of object product
+ * @since 2019-10-16
+ */
+ ObjectProduct(final Map<T, Integer> exponents, NameSymbol nameSymbol) {
this.exponents = Collections.unmodifiableMap(
ConditionalExistenceCollections.conditionalExistenceMap(exponents,
e -> !Integer.valueOf(0).equals(e.getValue())));
+ this.nameSymbol = nameSymbol;
}
/**
@@ -171,6 +188,11 @@ public final class ObjectProduct<T> {
}
@Override
+ public NameSymbol getNameSymbol() {
+ return this.nameSymbol;
+ }
+
+ @Override
public int hashCode() {
return Objects.hash(this.exponents);
}
@@ -235,16 +257,19 @@ public final class ObjectProduct<T> {
/**
* Converts this product to a string using the objects'
- * {@link Object#toString()} method. If objects have a long toString
- * representation, it is recommended to use {@link #toString(Function)}
- * instead to shorten the returned string.
+ * {@link Object#toString()} method (or {@link Nameable#getShortName} if
+ * available). If objects have a long toString representation, it is
+ * recommended to use {@link #toString(Function)} instead to shorten the
+ * returned string.
*
* <p>
* {@inheritDoc}
*/
@Override
public String toString() {
- return this.toString(Object::toString);
+ return this
+ .toString(o -> o instanceof Nameable ? ((Nameable) o).getShortName()
+ : o.toString());
}
/**
@@ -280,4 +305,13 @@ public final class ObjectProduct<T> {
return positiveString + negativeString;
}
+
+ /**
+ * @return named version of this {@code ObjectProduct}, using data from
+ * {@code nameSymbol}
+ * @since 2021-12-15
+ */
+ public ObjectProduct<T> withName(NameSymbol nameSymbol) {
+ return new ObjectProduct<>(this.exponents, nameSymbol);
+ }
}