From 16dfc3d7c7813fa343f3f3502bf5a2fea4a252cc Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Thu, 4 Nov 2021 16:01:24 -0500 Subject: Added a bunch of tests related to unit values --- src/main/java/sevenUnits/unit/BritishImperial.java | 39 +++--- src/main/java/sevenUnits/unit/LinearUnitValue.java | 5 +- src/main/java/sevenUnits/unit/UnitValue.java | 3 +- src/main/java/sevenUnits/utils/ObjectProduct.java | 135 ++++++++++----------- 4 files changed, 95 insertions(+), 87 deletions(-) (limited to 'src/main') diff --git a/src/main/java/sevenUnits/unit/BritishImperial.java b/src/main/java/sevenUnits/unit/BritishImperial.java index 81a3f2a..743beeb 100644 --- a/src/main/java/sevenUnits/unit/BritishImperial.java +++ b/src/main/java/sevenUnits/unit/BritishImperial.java @@ -37,7 +37,7 @@ public final class BritishImperial { public static final LinearUnit ROOD = Length.ROD.times(Length.FURLONG); public static final LinearUnit ACRE = Length.FURLONG.times(Length.CHAIN); } - + /** * Imperial units that measure length * @@ -46,7 +46,8 @@ public final class BritishImperial { */ public static final class Length { /** - * According to the International Yard and Pound of 1959, a yard is defined as exactly 0.9144 metres. + * According to the International Yard and Pound of 1959, a yard is + * defined as exactly 0.9144 metres. */ public static final LinearUnit YARD = Metric.METRE.times(0.9144); public static final LinearUnit FOOT = YARD.dividedBy(3); @@ -56,15 +57,15 @@ public final class BritishImperial { public static final LinearUnit FURLONG = CHAIN.times(10); public static final LinearUnit MILE = FURLONG.times(8); public static final LinearUnit LEAGUE = MILE.times(3); - + public static final LinearUnit NAUTICAL_MILE = Metric.METRE.times(1852); public static final LinearUnit CABLE = NAUTICAL_MILE.dividedBy(10); public static final LinearUnit FATHOM = CABLE.dividedBy(100); - + public static final LinearUnit ROD = YARD.times(5.5); public static final LinearUnit LINK = ROD.dividedBy(25); } - + /** * British Imperial units that measure mass. * @@ -82,7 +83,7 @@ public final class BritishImperial { public static final LinearUnit LONG_TON = HUNDREDWEIGHT.times(20); public static final LinearUnit SLUG = Metric.KILOGRAM.times(14.59390294); } - + /** * British Imperial units that measure volume * @@ -90,27 +91,33 @@ public final class BritishImperial { * @since 2019-11-08 */ public static final class Volume { - public static final LinearUnit FLUID_OUNCE = Metric.LITRE.withPrefix(Metric.MILLI).times(28.4130625); + public static final LinearUnit FLUID_OUNCE = Metric.LITRE + .withPrefix(Metric.MILLI).times(28.4130625); public static final LinearUnit GILL = FLUID_OUNCE.times(5); public static final LinearUnit PINT = FLUID_OUNCE.times(20); public static final LinearUnit QUART = PINT.times(2); public static final LinearUnit GALLON = QUART.times(4); public static final LinearUnit PECK = GALLON.times(2); public static final LinearUnit BUSHEL = PECK.times(4); - + public static final LinearUnit CUBIC_INCH = Length.INCH.toExponent(3); public static final LinearUnit CUBIC_FOOT = Length.FOOT.toExponent(3); public static final LinearUnit CUBIC_YARD = Length.YARD.toExponent(3); public static final LinearUnit ACRE_FOOT = Area.ACRE.times(Length.FOOT); } - - public static final LinearUnit OUNCE_FORCE = Mass.OUNCE.times(Metric.Constants.EARTH_GRAVITY); - public static final LinearUnit POUND_FORCE = Mass.POUND.times(Metric.Constants.EARTH_GRAVITY); - - public static final LinearUnit BRITISH_THERMAL_UNIT = Metric.JOULE.times(1055.06); + + public static final LinearUnit OUNCE_FORCE = Mass.OUNCE + .times(Metric.Constants.EARTH_GRAVITY); + public static final LinearUnit POUND_FORCE = Mass.POUND + .times(Metric.Constants.EARTH_GRAVITY); + + public static final LinearUnit BRITISH_THERMAL_UNIT = Metric.JOULE + .times(1055.06); public static final LinearUnit CALORIE = Metric.JOULE.times(4.184); public static final LinearUnit KILOCALORIE = Metric.JOULE.times(4184); - - public static final Unit FAHRENHEIT = Unit.fromConversionFunctions(Metric.KELVIN.getBase(), - tempK -> tempK * 1.8 - 459.67, tempF -> (tempF + 459.67) / 1.8); + + public static final Unit FAHRENHEIT = Unit + .fromConversionFunctions(Metric.KELVIN.getBase(), + tempK -> tempK * 1.8 - 459.67, tempF -> (tempF + 459.67) / 1.8) + .withName(NameSymbol.of("degrees Fahrenheit", "\u00B0F")); } diff --git a/src/main/java/sevenUnits/unit/LinearUnitValue.java b/src/main/java/sevenUnits/unit/LinearUnitValue.java index 2219ffd..a50e1f5 100644 --- a/src/main/java/sevenUnits/unit/LinearUnitValue.java +++ b/src/main/java/sevenUnits/unit/LinearUnitValue.java @@ -332,9 +332,10 @@ public final class LinearUnitValue { + (showUncertainty ? ")" : ""); // create string - if (primaryName.isEmpty() && symbol.isEmpty()) + if (chosenName == null) return String.format("%s unnamed unit (= %s %s)", valueString, - baseValueString, this.unit.getBase()); + baseValueString, this.unit.getBase() + .toString(unit -> unit.getSymbol().orElseThrow())); else return String.format("%s %s", valueString, chosenName); } diff --git a/src/main/java/sevenUnits/unit/UnitValue.java b/src/main/java/sevenUnits/unit/UnitValue.java index 2fedd81..f6d18f8 100644 --- a/src/main/java/sevenUnits/unit/UnitValue.java +++ b/src/main/java/sevenUnits/unit/UnitValue.java @@ -163,7 +163,8 @@ public final class UnitValue { if (primaryName.isEmpty() && symbol.isEmpty()) { final double baseValue = this.getUnit().convertToBase(this.getValue()); return String.format("%s unnamed unit (= %s %s)", this.getValue(), - baseValue, this.getUnit().getBase()); + baseValue, this.getUnit().getBase() + .toString(unit -> unit.getSymbol().orElseThrow())); } else { final String unitName = symbol.orElse(primaryName.get()); return this.getValue() + " " + unitName; diff --git a/src/main/java/sevenUnits/utils/ObjectProduct.java b/src/main/java/sevenUnits/utils/ObjectProduct.java index 1dacb7d..5b1b739 100644 --- a/src/main/java/sevenUnits/utils/ObjectProduct.java +++ b/src/main/java/sevenUnits/utils/ObjectProduct.java @@ -27,8 +27,8 @@ 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 - * exponentiated. + * An immutable product of multiple objects of a type, such as base units. The + * objects can be multiplied and exponentiated. * * @author Adrien Hopkins * @since 2019-10-16 @@ -37,38 +37,35 @@ public final class ObjectProduct { /** * Returns an empty ObjectProduct of a certain type * - * @param - * type of objects that can be multiplied + * @param type of objects that can be multiplied * @return empty product * @since 2019-10-16 */ 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 + * @param type of object in product + * @param map map mapping objects to exponents * @return object product * @since 2019-10-16 */ - public static final ObjectProduct fromExponentMapping(final Map map) { + public static final ObjectProduct fromExponentMapping( + final Map map) { return new ObjectProduct<>(new HashMap<>(map)); } - + /** - * Gets an ObjectProduct that has one of the inputted argument, and nothing else. + * Gets an ObjectProduct that has one of the inputted argument, and nothing + * else. * - * @param object - * object that will be in the product + * @param object object that will be in the product * @return product * @since 2019-10-16 - * @throws NullPointerException - * if object is null + * @throws NullPointerException if object is null */ public static final ObjectProduct oneOf(final T object) { Objects.requireNonNull(object, "object must not be null."); @@ -76,35 +73,34 @@ public final class ObjectProduct { 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. + * The objects that make up the product, mapped to their exponents. This map + * treats zero as null, and is immutable. * * @since 2019-10-16 */ final Map exponents; - + /** * Creates the {@code ObjectProduct}. * - * @param exponents - * objects that make up this product + * @param exponents objects that make up this product * @since 2019-10-16 */ private ObjectProduct(final Map exponents) { - this.exponents = Collections.unmodifiableMap(ConditionalExistenceCollections.conditionalExistenceMap(exponents, - e -> !Integer.valueOf(0).equals(e.getValue()))); + this.exponents = Collections.unmodifiableMap( + ConditionalExistenceCollections.conditionalExistenceMap(exponents, + e -> !Integer.valueOf(0).equals(e.getValue()))); } - + /** * Calculates the quotient of two products * - * @param other - * other product + * @param other other product * @return quotient of two products * @since 2019-10-16 - * @throws NullPointerException - * if other is null + * @throws NullPointerException if other is null */ public ObjectProduct dividedBy(final ObjectProduct other) { Objects.requireNonNull(other, "other must not be null."); @@ -112,17 +108,17 @@ public final class ObjectProduct { 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) { @@ -133,7 +129,7 @@ public final class ObjectProduct { final ObjectProduct other = (ObjectProduct) obj; return Objects.equals(this.exponents, other.exponents); } - + /** * @return immutable map mapping objects to exponents * @since 2019-10-16 @@ -141,30 +137,31 @@ public final class ObjectProduct { public Map exponentMap() { return this.exponents; } - + /** - * @return a set of all of the base objects with non-zero exponents that make up this dimension. + * @return a set of all of the base objects with non-zero exponents that make + * up this dimension. * @since 2018-12-12 * @since v0.1.0 */ 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 + + // add all dimensions with a nonzero exponent - zero exponents shouldn't + // be there in the first place for (final T dimension : this.exponents.keySet()) { if (!this.exponents.get(dimension).equals(0)) { dimensions.add(dimension); } } - + return dimensions; } - + /** * Gets the exponent for a specific dimension. * - * @param dimension - * dimension to check + * @param dimension dimension to check * @return exponent for that dimension * @since 2018-12-12 * @since v0.1.0 @@ -172,14 +169,15 @@ public final class ObjectProduct { public int getExponent(final T dimension) { return this.exponents.getOrDefault(dimension, 0); } - + @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 + * @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 isSingleObject() { @@ -194,16 +192,14 @@ public final class ObjectProduct { } return oneCount == 1 && !twoOrMore; } - + /** * Multiplies this product by another * - * @param other - * other product + * @param other other product * @return product of two products * @since 2019-10-16 - * @throws NullPointerException - * if other is null + * @throws NullPointerException if other is null */ public ObjectProduct times(final ObjectProduct other) { Objects.requireNonNull(other, "other must not be null."); @@ -211,22 +207,21 @@ public final class ObjectProduct { 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 + * @param exponent exponent * @return result of exponentiation * @since 2019-10-16 */ @@ -237,11 +232,12 @@ public final class ObjectProduct { } return new ObjectProduct<>(map); } - + /** - * 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. + * 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. * *

* {@inheritDoc} @@ -250,35 +246,38 @@ public final class ObjectProduct { 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} + * 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 + * @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 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", objectToString.apply(object), exponent)); + if (exponent > 1) { + positiveStringComponents.add(String.format("%s^%d", + objectToString.apply(object), exponent)); + } else if (exponent == 1) { + positiveStringComponents.add(objectToString.apply(object)); } else if (exponent < 0) { - negativeStringComponents.add(String.format("%s^%d", objectToString.apply(object), -exponent)); + negativeStringComponents.add(String.format("%s^%d", + objectToString.apply(object), -exponent)); } } - + final String positiveString = positiveStringComponents.isEmpty() ? "1" : String.join(" * ", positiveStringComponents); final String negativeString = negativeStringComponents.isEmpty() ? "" : " / " + String.join(" * ", negativeStringComponents); - + return positiveString + negativeString; } } -- cgit v1.2.3