001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.util;
010:
011: import com.completex.objective.components.OdalRuntimeException;
012:
013: import java.util.regex.Pattern;
014: import java.util.regex.Matcher;
015:
016: /**
017: * @author Gennady Krizhevsky
018: */
019: public class TimeInterval {
020:
021: public static final String TIME_PATTERN = "\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d";
022:
023: private String interval;
024: private String variableName;
025:
026: private long hour;
027: private long min;
028: private long sec;
029: private long ms;
030:
031: private long intervalMs;
032:
033: public TimeInterval(String interval, String variableName) {
034: this .interval = interval;
035: this .variableName = variableName;
036: parseInterval();
037: }
038:
039: protected void parseInterval() {
040:
041: validateTimeInterval();
042:
043: String[] mainTokens = interval.split(":");
044:
045: String[] secondaryTokens = mainTokens[2].split("\\.");
046:
047: String hourParam = mainTokens[0];
048: String minParam = mainTokens[1];
049: String secParam = secondaryTokens[0];
050: String msParam = secondaryTokens[1];
051:
052: hour = Long.parseLong(hourParam);
053: min = Long.parseLong(minParam);
054: sec = Long.parseLong(secParam);
055: ms = Long.parseLong(msParam);
056:
057: validateTimeParts(min, sec, ms);
058:
059: intervalMs = toMs(hour, min, sec, ms);
060: }
061:
062: public static long toMs(long hour, long min, long sec, long ms) {
063: return ((60 * hour + min) * 60 + sec) * 60 * 1000 + ms;
064: }
065:
066: protected void validateTimeParts(long min, long sec, long ms) {
067: validateMin(min);
068: validateSec(sec);
069: validateMs(ms);
070: }
071:
072: protected void validateMs(long ms) {
073: if (ms > 999) {
074: throw new OdalRuntimeException("Number of milliseconds in "
075: + variableName + " is greater than 999");
076: }
077: }
078:
079: protected void validateSec(long sec) {
080: if (sec > 59) {
081: throw new OdalRuntimeException("Number of seconds in "
082: + variableName + " is greater than 59");
083: }
084: }
085:
086: protected void validateMin(long min) {
087: if (min > 59) {
088: throw new OdalRuntimeException("Number of minutes in "
089: + variableName + " is greater than 59");
090: }
091: }
092:
093: protected void validateTimeInterval() {
094: if (StringUtil.isEmpty(interval)) {
095: throw new OdalRuntimeException(variableName + " is empty");
096: }
097:
098: Pattern p = Pattern.compile(TIME_PATTERN);
099: Matcher m = p.matcher(interval);
100:
101: if (!m.matches()) {
102: throw new OdalRuntimeException(variableName + " "
103: + interval + " not in HH:mm:ss.SSS format ");
104: }
105: }
106:
107: public long getHour() {
108: return hour;
109: }
110:
111: public long getMin() {
112: return min;
113: }
114:
115: public long getSec() {
116: return sec;
117: }
118:
119: public long getMs() {
120: return ms;
121: }
122:
123: public String getInterval() {
124: return interval;
125: }
126:
127: public long getIntervalMs() {
128: return intervalMs;
129: }
130: }
|