From da740edd3972fa049c4c8d0e43448c10a6a65dce Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 15 Jun 2025 19:26:12 -0500 Subject: Format & clean up source code --- src/main/java/sevenUnits/utils/ObjectProduct.java | 102 +++++++++++----------- 1 file changed, 50 insertions(+), 52 deletions(-) (limited to 'src/main/java/sevenUnits/utils/ObjectProduct.java') diff --git a/src/main/java/sevenUnits/utils/ObjectProduct.java b/src/main/java/sevenUnits/utils/ObjectProduct.java index 1b8832e..23fe41c 100644 --- a/src/main/java/sevenUnits/utils/ObjectProduct.java +++ b/src/main/java/sevenUnits/utils/ObjectProduct.java @@ -29,7 +29,7 @@ import java.util.function.Function; /** * An immutable product of multiple objects of a type, such as base units. The * objects can be multiplied and exponentiated. - * + * * @author Adrien Hopkins * @param type of object that is being multiplied * @since 2019-10-16 @@ -41,10 +41,10 @@ public class ObjectProduct implements Nameable { * this value, a warning will be printed to standard error. */ private static final double ROUND_WARN_THRESHOLD = 1e-12; - + /** * Returns an empty ObjectProduct of a certain type - * + * * @param type of objects that can be multiplied * @return empty product * @since 2019-10-16 @@ -53,10 +53,10 @@ public class ObjectProduct implements Nameable { public static final ObjectProduct empty() { return new ObjectProduct<>(new HashMap<>()); } - + /** * Gets an {@code ObjectProduct} from an object-to-integer mapping - * + * * @param type of object in product * @param map map mapping objects to exponents * @return object product @@ -67,13 +67,13 @@ public class ObjectProduct implements Nameable { final Map map) { return new ObjectProduct<>(new HashMap<>(map)); } - + /** * Gets an ObjectProduct that has one of the inputted argument, and nothing * else. - * + * * @param object object that will be in the product - * @param type of object contained in returned ObjectProduct + * @param type of object contained in returned ObjectProduct * @return product * @since 2019-10-16 * @since v0.3.0 @@ -85,24 +85,22 @@ public class ObjectProduct implements Nameable { map.put(object, 1); return new ObjectProduct<>(map); } - + /** * The objects that make up the product, mapped to their exponents. This map * treats zero as null, and is immutable. - * + * * @since 2019-10-16 * @since v0.3.0 */ final Map exponents; - - /** - * The object's name and symbol - */ + + /** 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 * @since v0.3.0 @@ -110,10 +108,10 @@ public class ObjectProduct implements Nameable { ObjectProduct(final Map 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 @@ -125,7 +123,7 @@ public class ObjectProduct implements Nameable { e -> !Integer.valueOf(0).equals(e.getValue()))); this.nameSymbol = nameSymbol; } - + /** * Calculates the quotient of two products * @@ -141,17 +139,17 @@ public class ObjectProduct implements Nameable { final Set objects = new HashSet<>(); objects.addAll(this.getBaseSet()); objects.addAll(other.getBaseSet()); - + // get a list of all exponents final Map map = new HashMap<>(objects.size()); for (final T key : objects) { map.put(key, this.getExponent(key) - other.getExponent(key)); } - + // create the product return new ObjectProduct<>(map); } - + // this method relies on the use of ZeroIsNullMap @Override public boolean equals(final Object obj) { @@ -162,7 +160,7 @@ public class ObjectProduct implements Nameable { final ObjectProduct other = (ObjectProduct) obj; return Objects.equals(this.exponents, other.exponents); } - + /** * @return immutable map mapping objects to exponents * @since 2019-10-16 @@ -171,7 +169,7 @@ public class ObjectProduct implements Nameable { public Map exponentMap() { return this.exponents; } - + /** * @return a set of all of the base objects with non-zero exponents that make * up this dimension. @@ -180,7 +178,7 @@ public class ObjectProduct implements Nameable { */ public final Set getBaseSet() { final Set dimensions = new HashSet<>(); - + // add all dimensions with a nonzero exponent - zero exponents shouldn't // be there in the first place for (final T dimension : this.exponents.keySet()) { @@ -188,13 +186,13 @@ public class ObjectProduct implements Nameable { dimensions.add(dimension); } } - + return dimensions; } - + /** * Gets the exponent for a specific dimension. - * + * * @param dimension dimension to check * @return exponent for that dimension * @since 2018-12-12 @@ -203,17 +201,17 @@ public class ObjectProduct implements Nameable { public int getExponent(final T dimension) { return this.exponents.getOrDefault(dimension, 0); } - + @Override public NameSymbol getNameSymbol() { return this.nameSymbol; } - + @Override public int hashCode() { return Objects.hash(this.exponents); } - + /** * @return true if this product is a single object, i.e. it has one exponent * of one and no other nonzero exponents @@ -221,8 +219,8 @@ public class ObjectProduct implements Nameable { * @since v0.3.0 */ public boolean isSingleObject() { - int oneCount = 0; - boolean twoOrMore = false; // has exponents of 2 or more + var oneCount = 0; + var twoOrMore = false; // has exponents of 2 or more for (final T b : this.getBaseSet()) { if (this.getExponent(b) == 1) { oneCount++; @@ -232,7 +230,7 @@ public class ObjectProduct implements Nameable { } return oneCount == 1 && !twoOrMore; } - + /** * Multiplies this product by another * @@ -248,20 +246,20 @@ public class ObjectProduct implements Nameable { final Set objects = new HashSet<>(); objects.addAll(this.getBaseSet()); objects.addAll(other.getBaseSet()); - + // get a list of all exponents final Map map = new HashMap<>(objects.size()); for (final T key : objects) { map.put(key, this.getExponent(key) + other.getExponent(key)); } - + // create the product return new ObjectProduct<>(map); } - + /** * Returns this product, but to an exponent - * + * * @param exponent exponent * @return result of exponentiation * @since 2019-10-16 @@ -274,14 +272,14 @@ public class ObjectProduct implements Nameable { } return new ObjectProduct<>(map); } - + /** * Returns this product to an exponent, where every dimension is rounded to * the nearest integer. - * + * * This function will send a warning (via standard error) if the rounding * significantly changes the value. - * + * * @param exponent exponent to raise this product to * @return result of exponentiation * @@ -291,7 +289,7 @@ public class ObjectProduct implements Nameable { public ObjectProduct toExponentRounded(final double exponent) { final Map map = new HashMap<>(this.exponents); for (final T key : this.exponents.keySet()) { - final double newExponent = this.getExponent(key) * exponent; + final var newExponent = this.getExponent(key) * exponent; if (Math.abs( newExponent - Math.round(newExponent)) > ROUND_WARN_THRESHOLD) { System.err.printf( @@ -303,14 +301,14 @@ public class ObjectProduct implements Nameable { } return new ObjectProduct<>(map); } - + /** * Converts this product to a string using the objects' * {@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. - * + * *

* {@inheritDoc} */ @@ -320,11 +318,11 @@ public class ObjectProduct implements Nameable { .toString(o -> o instanceof Nameable ? ((Nameable) o).getShortName() : o.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 @@ -333,7 +331,7 @@ public class ObjectProduct implements Nameable { public String toString(final Function objectToString) { final List positiveStringComponents = new ArrayList<>(); final List negativeStringComponents = new ArrayList<>(); - + // 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); @@ -347,15 +345,15 @@ public class ObjectProduct implements Nameable { objectToString.apply(object), -exponent)); } } - - final String positiveString = positiveStringComponents.isEmpty() ? "1" + + final var positiveString = positiveStringComponents.isEmpty() ? "1" : String.join(" * ", positiveStringComponents); - final String negativeString = negativeStringComponents.isEmpty() ? "" + final var negativeString = negativeStringComponents.isEmpty() ? "" : " / " + String.join(" * ", negativeStringComponents); - + return positiveString + negativeString; } - + /** * @param nameSymbol name to add to this product * @return named version of this {@code ObjectProduct}, using data from -- cgit v1.2.3