public final class UnitManager extends Object
Singleton instance and main access point for quantities, units and unit conversions.
This class is thread-safe.
Modifier and Type | Method and Description |
---|---|
void |
addQuantity(Quantity quantity)
Add the specified quantity to this unit manager.
|
void |
addUnitAlias(String unitSymbolAlias,
String unitSymbol)
Add specified alias to be associated with the given official
unit symbol.
|
boolean |
canConvert(String unitSymbol1,
String unitSymbol2)
Check if it is possible to convert between the two specified units.
|
boolean |
canConvert(Unit unit1,
Unit unit2)
Check if it is possible to convert between the two specified units.
|
double |
convert(String fromUnitSymbol,
String toUnitSymbol,
double value)
Convert the specified value between the two given units.
|
static double |
convert(Unit fromUnit,
Unit toUnit,
double value)
Convert the specified value between the two given units.
|
List<Unit> |
findConvertibleUnits(String unitSymbol)
Return all units that are convertible with the unit of the specified
symbol.
|
List<Unit> |
findConvertibleUnits(Unit unit)
Return all units that are convertible with the specified unit.
|
List<Quantity> |
findQuantities(String unitSymbol)
Return all quantities that includes the unit of the specified symbol.
|
List<Quantity> |
findQuantities(Unit unit)
Return all quantities that includes the specified unit.
|
Quantity |
findQuantity(String quantityName)
Find quantity of the given name.
|
Quantity |
findQuantity(Unit unit)
Find quantity of the specified unit.
|
Unit |
findUnit(String unitSymbol)
Find corresponding unit instance for the given unit symbol.
|
String |
getDisplaySymbol(String unitSymbol)
Return the display symbol for the corresponding unit symbol.
|
String |
getDisplaySymbol(Unit unit)
Return the display symbol for the specified unit.
|
static UnitManager |
getInstance()
Return the sole instance of this class.
|
List<Quantity> |
getQuantities()
Return all quantities known by this unit manager.
|
void |
setDisplaySymbol(String unitSymbol,
String displaySymbol)
Set the specified display symbol for the given unit symbol.
|
void |
setDisplaySymbol(Unit unit,
String displaySymbol)
Set the specified display symbol for the given unit.
|
String |
toString() |
public static UnitManager getInstance()
public void addUnitAlias(String unitSymbolAlias, String unitSymbol)
The alias will be used when identifying Unit instances from unit symbols and affects all methods of this class taking unit symbol as argument.
Multiple aliases can be added for each unit symbol.
unitSymbolAlias
- Alias to add. Non-null.unitSymbol
- Unit symbol to associated alias with. Non-null.IllegalArgumentException
- If unitSymbolAlias or unitSymbol is null.public void setDisplaySymbol(String unitSymbol, String displaySymbol)
unitSymbol
- Official unit symbol. Non-null.displaySymbol
- Associated display symbol. Non-null.IllegalArgumentException
- If unitSymbol or displaySymbol is null.public void setDisplaySymbol(Unit unit, String displaySymbol)
unit
- Unit to set display symbol of. Non-null.displaySymbol
- Associated display symbol. Non-null.IllegalArgumentException
- If unitSymbol or displaySymbol is null.public List<Quantity> getQuantities()
public void addQuantity(Quantity quantity)
quantity
- Quantity to add. Non-null.IllegalArgumentException
- If quantity is null or already contained
in this manager.public Quantity findQuantity(String quantityName)
quantityName
- Name of quantity to find. Non-null.IllegalArgumentException
- If quantityName is null.public Unit findUnit(String unitSymbol)
The alias mapping is considered, and units are searched both case sensitive and case insensitive.
unitSymbol
- Unit symbol to find unit for. May be null for unitless.public List<Unit> findConvertibleUnits(Unit unit)
unit
- Unit to consider. Non-null.IllegalArgumentException
- If unit is null.public List<Unit> findConvertibleUnits(String unitSymbol)
unitSymbol
- Symbol of unit to consider. Null if unitless.public List<Quantity> findQuantities(Unit unit)
unit
- Unit to consider. Non-null.IllegalArgumentException
- If unit is null.public List<Quantity> findQuantities(String unitSymbol)
unitSymbol
- Unit symbol of unit to consider. Null if unitless.public Quantity findQuantity(Unit unit)
Note that a unit may be contained in multiple quantities. This method is convenient if the client knows that the unit exists in one quantity only. If it exists in more than one quantity, the first one encountered is returned.
unit
- Unit to consider. Non-null.IllegalArgumentException
- If unit is null.public boolean canConvert(Unit unit1, Unit unit2)
unit1
- First unit to consider. Non-null.unit2
- Second unit to consider. Non-null.IllegalArgumentException
- If unit1 or unit2 is null.public boolean canConvert(String unitSymbol1, String unitSymbol2)
unitSymbol1
- Unit symbol of first unit to consider. Null if unitless.unitSymbol2
- Unit symbol of second unit to consider. Null if unitless.public static double convert(Unit fromUnit, Unit toUnit, double value)
Note that it is the client responsibility to check if it makes sense to convert between the given units. This method simply converts the value to base of the from unit, and convert this result from base of the to unit, without considering the compatibility between the two.
fromUnit
- Current unit of value. Non-null.toUnit
- Unit to convert to. Non-null.value
- Value to convert.IllegalArgumentException
- If fromUnit or toUnit is null.public double convert(String fromUnitSymbol, String toUnitSymbol, double value)
Note that it is the client responsibility to check if it makes sense to convert between the given units. This method simply converts the value to base of the from unit, and convert this result from base of the to unit, without considering the compatibility between the two.
fromUnitSymbol
- Unit symbol of current unit of value. Non-null.toUnitSymbol
- Unit symbol of unit to convert to. Non-null.value
- Value to convert.IllegalArgumentException
- If fromUnitSymbol or toUnitSymbol is null.public String getDisplaySymbol(String unitSymbol)
unitSymbol
- Unit symbol to get display symbol of. As a convenience
for the client, null is allowed, in case an empty string is
returned.public String getDisplaySymbol(Unit unit)
unit
- Unit to get display symbol of. May be null in case
an empty string is returned.Copyright © 2020 Petroware AS - https://petroware.no