2.17.2: 2011-06-17

net.sf.basedb.util.units
Class UnitUtil

java.lang.Object
  extended by net.sf.basedb.util.units.UnitUtil

public class UnitUtil
extends Object

A utility class for working with Unit:s.

Version:
2.9
Author:
Nicklas
Last modified
$Date: 2008-12-18 09:22:27 +0100 (Thu, 18 Dec 2008) $

Field Summary
static Pattern VALUE_WITH_UNIT
          Pattern for checking if a string is a numeric value with an optional unit.
 
Constructor Summary
UnitUtil()
           
 
Method Summary
static Unit getUnit(DbControl dc, String quantityId, String symbol)
          Find the unit with a given symbol for a given quantity.
static String[] splitValueWithUnit(String value)
          Splits a value with an optional unit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALUE_WITH_UNIT

public static final Pattern VALUE_WITH_UNIT
Pattern for checking if a string is a numeric value with an optional unit. Used by splitValueWithUnit(String)

Constructor Detail

UnitUtil

public UnitUtil()
Method Detail

getUnit

public static Unit getUnit(DbControl dc,
                           String quantityId,
                           String symbol)
Find the unit with a given symbol for a given quantity. Use this method only when you need to find few units. Each invokation results in a new database query. If you need to lookup mulitple units, consider using a UnitCache instead.

Parameters:
dc - The DbControl to use for database access
quantityId - The system ID of the quantity
symbol - The unit symbol
Returns:
A unit object, or null if no unit is found

splitValueWithUnit

public static String[] splitValueWithUnit(String value)
Splits a value with an optional unit. The input string should start with a numerical value and optionally end with a unit. If the value is not numeric, null is returned, otherwise an array with two elements is returned. The 0th element is the numeric part, and the 1st is the unit part. The unit part is null if the value doesn't have a unit. White-space has been stripped from all values.

Parameters:
value - The value to split
Returns:
An array of length 2, or null

2.17.2: 2011-06-17