ucar.units
Class UnitDBImpl

java.lang.Object
  extended by 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.

Version:
$Id: UnitDBImpl.java 64 2006-07-12 22:30:50Z edavis $
Author:
Steven R. Emmerson
See Also:
Serialized Form

Constructor Summary
protected UnitDBImpl(int nameCount, int symbolCount)
          Constructs from the expected number of names and symbols.
 
Method Summary
 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 getIterator()
          Gets an iterator over the units in the database.
 int nameCount()
          Return the number of names in this database
 int symbolCount()
          Return the number of symbols in this database.
 String toString()
          Returns the string representation of this database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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 interface UnitDB
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 interface UnitDB
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 interface UnitDB
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 interface UnitDB
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 interface UnitDB
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. If null , 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 interface UnitDB
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.

Specified by:
get in interface UnitDB
Parameters:
id - The id to be matched.
Returns:
The unit whose name, plural, or symbol matches or null if no such unit was found.

getByName

public Unit getByName(String name)
Gets a unit by name.

Specified by:
getByName in interface UnitDB
Parameters:
name - The name to be matched.
Returns:
The unit whose name, plural, or alias matches or null if no such unit was found.

getBySymbol

public Unit getBySymbol(String symbol)
Gets a unit by symbol.

Specified by:
getBySymbol in interface UnitDB
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.

Specified by:
toString in interface UnitDB
Overrides:
toString in class Object
Returns:
The string representation of this database.

getIterator

public final Iterator getIterator()
Gets an iterator over the units in the database.

Specified by:
getIterator in interface UnitDB
Returns:
An iterator over the units in the database. The iterator's next() method returns objects of type Unit.


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.