From 221f2a76de7cb2b8c3039c459269cac18a1758d5 Mon Sep 17 00:00:00 2001 From: Werner Keil Date: Sat, 23 Nov 2024 21:25:21 +0100 Subject: [PATCH] Changed visibility of some Units.addUnit() methods --- .../units/indriya/AbstractSystemOfUnits.java | 24 +++++--- .../java/tech/units/indriya/unit/Units.java | 59 +++++++++++++------ 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/main/java/tech/units/indriya/AbstractSystemOfUnits.java b/src/main/java/tech/units/indriya/AbstractSystemOfUnits.java index 0b81a4c2..6224f01d 100644 --- a/src/main/java/tech/units/indriya/AbstractSystemOfUnits.java +++ b/src/main/java/tech/units/indriya/AbstractSystemOfUnits.java @@ -50,7 +50,7 @@ *

* * @author Werner Keil - * @version 2.2, January 11, 2024 + * @version 2.3, November 23, 2024 * @since 1.0 */ public abstract class AbstractSystemOfUnits implements SystemOfUnits, Nameable { @@ -166,6 +166,11 @@ public Unit getUnit(String string, UnitStyle style) { return getUnit(string, style, false); } + /** + * Static helper class. + * + * @since 1.0 + */ protected static class Helper { static Set> getUnitsOfDimension(final Set> units, Dimension dimension) { if (dimension != null) { @@ -176,8 +181,9 @@ static Set> getUnitsOfDimension(final Set> units, Dimension dime } /** - * Adds a new named unit to the collection. + * Adds a new named unit to a collection. * + * @param units the collection to add to. * @param unit the unit being added. * @param name the name of the unit. * @return unit. @@ -188,11 +194,12 @@ public static > U addUnit(Set> units, U unit, String n } /** - * Adds a new named unit to the collection. + * Adds a new named unit to a collection. * + * @param units the collection to add to. * @param unit the unit being added. * @param name the name of the unit. - * @param name the symbol of the unit. + * @param symbol the symbol of the unit. * @return unit. * @since 1.0 */ @@ -201,11 +208,12 @@ public static > U addUnit(Set> units, U unit, String n } /** - * Adds a new named unit to the collection. - * + * Adds a new named unit to a set. + * + * @param units the set to add to. * @param unit the unit being added. * @param name the name of the unit. - * @param name the symbol of the unit. + * @param symbol the symbol of the unit. * @param style style of the unit. * @return unit. * @since 1.0.1 @@ -265,7 +273,7 @@ public static > U addUnit(Set> units, U unit, final St } /** - * Adds a new labeled unit to the set. + * Adds a new labeled unit to a set. * * @param units the set to add to. * @param unit the unit being added. diff --git a/src/main/java/tech/units/indriya/unit/Units.java b/src/main/java/tech/units/indriya/unit/Units.java index eb7216c5..fe34fe28 100644 --- a/src/main/java/tech/units/indriya/unit/Units.java +++ b/src/main/java/tech/units/indriya/unit/Units.java @@ -31,6 +31,8 @@ import static tech.units.indriya.AbstractUnit.ONE; +import java.util.Set; + import javax.measure.Quantity; import javax.measure.Unit; import javax.measure.quantity.Acceleration; @@ -264,7 +266,7 @@ public String getName() { * The SI derived unit for mass quantities (standard name g). The * base unit for mass quantity is {@link #KILOGRAM}. */ - public static final Unit GRAM = addUnit(KILOGRAM.divide(1000), "Gram"); + public static final Unit GRAM = addUnit(INSTANCE.units, KILOGRAM.divide(1000), "Gram"); /** * The SI unit for plane angle quantities (standard name rad). One @@ -539,7 +541,7 @@ public String getName() { * * @see Wikipedia: Kilometres per hour */ - public static final Unit KILOMETRE_PER_HOUR = addUnit(METRE_PER_SECOND.multiply(RationalNumber.of(5, 18)), "Kilometre per hour") + public static final Unit KILOMETRE_PER_HOUR = addUnit(INSTANCE.units, METRE_PER_SECOND.multiply(RationalNumber.of(5, 18)), "Kilometre per hour") .asType(Speed.class); ///////////////////////////////////////////////////////////////// @@ -609,15 +611,30 @@ public String getName() { public static Units getInstance() { return INSTANCE; } - + static { // have to add AbstractUnit.ONE as Dimensionless, too - addUnit(ONE); + addUnit(INSTANCE.units, ONE); Helper.addUnit(INSTANCE.units, ONE, "One"); INSTANCE.quantityToUnit.put(Dimensionless.class, ONE); - } + } - /** + /** + * Adds a new unit not mapped to any specified quantity type and puts a name and symbol. + * + * @param unit + * the unit being added. + * @param name + * the string to use as name + * @param symbol + * the string to use as symbol + * @return unit. + */ + protected static > U addUnit(final Set> units, U unit, String name, String symbol) { + return Helper.addUnit(units, unit, name, symbol); + } + + /** * Adds a new unit not mapped to any specified quantity type and puts a text * as symbol or label. * @@ -627,11 +644,11 @@ public static Units getInstance() { * the string to use as name * @return unit. */ - protected static > U addUnit(U unit, String name) { + protected static > U addUnit(final Set> units, U unit, String name) { if (name != null && unit instanceof AbstractUnit) { - return Helper.addUnit(INSTANCE.units, unit, name); + return Helper.addUnit(units, unit, name); } else { - INSTANCE.units.add(unit); + units.add(unit); } return unit; } @@ -642,10 +659,20 @@ protected static > U addUnit(U unit, String name) { * @param unit the unit being added. * @return unit. */ - protected static > U addUnit(U unit) { - INSTANCE.units.add(unit); + protected static > U addUnit(final Set> units, U unit) { + units.add(unit); return unit; } + + /** + * Adds a new unit not mapped to any specified quantity type. + * + * @param unit the unit being added. + * @return unit. + */ + private static > U addUnit(U unit) { + return addUnit(INSTANCE.units, unit); + } /** * Adds a new unit and maps it to the specified quantity type. @@ -655,7 +682,7 @@ protected static > U addUnit(U unit) { * @param type the quantity type. * @return unit. */ - protected static > U addUnit(U unit, String name, Class> type) { + private static > U addUnit(U unit, String name, Class> type) { Helper.addUnit(INSTANCE.units, unit, name); INSTANCE.quantityToUnit.put(type, unit); return unit; @@ -668,13 +695,9 @@ protected static > U addUnit(U unit, String name, Clas * @param type the quantity type. * @return unit. */ - protected static > U addUnit(U unit, Class> type) { + private static > U addUnit(U unit, Class> type) { INSTANCE.units.add(unit); INSTANCE.quantityToUnit.put(type, unit); return unit; - } - - protected static > U addUnit(U unit, String name, String symbol) { - return Helper.addUnit(INSTANCE.units, unit, name, symbol); - } + } }