001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.util.*;
013: import org.mmbase.util.logging.*;
014:
015: /**
016: * This util class contains several methods and constants to manipulate relative time values.
017: * The relative time value has to be provided as either one integer value (representing the time in milliseconds),
018: * or as a set of time attribute integers (hours,minutes,seconds and milliseconds).
019: *
020: * @application SCAN or Tools (INFO, AnnotRel builder)
021: * @author David V van Zeventer
022: * @version $Id: RelativeTime.java,v 1.11 2007/02/25 17:56:58 nklasens Exp $
023: */
024: public class RelativeTime {
025:
026: //These time attribute constants define their position in the relative time format (h:m:s.ms)
027: private final static int HOUR_POS = 0;
028: private final static int MINUTE_POS = 1;
029: private final static int SECOND_POS = 2;
030: private final static int MILLI_POS = 3;
031:
032: // logger
033: private static Logger log = Logging
034: .getLoggerInstance(RelativeTime.class.getName());
035:
036: /**
037: * Retrieves the amount of hours that are left in the timeValue variable (representing the time in milliseconds).
038: * @param timeValue An integer which holds the relative time value.
039: * @return The amount of hours left.
040: */
041: public static int getHours(int timeValue) {
042: return getTimeValue(timeValue, HOUR_POS);
043: }
044:
045: /**
046: * Retrieves the amount of minutes that are left in the timeValue variable (representing the time in milliseconds).
047: * @param timeValue An integer which holds the relative time value.
048: * @return The amount of minutes left.
049: */
050: public static int getMinutes(int timeValue) {
051: return getTimeValue(timeValue, MINUTE_POS);
052: }
053:
054: /**
055: * Retrieves the amount of seconds that are left in the timeValue variable (representing the time in milliseconds).
056: * @param timeValue An integer which holds the relative time value.
057: * @return The amount of seconds left.
058: */
059: public static int getSeconds(int timeValue) {
060: return getTimeValue(timeValue, SECOND_POS);
061: }
062:
063: /**
064: * Retrieves the amount of milliseconds that are left in the timeValue variable (representing the time in milliseconds).
065: * @param timeValue An integer which holds the relative time value.
066: * @return The amount of milliseconds left.
067: */
068: public static int getMillis(int timeValue) {
069: return getTimeValue(timeValue, MILLI_POS);
070: }
071:
072: /**
073: * Retrieves the amount of hours or minutes or seconds or milliseconds that are left in the timeValue variable
074: * (representing the time in milliseconds). Which time attribute is retrieved is determined by the timePos value.
075: * @param timeValue An integer which holds the relative time value.
076: * @param timePos An integer which holds the time attribute value requested.
077: * @return The amount of time attribute parts left OR -1 when an invalid timePosition is provided.
078: */
079: private static int getTimeValue(int timeValue, int timePos) {
080: int HOUR_IN_MILLIS = 60 * 60 * 1000;
081: int MINUTE_IN_MILLIS = 60 * 1000;
082: int SECOND_IN_MILLIS = 1000;
083: int timeLeft, hoursLeft, minutesLeft, secondsLeft, millisLeft;
084:
085: if (timeValue >= 0) {
086: timeLeft = timeValue;
087: hoursLeft = timeLeft / HOUR_IN_MILLIS;
088: if (timePos == HOUR_POS) {
089: return hoursLeft;
090: }
091:
092: timeLeft -= hoursLeft * HOUR_IN_MILLIS;
093: minutesLeft = timeLeft / MINUTE_IN_MILLIS;
094: if (timePos == MINUTE_POS) {
095: return minutesLeft;
096: }
097:
098: timeLeft -= minutesLeft * MINUTE_IN_MILLIS;
099: secondsLeft = timeLeft / SECOND_IN_MILLIS;
100: if (timePos == SECOND_POS) {
101: return secondsLeft;
102: }
103:
104: timeLeft -= secondsLeft * SECOND_IN_MILLIS;
105: millisLeft = timeLeft;
106: if (timePos == MILLI_POS) {
107: return millisLeft;
108: }
109:
110: //Invalid timePosition used -> returning -1
111: log.warn("Invalid timePos used -> timePos=" + timePos
112: + " returning -1");
113: return -1;
114:
115: } else { //Negative timeValue used -> returning -1
116: log.warn("Negative timeValue used at position " + timePos
117: + " -> timeValue=" + timeValue + " returning -1");
118: return -1;
119: }
120: }
121:
122: /**
123: * Converts an integer (representing the time in milliseconds) to a string (like "12:42:15.020")
124: * @param timeValue The amount of time in milliseconds.
125: * @return String containing the amount of time in the "h:m:s.ms" format.
126: */
127: public static String convertIntToTime(int timeValue) {
128:
129: int h = getHours(timeValue);
130: int m = getMinutes(timeValue);
131: int s = getSeconds(timeValue);
132: int ms = getMillis(timeValue);
133: if (!testTimeValue(h, HOUR_POS))
134: h = -1;
135: if (!testTimeValue(m, MINUTE_POS))
136: m = -1;
137: if (!testTimeValue(s, SECOND_POS))
138: s = -1;
139: if (!testTimeValue(ms, MILLI_POS))
140: ms = -1;
141:
142: //Setting milliseconds attribute to right format value.
143: if (ms < 0) {
144: return (h + ":" + m + ":" + s + "." + ms);
145: } else if ((ms / 100.0) >= 1) {
146: return (h + ":" + m + ":" + s + "." + ms);
147: } else if ((ms / 10.0) >= 1) {
148: return (h + ":" + m + ":" + s + ".0" + ms);
149: } else {
150: return (h + ":" + m + ":" + s + ".00" + ms);
151: }
152: }
153:
154: /**
155: * Converts the time attribute values to one integer representing the time in milliseconds.
156: * @param h The amount of hours
157: * @param m The amount of minutes
158: * @param s The amount of seconds
159: * @param ms The amount of millis
160: * @return The amount of time in milliseconds OR -1 if the timeValue contains negative values.
161: */
162: public static int convertTimeToInt(int h, int m, int s, int ms) {
163: //Setting milliseconds attribute to right format value.
164: if (ms < 0) {
165: return convertTimeToInt(h + ":" + m + ":" + s + "." + ms);
166: } else if ((ms / 100.0) >= 1) {
167: return convertTimeToInt(h + ":" + m + ":" + s + "." + ms);
168: } else if ((ms / 10.0) >= 1) {
169: return convertTimeToInt(h + ":" + m + ":" + s + ".0" + ms);
170: } else {
171: return convertTimeToInt(h + ":" + m + ":" + s + ".00" + ms);
172: }
173: }
174:
175: /**
176: * Converts a string (like "12:42:15.020") to milliseconds
177: * @param time A string which contains the relative time in the format "hours:minutes:seconds.millis"
178: * @return The amount of time in milliseconds OR -1 if one of the time attributes provided is invalid.
179: */
180: public static int convertTimeToInt(String time) {
181: int result = 0; //The amount of milliseconds that is to be returned.
182: String attrStrValues[] = new String[4];
183:
184: // Splice string value into time attribute tokens.
185: int timePos = 0;
186: StringTokenizer st = new StringTokenizer(time, ":.");
187: while (st.hasMoreTokens()) {
188: attrStrValues[timePos] = new String(st.nextToken());
189: timePos++;
190: }
191:
192: // Test all time attribute values.
193: for (int i = 0; i < attrStrValues.length; i++) {
194: if (!testTimeValue(Integer.parseInt(attrStrValues[i]), i)) {
195: return -1;
196: }
197: }
198:
199: // Convert hours,minutes and seconds to millis and adding them to result.
200: int attrIntValue = Integer.parseInt(attrStrValues[HOUR_POS]);
201: result += attrIntValue * 3600 * 1000;
202: attrIntValue = Integer.parseInt(attrStrValues[MINUTE_POS]);
203: result += attrIntValue * 60 * 1000;
204: attrIntValue = Integer.parseInt(attrStrValues[SECOND_POS]);
205: result += attrIntValue * 1000;
206:
207: // Convert milliseconds attribute value and adding them to result.
208: attrIntValue = Integer.parseInt(attrStrValues[MILLI_POS]);
209: if (attrStrValues[MILLI_POS].length() == 3) {
210: result += attrIntValue;
211: } else if (attrStrValues[MILLI_POS].length() == 2) {
212: result += 10 * attrIntValue;
213: } else if (attrStrValues[MILLI_POS].length() == 1) {
214: result += 100 * attrIntValue;
215: }
216:
217: return result;
218: }
219:
220: /**
221: * Tests if a time attribute (h,m,s or ms) is valid or not.
222: * @param timeAttrValue This value holds the time attribute value.
223: * @param timePos Denotes the position time attribute value.
224: * @return true if value is valid, otherwise returns false.
225: */
226:
227: private static boolean testTimeValue(int timeAttrValue, int timePos) {
228: // Test if value is negative.
229: if (timeAttrValue < 0) {
230: log.warn("Negative timeAttrValue used at position "
231: + timePos + " -> timeAttrValue=" + timeAttrValue
232: + " returning false");
233: return false;
234: }
235: if (timePos == HOUR_POS) {
236: if (timeAttrValue > 23) {
237: log.warn("Invalid timeAttrValue used at position "
238: + timePos + " -> timeAttrValue="
239: + timeAttrValue + " returning false");
240: return false;
241: }
242: } else if (timePos == MINUTE_POS) {
243: if (timeAttrValue > 59) {
244: log.warn("Invalid timeAttrValue used at position "
245: + timePos + " -> timeAttrValue="
246: + timeAttrValue + " returning false");
247: return false;
248: }
249: } else if (timePos == SECOND_POS) {
250: if (timeAttrValue > 59) {
251: log.warn("Invalid timeAttrValue used at position "
252: + timePos + " -> timeAttrValue="
253: + timeAttrValue + " returning false");
254: return false;
255: }
256: } else if (timePos == MILLI_POS) {
257: if (timeAttrValue > 999) {
258: log.warn("Invalid timeAttrValue used at position "
259: + timePos + " -> timeAttrValue="
260: + timeAttrValue + " returning false");
261: return false;
262: }
263: } else { //Invalid timePosition provided -> returning false
264: log.warn("Invalid timePos provided -> timePos=" + timePos
265: + " returning false");
266: return false;
267: }
268:
269: return true;
270: }
271:
272: /**
273: * Entry point for calling this class from commandline.
274: * For testing
275: */
276: public static void main(String args[]) {
277: //Use java org.mmbase.util.RelativeTime
278: Enumeration<?> e;
279: String timeKey;
280: int timeValue;
281: Properties testProps = new Properties();
282: testProps.put("23:59:59.999", 86399999);
283: testProps.put("0:0:0.0", 0);
284: testProps.put("23:0:0.0", 82800000);
285: testProps.put("0:59:0.0", 3540000);
286: testProps.put("0:0:59.0", 59000);
287: testProps.put("0:0:0.999", 999);
288: testProps.put("1:33:59.5", 5639500);
289: testProps.put("1:33:59.52", 5639520);
290: testProps.put("1:33:59.521", 5639521);
291: testProps.put("1:33:59.012", 5639012);
292: testProps.put("1:33:09.002", 5589002);
293: testProps.put("24:33:09.002", 88389002);
294: testProps.put("0:0:2.100", 2100);
295: testProps.put("0:0:2.010", 2010);
296:
297: log.info("----------------------------------------");
298: log.info("|Testing RelativeTime util class |");
299: log.info("----------------------------------------");
300:
301: //log.info("Testing getHours(args[0]) = "+getHours(Integer.parseInt(args[0])));
302:
303: if ((args.length < 1) || (args.length > 1)) {
304: log.info("Usage: RelativeTime methodName");
305: log
306: .info("Methods available: getHours,getMinutes,getSeconds,getMillis,convertIntToTime,convertTimeToInt,convertTimeToInt2");
307: } else {
308: if (args[0].equals("getHours")) {
309: log.info("Testing method: " + args[0]);
310: e = testProps.keys();
311: while (e.hasMoreElements()) {
312: timeKey = (String) e.nextElement();
313: timeValue = ((Integer) testProps.get(timeKey))
314: .intValue();
315: log.info("getHours using time = " + timeKey + " ->"
316: + getHours(timeValue));
317: }
318: } else if (args[0].equals("getMinutes")) {
319: log.info("Testing method: " + args[0]);
320: e = testProps.keys();
321: while (e.hasMoreElements()) {
322: timeKey = (String) e.nextElement();
323: timeValue = ((Integer) testProps.get(timeKey))
324: .intValue();
325: log.info("getMinutes using time = " + timeKey
326: + " ->" + getMinutes(timeValue));
327: }
328: } else if (args[0].equals("getSeconds")) {
329: log.info("Testing method: " + args[0]);
330: e = testProps.keys();
331: while (e.hasMoreElements()) {
332: timeKey = (String) e.nextElement();
333: timeValue = ((Integer) testProps.get(timeKey))
334: .intValue();
335: log.info("getSeconds using time = " + timeKey
336: + " ->" + getSeconds(timeValue));
337: }
338: } else if (args[0].equals("getMillis")) {
339: log.info("Testing method: " + args[0]);
340: e = testProps.keys();
341: while (e.hasMoreElements()) {
342: timeKey = (String) e.nextElement();
343: timeValue = ((Integer) testProps.get(timeKey))
344: .intValue();
345: log.info("getMillis using time = " + timeKey
346: + " ->" + getMillis(timeValue));
347: }
348: } else if (args[0].equals("convertIntToTime")) {
349: log.info("Testing method: " + args[0]);
350: e = testProps.elements();
351: while (e.hasMoreElements()) {
352: timeValue = ((Integer) e.nextElement()).intValue();
353: log.info("convertIntToTime using timeValue = "
354: + timeValue + " ->"
355: + convertIntToTime(timeValue));
356: }
357: } else if (args[0].equals("convertTimeToInt")) {
358: log.info("Testing method: " + args[0]);
359: e = testProps.keys();
360: while (e.hasMoreElements()) {
361: timeKey = (String) e.nextElement();
362: timeValue = convertTimeToInt(timeKey);
363: log.info("convertTimeToInt using timeKey="
364: + timeKey + " , timeValue=" + timeValue
365: + " in testProps? "
366: + testProps.contains(timeValue));
367: }
368: } else if (args[0].equals("convertTimeToInt2")) {
369: log.info("Testing method: " + args[0]);
370: timeValue = convertTimeToInt(4, 33, 9, 32);
371: log
372: .info("convertTimeToInt2 using 4:33:9:32 , timeValue="
373: + timeValue);
374: }
375: }
376: }
377: }
|