Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

de.danet.an.util
Class Duration  view Duration download Duration.java

java.lang.Object
  extended byde.danet.an.util.Duration

public class Duration
extends java.lang.Object

This class provides a representation of durations and a parser for strings that represent durations.

Strings that can be parsed consist of value/unit pairs. Values can be arbitrary text, valid unit names are "years", "yrs", "months", "mts", "hours", "hrs", "min" and "sec". There may be only one pair for each unit i.e. "5 years 3 yrs" is illegal.

Value/unit pairs must be separated from each other with at least one whitespace character. Units may follow values immediately if the value is a number, else they must be separated by at least one whitespace.

Any duration unit may be omitted, but the descending order of units must be preserved.

Parsing is done in two phases. First, during parse the value/unit pairs are identified and the values are stored as strings. This allows the class even to be used when the values are expressions that must be evaluated. If this feature is used, the evaluation must be performed by retrieving the string values (using "...AsParsed"), evaluating the expression and setting the result.

Duration will try to convert the parsed string to a number only if a unit is accessed and has not been set explicitly. Thus, all accessor methods may throw a NumberFormatException even if parse has not thrown a ParseException.

Version:
$Revision: 1.8 $

Nested Class Summary
static interface Duration.ValueEvaluator
          This interface must be implemented by a class that wants to evaluate the value expression during duration parsing.
 
Field Summary
private  int days
           
private static int[] daysPerMonth
           
private  int hours
           
private static org.apache.commons.logging.Log logger
           
private  int minutes
           
private  int months
           
private static java.util.regex.Pattern parsePat
           
private  float seconds
           
private  int years
           
 
Constructor Summary
Duration()
          Creates an instance of Duration with a time span of 0.
 
Method Summary
 java.util.Date addTo(java.util.Date dateTime)
          Adds this duration to the given date and returns the result.
private  int fQuotient(int a, int b)
           
 int getDays()
          Get the value of days.
 int getHours()
          Get the value of hours.
 int getMinutes()
          Get the value of minutes.
 int getMonths()
          Get the value of months.
 float getSeconds()
          Get the value of seconds.
 int getYears()
          Get the value of years.
private  int maxDayInMonth(int year, int month)
           
static Duration parse(java.lang.String s)
          Parse the given string.
static Duration parse(java.lang.String s, Duration.ValueEvaluator e)
          Parse the given string.
 void setDays(int v)
          Set the value of days.
 void setHours(int v)
          Set the value of hours.
 void setMinutes(int v)
          Set the value of minutes.
 void setMonths(int v)
          Set the value of months.
 void setSeconds(float v)
          Set the value of seconds.
 void setYears(int v)
          Set the value of years.
 java.lang.String toString()
          Return a string representation for debugging purposes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

private static final org.apache.commons.logging.Log logger

parsePat

private static java.util.regex.Pattern parsePat

years

private int years

months

private int months

days

private int days

hours

private int hours

minutes

private int minutes

seconds

private float seconds

daysPerMonth

private static int[] daysPerMonth
Constructor Detail

Duration

public Duration()
Creates an instance of Duration with a time span of 0.

Method Detail

getYears

public int getYears()
Get the value of years.


setYears

public void setYears(int v)
Set the value of years.


getMonths

public int getMonths()
Get the value of months.


setMonths

public void setMonths(int v)
Set the value of months.


getDays

public int getDays()
Get the value of days.


setDays

public void setDays(int v)
Set the value of days.


getHours

public int getHours()
Get the value of hours.


setHours

public void setHours(int v)
Set the value of hours.


getMinutes

public int getMinutes()
Get the value of minutes.


setMinutes

public void setMinutes(int v)
Set the value of minutes.


getSeconds

public float getSeconds()
Get the value of seconds.


setSeconds

public void setSeconds(float v)
Set the value of seconds.


parse

public static Duration parse(java.lang.String s)
                      throws java.text.ParseException
Parse the given string.


parse

public static Duration parse(java.lang.String s,
                             Duration.ValueEvaluator e)
                      throws java.rmi.RemoteException,
                             java.text.ParseException
Parse the given string.


addTo

public java.util.Date addTo(java.util.Date dateTime)
Adds this duration to the given date and returns the result. This uses the algorithm described in the XSD datatypes specification.


fQuotient

private final int fQuotient(int a,
                            int b)

maxDayInMonth

private int maxDayInMonth(int year,
                          int month)

toString

public java.lang.String toString()
Return a string representation for debugging purposes.