org.pfsw.text
Class TimeValue

java.lang.Object
  extended by org.pfsw.text.TimeValue
All Implemented Interfaces:
java.lang.Comparable<TimeValue>

public class TimeValue
extends java.lang.Object
implements java.lang.Comparable<TimeValue>

Represents a time value and provides parsing of strings and various different getters for various time units.


Constructor Summary
TimeValue(long millisecondsValue)
          Initialize the new instance with a milliseconds value.
TimeValue(long value, TimeUnit unit)
          Initialize the new instance with a value and unit.
TimeValue(NaturalNumber value, TimeUnit unit)
          Initialize the new instance with a value and unit.
TimeValue(java.lang.String strValue)
          Initialize the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
TimeValue(java.lang.String strValue, TimeUnit defaultUnit)
          Initialize the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
 
Method Summary
 long asHours()
          Returns the underlying time in hours.
 long asMilliseconds()
           
 long asMinutes()
           
 long asSeconds()
           
 int compareTo(TimeValue other)
           
 long convertTo(TimeUnit unit)
          Returns the value converted to the specified unit.
static TimeValue create(long millisecondsValue)
          Creates a new instance with the given milliseconds value.
static TimeValue create(long value, TimeUnit unit)
          Creates a new instance with the given value and time unit.
static TimeValue create(NaturalNumber value, TimeUnit unit)
          Creates a new instance with the given value and time unit.
static TimeValue create(java.lang.String strValue)
          Creates a the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
static TimeValue create(java.lang.String strValue, TimeUnit defaultUnit)
          Creates a new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
 boolean equals(java.lang.Object obj)
           
protected  long getMilliseconds()
           
 int hashCode()
           
protected  long parseToMilliseconds(java.lang.String strValue, TimeUnit defaultUnit)
          Parse the given string, taking any optional unit string at the end into account.
protected  void setMilliseconds(long milliseconds)
           
protected  StringPair splitDigitsAndUnit(java.lang.String str)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimeValue

public TimeValue(long millisecondsValue)
Initialize the new instance with a milliseconds value.


TimeValue

public TimeValue(long value,
                 TimeUnit unit)
Initialize the new instance with a value and unit.


TimeValue

public TimeValue(NaturalNumber value,
                 TimeUnit unit)
Initialize the new instance with a value and unit.


TimeValue

public TimeValue(java.lang.String strValue,
                 TimeUnit defaultUnit)
Initialize the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
The given string will be parsed, taking any optional unit string at the end into account. Allowed unit strings are the short names defined in TimeUnit (i.e. "h", "m", "s", "ms").

Parameters:
strValue - The string to parse (must not be null).
defaultUnit - The unit to use if the string does not contain an explicit short name (must not be null).
Throws:
java.lang.NumberFormatException - If the string does not contain a valid long value.

TimeValue

public TimeValue(java.lang.String strValue)
Initialize the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
The default unit is "ms" if not explicitly set in the string.

Throws:
java.lang.NumberFormatException - If the string does not contain a valid long value.
Method Detail

create

public static TimeValue create(long millisecondsValue)
Creates a new instance with the given milliseconds value.


create

public static TimeValue create(long value,
                               TimeUnit unit)
Creates a new instance with the given value and time unit.


create

public static TimeValue create(NaturalNumber value,
                               TimeUnit unit)
Creates a new instance with the given value and time unit.


create

public static TimeValue create(java.lang.String strValue,
                               TimeUnit defaultUnit)
Creates a new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
The given string will be parsed, taking any optional unit string at the end into account. Allowed unit strings are the short names defined in TimeUnit (i.e. "h", "m", "s", "ms").

Parameters:
strValue - The string to parse (must not be null).
defaultUnit - The unit to use if the string does not contain an explicit short name (must not be null).
Throws:
java.lang.NumberFormatException - If the string does not contain a valid long value.

create

public static TimeValue create(java.lang.String strValue)
Creates a the new instance with a string that must contain digits, optionally followed by the short name of a unit (@see TimeUnit.
The default unit is "ms" if not explicitly set in the string.

Throws:
java.lang.NumberFormatException - If the string does not contain a valid long value.

asMilliseconds

public long asMilliseconds()

asSeconds

public long asSeconds()

asMinutes

public long asMinutes()

asHours

public long asHours()
Returns the underlying time in hours.


convertTo

public long convertTo(TimeUnit unit)
Returns the value converted to the specified unit.

Parameters:
unit - The unit of the result value (must not be null).

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(TimeValue other)
Specified by:
compareTo in interface java.lang.Comparable<TimeValue>

parseToMilliseconds

protected long parseToMilliseconds(java.lang.String strValue,
                                   TimeUnit defaultUnit)
Parse the given string, taking any optional unit string at the end into account. Allowed unit strings are the short names defined in TimeUnit (i.e. "h", "m", "s", "ms").

Parameters:
strValue - The string to parse
defaultUnit - The unit to use if the string does not contain an explicit short name.
Returns:
The parsed value as milliseconds
Throws:
java.lang.NumberFormatException - If the string does not contain a valid long value or an invalid unit.

splitDigitsAndUnit

protected StringPair splitDigitsAndUnit(java.lang.String str)

getMilliseconds

protected long getMilliseconds()

setMilliseconds

protected void setMilliseconds(long milliseconds)