UoM .Net
UnitManager Class Reference

Units of measurement manager. More...

Public Member Functions

void AddQuantity (Quantity quantity)
 Add the specified quantity to this unit manager. More...
 
void AddUnitAlias (string unitSymbolAlias, string unitSymbol)
 Add specified alias to be associated with the given official unit symbol. More...
 
bool CanConvert (Unit unit1, Unit unit2)
 Check if it is possible to convert between the two specified units. More...
 
bool CanConvert (string unitSymbol1, string unitSymbol2)
 Check if it is possible to convert between the two specified units. More...
 
double Convert (string fromUnitSymbol, string toUnitSymbol, double value)
 Convert the specified value between the two given units. More...
 
IList< UnitFindConvertibleUnits (Unit unit)
 Return all units that are convertible with the specified unit. More...
 
IList< UnitFindConvertibleUnits (string unitSymbol)
 Return all units that are convertible with the unit of the specified symbol. More...
 
IList< QuantityFindQuantities (Unit unit)
 Return all quantities that includes the specified unit. More...
 
IList< QuantityFindQuantities (string unitSymbol)
 Return all quantities that includes the unit of the specified symbol. More...
 
Quantity FindQuantity (string quantityName)
 Find quantity of the given name. More...
 
Quantity FindQuantity (Unit unit)
 Find quantity of the specified unit. More...
 
Unit FindUnit (string unitSymbol)
 Find corresponding unit instance for the given unit symbol. More...
 
string GetDisplaySymbol (Unit unit)
 Return the display symbol for the given unit. More...
 
string GetDisplaySymbol (string unitSymbol)
 Return the display symbol for the given unit. More...
 
IList< QuantityGetQuantities ()
 Return all quantities known by this unit manager. More...
 
void SetDisplaySymbol (string unitSymbol, string displaySymbol)
 Set the specified display symbol for the given unit symbol. More...
 
void SetDisplaySymbol (Unit unit, string displaySymbol)
 Set the specified display symbol for the given unit. More...
 
override string ToString ()
 

Static Public Member Functions

static double Convert (Unit fromUnit, Unit toUnit, double value)
 Convert the specified value between the two given units. More...
 
static UnitManager GetInstance ()
 Return the sole instance of this class. More...
 

Detailed Description

Units of measurement manager.

Singleton instance and main access point for quantities, units and unit conversions.

This class is thread-safe.

Author
Petroware AS

Member Function Documentation

void AddQuantity ( Quantity  quantity)

Add the specified quantity to this unit manager.

Parameters
quantityQuantity to add. Non-null.
Exceptions
ArgumentNullExceptionIf quantity is null.
ArgumentExceptionIf quantity is already contained in this manager.
void AddUnitAlias ( string  unitSymbolAlias,
string  unitSymbol 
)

Add specified alias to be associated with the given official unit symbol.

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.

Parameters
unitSymbolAliasAlias to add. Non-null.
unitSymbolUnit symbol to associated alias with. Non-null.
Exceptions
ArgumentNullExceptionIf unitSymbolAlias or unitSymbol is null.
bool CanConvert ( Unit  unit1,
Unit  unit2 
)

Check if it is possible to convert between the two specified units.

Parameters
unit1First unit to consider. Non-null.
unit2Second unit to consider. Non-null.
Returns
True if it is possible to convert between the two, false otherwise.
Exceptions
ArgumentNullExceptionIf unit1 or unit2 is null.
bool CanConvert ( string  unitSymbol1,
string  unitSymbol2 
)

Check if it is possible to convert between the two specified units.

Parameters
unitSymbol1Unit symbol of first unit to consider. Null if unitless.
unitSymbol2Unit symbol of second unit to consider. Null if unitless.
Returns
True if it is possible to convert between the two, false otherwise.
static double Convert ( Unit  fromUnit,
Unit  toUnit,
double  value 
)
static

Convert the specified value between the two given units.

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.

Parameters
fromUnitCurrent unit of value. Non-null.
toUnitUnit to convert to. Non-null.
valueValue to convert.
Returns
Converted value.
Exceptions
ArgumentNullExceptionIf fromUnit or toUnit is null.
double Convert ( string  fromUnitSymbol,
string  toUnitSymbol,
double  value 
)

Convert the specified value between the two given units.

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.

Parameters
fromUnitSymbolUnit symbol of current unit of value. Non-null.
toUnitSymbolUnit symbol of unit to convert to. Non-null.
valueValue to convert.
Returns
Converted value, or the input value it unit symbols are unknown.
Exceptions
ArgumentNullExceptionIf fromUnitSymbol or toUnitSymbol is null.
IList<Unit> FindConvertibleUnits ( Unit  unit)

Return all units that are convertible with the specified unit.

Parameters
unitUnit to consider. Non-null.
Returns
All convertible units. Never null.
Exceptions
ArgumentNullExceptionIf unit is null.
IList<Unit> FindConvertibleUnits ( string  unitSymbol)

Return all units that are convertible with the unit of the specified symbol.

Parameters
unitSymbolSymbol of unit to consider. Null if unitless.
Returns
All convertible units. Never null.
IList<Quantity> FindQuantities ( Unit  unit)

Return all quantities that includes the specified unit.

Parameters
unitUnit to consider. Non-null.
Returns
Requested quantities. Never null.
Exceptions
ArgumentNullExceptionIf unit is null.
IList<Quantity> FindQuantities ( string  unitSymbol)

Return all quantities that includes the unit of the specified symbol.

Parameters
unitSymbolUnit symbol of unit to consider. Null if unitless.
Returns
Requested quantities. Never null.
Quantity FindQuantity ( string  quantityName)

Find quantity of the given name.

Parameters
quantityNameName of quantity to find. Non-null.
Returns
Requested quantity or null if not found.
Exceptions
ArgumentNullExceptionIf quantityName is null.
Quantity FindQuantity ( Unit  unit)

Find quantity of the specified 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.

Parameters
unitUnit to consider. Non-null.
Returns
Requested quantity or null if none found.
Exceptions
ArgumentNullExceptionIf unit is null.
Unit FindUnit ( string  unitSymbol)

Find corresponding unit instance for the given unit symbol.

The alias mapping is considered, and units are searched both case sensitive and case insensitive.

Parameters
unitSymbolUnit symbol to find unit for. May be null for unitless.
Returns
Associated unit, or null if not found.
string GetDisplaySymbol ( Unit  unit)

Return the display symbol for the given unit.

Parameters
unitUnit to get display symbol of. As a convenience for the client, null is allowed, in case an empty string is returned.
Returns
Display symbol of specified unit. Empty string if unitless. Never null.
string GetDisplaySymbol ( string  unitSymbol)

Return the display symbol for the given unit.

Parameters
unitSymbolUnit symbol of the unit to get display symbol of. Non-null.
Returns
Display symbol of specified unit. Null if unitless.
static UnitManager GetInstance ( )
static

Return the sole instance of this class.

Returns
The sole instance of this class. Never null.
IList<Quantity> GetQuantities ( )

Return all quantities known by this unit manager.

Returns
All quantities. Never null.
void SetDisplaySymbol ( string  unitSymbol,
string  displaySymbol 
)

Set the specified display symbol for the given unit symbol.

Parameters
unitSymbolOfficial unit symbol. Non-null.
displaySymbolAssociated display symbol. Non-null.
Exceptions
ArgumentNullExceptionIf unitSymbol or displaySymbol is null.
void SetDisplaySymbol ( Unit  unit,
string  displaySymbol 
)

Set the specified display symbol for the given unit.

Parameters
unitUnit to set display symbol of. Non-null.
displaySymbolAssociated display symbol. Non-null.
Exceptions
ArgumentNullExceptionIf unitSymbol or displaySymbol is null.

The documentation for this class was generated from the following file: