org.apache.tapestry5.ioc.util
public class: TimeInterval [javadoc |
source]
java.lang.Object
org.apache.tapestry5.ioc.util.TimeInterval
Used to represent a period of time, specifically as a configuration value. This is often used to specify timeouts.
TimePeriods are parsed from strings.
The string specifys a number of terms. The values of all the terms are summed together to form the total time period.
Each term consists of a number followed by a unit. Units (from largest to smallest) are:
- y
- year
- d
- day
- h
- hour
- m
- minute
- s
- second
- ms
- millisecond
Example: "2 h 30 m". By
convention, terms are specified largest to smallest. A term without a unit is assumed to be milliseconds. Units are
case insensitive ("h" or "H" are treated the same).
| Constructor: |
public TimeInterval(String input) {
milliseconds = parseMilliseconds(input);
}
Creates a TimeInterval for a string. Parameters:
input - the string specifying the amount of time in the period
|
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.apache.tapestry5.ioc.util.TimeInterval Detail: |
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj instanceof TimeInterval)
{
TimeInterval tp = (TimeInterval) obj;
return milliseconds == tp.milliseconds;
}
return false;
}
|
public long milliseconds() {
return milliseconds;
}
|
static long parseMilliseconds(String input) {
long milliseconds = 0l;
Matcher matcher = PATTERN.matcher(input);
matcher.useAnchoringBounds(true);
// TODO: Notice non matching characters and reject input, including at end
int lastMatchEnd = -1;
while (matcher.find())
{
int start = matcher.start();
if (lastMatchEnd + 1 < start)
{
String invalid = input.substring(lastMatchEnd + 1, start);
throw new RuntimeException(UtilMessages.invalidTimeIntervalInput(invalid, input));
}
lastMatchEnd = matcher.end();
long count = Long.parseLong(matcher.group(1));
String units = matcher.group(2);
if (units.length() == 0)
{
milliseconds += count;
continue;
}
Long unitValue = UNITS.get(units);
if (unitValue == null)
throw new RuntimeException(UtilMessages.invalidTimeIntervalUnit(units, input, UNITS.keySet()));
milliseconds += count * unitValue;
}
if (lastMatchEnd + 1 < input.length())
{
String invalid = input.substring(lastMatchEnd + 1);
throw new RuntimeException(UtilMessages.invalidTimeIntervalInput(invalid, input));
}
return milliseconds;
}
|
public long seconds() {
return milliseconds / MILLISECOND;
}
|
public String toString() {
return String.format("TimeInterval[%d ms]", milliseconds);
}
|