Skip to content

Commit

Permalink
Changed visibility of some Units.addUnit() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Nov 23, 2024
1 parent f482fc0 commit 221f2a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
24 changes: 16 additions & 8 deletions src/main/java/tech/units/indriya/AbstractSystemOfUnits.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* </p>
*
* @author <a href="mailto:[email protected]">Werner Keil</a>
* @version 2.2, January 11, 2024
* @version 2.3, November 23, 2024
* @since 1.0
*/
public abstract class AbstractSystemOfUnits implements SystemOfUnits, Nameable {
Expand Down Expand Up @@ -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<Unit<?>> getUnitsOfDimension(final Set<Unit<?>> units, Dimension dimension) {
if (dimension != null) {
Expand All @@ -176,8 +181,9 @@ static Set<Unit<?>> getUnitsOfDimension(final Set<Unit<?>> 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 <code>unit</code>.
Expand All @@ -188,11 +194,12 @@ public static <U extends Unit<?>> U addUnit(Set<Unit<?>> 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 <code>unit</code>.
* @since 1.0
*/
Expand All @@ -201,11 +208,12 @@ public static <U extends Unit<?>> U addUnit(Set<Unit<?>> 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 <code>unit</code>.
* @since 1.0.1
Expand Down Expand Up @@ -265,7 +273,7 @@ public static <U extends Unit<?>> U addUnit(Set<Unit<?>> 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.
Expand Down
59 changes: 41 additions & 18 deletions src/main/java/tech/units/indriya/unit/Units.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -264,7 +266,7 @@ public String getName() {
* The SI derived unit for mass quantities (standard name <code>g</code>). The
* base unit for mass quantity is {@link #KILOGRAM}.
*/
public static final Unit<Mass> GRAM = addUnit(KILOGRAM.divide(1000), "Gram");
public static final Unit<Mass> GRAM = addUnit(INSTANCE.units, KILOGRAM.divide(1000), "Gram");

/**
* The SI unit for plane angle quantities (standard name <code>rad</code>). One
Expand Down Expand Up @@ -539,7 +541,7 @@ public String getName() {
*
* @see <a href="https://en.wikipedia.org/wiki/Kilometres_per_hour"> Wikipedia: Kilometres per hour</a>
*/
public static final Unit<Speed> KILOMETRE_PER_HOUR = addUnit(METRE_PER_SECOND.multiply(RationalNumber.of(5, 18)), "Kilometre per hour")
public static final Unit<Speed> KILOMETRE_PER_HOUR = addUnit(INSTANCE.units, METRE_PER_SECOND.multiply(RationalNumber.of(5, 18)), "Kilometre per hour")
.asType(Speed.class);

/////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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 <code>unit</code>.
*/
protected static <U extends AbstractUnit<?>> U addUnit(final Set<Unit<?>> 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.
*
Expand All @@ -627,11 +644,11 @@ public static Units getInstance() {
* the string to use as name
* @return <code>unit</code>.
*/
protected static <U extends Unit<?>> U addUnit(U unit, String name) {
protected static <U extends Unit<?>> U addUnit(final Set<Unit<?>> 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;
}
Expand All @@ -642,10 +659,20 @@ protected static <U extends Unit<?>> U addUnit(U unit, String name) {
* @param unit the unit being added.
* @return <code>unit</code>.
*/
protected static <U extends Unit<?>> U addUnit(U unit) {
INSTANCE.units.add(unit);
protected static <U extends Unit<?>> U addUnit(final Set<Unit<?>> 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 <code>unit</code>.
*/
private static <U extends Unit<?>> U addUnit(U unit) {
return addUnit(INSTANCE.units, unit);
}

/**
* Adds a new unit and maps it to the specified quantity type.
Expand All @@ -655,7 +682,7 @@ protected static <U extends Unit<?>> U addUnit(U unit) {
* @param type the quantity type.
* @return <code>unit</code>.
*/
protected static <U extends AbstractUnit<?>> U addUnit(U unit, String name, Class<? extends Quantity<?>> type) {
private static <U extends AbstractUnit<?>> U addUnit(U unit, String name, Class<? extends Quantity<?>> type) {
Helper.addUnit(INSTANCE.units, unit, name);
INSTANCE.quantityToUnit.put(type, unit);
return unit;
Expand All @@ -668,13 +695,9 @@ protected static <U extends AbstractUnit<?>> U addUnit(U unit, String name, Clas
* @param type the quantity type.
* @return <code>unit</code>.
*/
protected static <U extends AbstractUnit<?>> U addUnit(U unit, Class<? extends Quantity<?>> type) {
private static <U extends AbstractUnit<?>> U addUnit(U unit, Class<? extends Quantity<?>> type) {
INSTANCE.units.add(unit);
INSTANCE.quantityToUnit.put(type, unit);
return unit;
}

protected static <U extends AbstractUnit<?>> U addUnit(U unit, String name, String symbol) {
return Helper.addUnit(INSTANCE.units, unit, name, symbol);
}
}
}

0 comments on commit 221f2a7

Please sign in to comment.