Package ucar.units
Class UnitDBImpl
- java.lang.Object
-
- ucar.units.UnitDBImpl
-
- All Implemented Interfaces:
Serializable
,UnitDB
- Direct Known Subclasses:
StandardUnitDB
public class UnitDBImpl extends Object implements UnitDB, Serializable
Provides most of a concrete implementation of a database of units.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
UnitDBImpl(int nameCount, int symbolCount)
Constructs from the expected number of names and symbols.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(UnitDBImpl that)
Adds all the entries in another UnitDBImpl to this database.void
addAlias(String alias, String name)
Adds an alias for a unit already in the database.void
addAlias(String alias, String name, String symbol)
Adds an alias for a unit already in the database.void
addAlias(String alias, String name, String symbol, String plural)
Adds an alias for a unit already in the database.void
addAlias(UnitID alias, String name)
Adds an alias for a unit already in the database.void
addSymbol(String symbol, String name)
Adds a symbol for a unit already in the database.void
addUnit(Unit unit)
Adds a unit to the database.Unit
get(String id)
Gets a unit by either name, plural, or symbol.Unit
getByName(String name)
Gets a unit by name.Unit
getBySymbol(String symbol)
Gets a unit by symbol.Iterator<Unit>
getIterator()
Gets an iterator over the units in the database.int
nameCount()
Return the number of names in this databaseint
symbolCount()
Return the number of symbols in this database.String
toString()
Returns the string representation of this database.
-
-
-
Constructor Detail
-
UnitDBImpl
protected UnitDBImpl(int nameCount, int symbolCount)
Constructs from the expected number of names and symbols. The sizes will be used to construct the initial database but will not limit its growth.- Parameters:
nameCount
- The expected number of names (including plurals and aliases).symbolCount
- The expected number of symbols.
-
-
Method Detail
-
add
public void add(UnitDBImpl that) throws UnitExistsException
Adds all the entries in another UnitDBImpl to this database.- Parameters:
that
- The other UnitDBImpl.- Throws:
UnitExistsException
- Attempt to redefine an existing entry.
-
nameCount
public int nameCount()
Return the number of names in this database- Returns:
- The total number of names, plurals, and aliases.
-
symbolCount
public int symbolCount()
Return the number of symbols in this database.- Returns:
- The number of symbols in this database.
-
addUnit
public void addUnit(Unit unit) throws UnitExistsException, NameException
Adds a unit to the database.- Specified by:
addUnit
in interfaceUnitDB
- Parameters:
unit
- The unit to be added.- Throws:
UnitExistsException
- Another unit with the same name or symbol already exists in the database.NameException
- Bad unit name.
-
addAlias
public final void addAlias(String alias, String name) throws NoSuchUnitException, UnitExistsException
Adds an alias for a unit already in the database.- Specified by:
addAlias
in interfaceUnitDB
- Parameters:
alias
- An alias for the unit.name
- The name of the unit already in the database.- Throws:
UnitExistsException
- Another unit with the same name or symbol already exists in the database.NoSuchUnitException
- The unit isn't in the database.
-
addAlias
public final void addAlias(String alias, String name, String symbol) throws NoSuchUnitException, UnitExistsException
Adds an alias for a unit already in the database.- Specified by:
addAlias
in interfaceUnitDB
- Parameters:
alias
- An alias for the unit.name
- The name of the unit already in the database.symbol
- The symbol for the unit.- Throws:
UnitExistsException
- Another unit with the same name or symbol already exists in the database.NoSuchUnitException
- The unit isn't in the database.
-
addSymbol
public final void addSymbol(String symbol, String name) throws NoSuchUnitException, UnitExistsException
Adds a symbol for a unit already in the database.- Specified by:
addSymbol
in interfaceUnitDB
- Parameters:
symbol
- The symbol for the unit.name
- The name of the unit already in the database.- Throws:
UnitExistsException
- Another unit with the same name or symbol already exists in the database.NoSuchUnitException
- The unit isn't in the database.
-
addAlias
public final void addAlias(String alias, String name, String symbol, String plural) throws NoSuchUnitException, UnitExistsException
Adds an alias for a unit already in the database.- Specified by:
addAlias
in interfaceUnitDB
- Parameters:
alias
- The alias to be added to the database. May be null.name
- The name of the unit to have an alias added to the database.symbol
- The symbol to be added. May be null.plural
- The plural form of the alias. Ifnull
, then regular plural-forming rules are followed.- Throws:
NoSuchUnitException
- The unit is not in the database.UnitExistsException
- Another unit with the same alias is already in the database.
-
addAlias
public final void addAlias(UnitID alias, String name) throws NoSuchUnitException, UnitExistsException
Adds an alias for a unit already in the database.- Specified by:
addAlias
in interfaceUnitDB
- Parameters:
alias
- The alias to be added to the database.name
- The name of the unit to have an alias added to the database.- Throws:
NoSuchUnitException
- The unit is not in the database.UnitExistsException
- Another unit with the same alias is already in the database.
-
get
public Unit get(String id)
Gets a unit by either name, plural, or symbol. Retrieving the unit by symbol is attempted before retrieving the unit by name because symbol comparisons are case sensitive and, hence, should be more robust.
-
getBySymbol
public Unit getBySymbol(String symbol)
Gets a unit by symbol.- Specified by:
getBySymbol
in interfaceUnitDB
- Parameters:
symbol
- The symbol to be matched.- Returns:
- The unit whose symbol matches or
null
if no such unit was found.
-
toString
public String toString()
Returns the string representation of this database.
-
getIterator
public final Iterator<Unit> getIterator()
Gets an iterator over the units in the database.- Specified by:
getIterator
in interfaceUnitDB
- Returns:
- An iterator over the units in the database. The iterator's
next()
method returns objects of typeUnit
.
-
-