KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > RelativeTime


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * This util class contains several methods and constants to manipulate relative time values.
17  * The relative time value has to be provided as either one integer value (representing the time in milliseconds),
18  * or as a set of time attribute integers (hours,minutes,seconds and milliseconds).
19  *
20  * @application SCAN or Tools (INFO, AnnotRel builder)
21  * @author David V van Zeventer
22  * @version $Id: RelativeTime.java,v 1.9 2005/10/05 10:44:00 michiel Exp $
23  */

24 public class RelativeTime {
25
26     //These time attribute constants define their position in the relative time format (h:m:s.ms)
27
private final static int HOUR_POS = 0;
28     private final static int MINUTE_POS = 1;
29     private final static int SECOND_POS = 2;
30     private final static int MILLI_POS = 3;
31
32     // logger
33
private static Logger log = Logging.getLoggerInstance(RelativeTime.class.getName());
34
35     /**
36      * Retrieves the amount of hours that are left in the timeValue variable (representing the time in milliseconds).
37      * @param timeValue An integer which holds the relative time value.
38      * @return The amount of hours left.
39      */

40     public static int getHours(int timeValue) {
41         return getTimeValue(timeValue,HOUR_POS);
42     }
43
44     /**
45      * Retrieves the amount of minutes that are left in the timeValue variable (representing the time in milliseconds).
46      * @param timeValue An integer which holds the relative time value.
47      * @return The amount of minutes left.
48      */

49     public static int getMinutes(int timeValue) {
50         return getTimeValue(timeValue,MINUTE_POS);
51     }
52
53     /**
54      * Retrieves the amount of seconds that are left in the timeValue variable (representing the time in milliseconds).
55      * @param timeValue An integer which holds the relative time value.
56      * @return The amount of seconds left.
57      */

58     public static int getSeconds(int timeValue) {
59         return getTimeValue(timeValue,SECOND_POS);
60     }
61
62     /**
63      * Retrieves the amount of milliseconds that are left in the timeValue variable (representing the time in milliseconds).
64      * @param timeValue An integer which holds the relative time value.
65      * @return The amount of milliseconds left.
66      */

67     public static int getMillis(int timeValue) {
68         return getTimeValue(timeValue,MILLI_POS);
69     }
70
71     /**
72      * Retrieves the amount of hours or minutes or seconds or milliseconds that are left in the timeValue variable
73      * (representing the time in milliseconds). Which time attribute is retrieved is determined by the timePos value.
74      * @param timeValue An integer which holds the relative time value.
75      * @param timePos An integer which holds the time attribute value requested.
76      * @return The amount of time attribute parts left OR -1 when an invalid timePosition is provided.
77      */

78     private static int getTimeValue(int timeValue,int timePos) {
79         int HOUR_IN_MILLIS = 60*60*1000;
80         int MINUTE_IN_MILLIS = 60*1000;
81         int SECOND_IN_MILLIS = 1000;
82         int timeLeft, hoursLeft, minutesLeft, secondsLeft, millisLeft;
83
84         if (timeValue >= 0) {
85             timeLeft = timeValue;
86             hoursLeft = timeLeft / HOUR_IN_MILLIS;
87             if (timePos == HOUR_POS) {
88                 return hoursLeft;
89             }
90
91             timeLeft -= hoursLeft * HOUR_IN_MILLIS;
92             minutesLeft = timeLeft / MINUTE_IN_MILLIS;
93             if (timePos == MINUTE_POS) {
94                 return minutesLeft;
95             }
96
97             timeLeft -= minutesLeft * MINUTE_IN_MILLIS;
98             secondsLeft = timeLeft / SECOND_IN_MILLIS;
99             if (timePos == SECOND_POS) {
100                 return secondsLeft;
101             }
102
103             timeLeft -= secondsLeft * SECOND_IN_MILLIS;
104             millisLeft = timeLeft;
105             if (timePos == MILLI_POS) {
106                 return millisLeft;
107             }
108
109             //Invalid timePosition used -> returning -1
110
log.warn("Invalid timePos used -> timePos="+timePos+" returning -1");
111             return -1;
112
113         } else { //Negative timeValue used -> returning -1
114
log.warn("Negative timeValue used at position "+timePos+" -> timeValue="+timeValue+" returning -1");
115             return -1;
116         }
117     }
118
119     /**
120      * Converts an integer (representing the time in milliseconds) to a string (like "12:42:15.020")
121      * @param timeValue The amount of time in milliseconds.
122      * @return String containing the amount of time in the "h:m:s.ms" format.
123      */

124     public static String JavaDoc convertIntToTime(int timeValue) {
125
126         int h = getHours(timeValue);
127         int m = getMinutes(timeValue);
128         int s = getSeconds(timeValue);
129         int ms = getMillis(timeValue);
130         if (!testTimeValue(h,HOUR_POS)) h = -1;
131         if (!testTimeValue(m,MINUTE_POS)) m = -1;
132         if (!testTimeValue(s,SECOND_POS)) s = -1;
133         if (!testTimeValue(ms,MILLI_POS)) ms= -1;
134
135         //Setting milliseconds attribute to right format value.
136
if (ms < 0) {
137             return (h+":"+m+":"+s+"."+ms);
138         } else if ((ms/100.0) >= 1) {
139             return (h+":"+m+":"+s+"."+ms);
140         } else if ((ms/10.0) >= 1) {
141             return (h+":"+m+":"+s+".0"+ms);
142         } else {
143             return (h+":"+m+":"+s+".00"+ms);
144         }
145     }
146
147     /**
148      * Converts the time attribute values to one integer representing the time in milliseconds.
149      * @param h The amount of hours
150      * @param m The amount of minutes
151      * @param s The amount of seconds
152      * @param ms The amount of millis
153      * @return The amount of time in milliseconds OR -1 if the timeValue contains negative values.
154      */

155     public static int convertTimeToInt(int h,int m,int s,int ms) {
156         //Setting milliseconds attribute to right format value.
157
if (ms < 0) {
158             return convertTimeToInt(h+":"+m+":"+s+"."+ms);
159         } else if ((ms/100.0) >= 1) {
160             return convertTimeToInt(h+":"+m+":"+s+"."+ms);
161         } else if ((ms/10.0) >= 1) {
162             return convertTimeToInt(h+":"+m+":"+s+".0"+ms);
163         } else {
164             return convertTimeToInt(h+":"+m+":"+s+".00"+ms);
165         }
166     }
167
168     /**
169      * Converts a string (like "12:42:15.020") to milliseconds
170      * @param time A string which contains the relative time in the format "hours:minutes:seconds.millis"
171      * @return The amount of time in milliseconds OR -1 if one of the time attributes provided is invalid.
172      */

173     public static int convertTimeToInt(String JavaDoc time) {
174         int result = 0; //The amount of milliseconds that is to be returned.
175
String JavaDoc attrStrValues[] = new String JavaDoc[4];
176
177         // Splice string value into time attribute tokens.
178
int timePos=0;
179         StringTokenizer st = new StringTokenizer(time, ":.");
180         while (st.hasMoreTokens()) {
181                attrStrValues[timePos] = new String JavaDoc(st.nextToken());
182             timePos++;
183         }
184
185         // Test all time attribute values.
186
for (int i=0; i<attrStrValues.length; i++) {
187             if (!testTimeValue(Integer.parseInt(attrStrValues[i]),i)) {
188                 return -1;
189             }
190         }
191
192         // Convert hours,minutes and seconds to millis and adding them to result.
193
int attrIntValue = Integer.parseInt(attrStrValues[HOUR_POS]);
194         result += attrIntValue*3600*1000;
195         attrIntValue = Integer.parseInt(attrStrValues[MINUTE_POS]);
196         result += attrIntValue*60*1000;
197         attrIntValue = Integer.parseInt(attrStrValues[SECOND_POS]);
198         result += attrIntValue*1000;
199
200         // Convert milliseconds attribute value and adding them to result.
201
attrIntValue = Integer.parseInt(attrStrValues[MILLI_POS]);
202         if (attrStrValues[MILLI_POS].length() == 3) {
203             result += attrIntValue;
204         } else if (attrStrValues[MILLI_POS].length() == 2) {
205             result += 10 * attrIntValue;
206         } else if (attrStrValues[MILLI_POS].length() == 1) {
207             result += 100 * attrIntValue;
208         }
209
210         return result;
211     }
212
213     /**
214      * Tests if a time attribute (h,m,s or ms) is valid or not.
215      * @param timeAttrValue This value holds the time attribute value.
216      * @param timePos Denotes the position time attribute value.
217      * @return true if value is valid, otherwise returns false.
218      */

219
220     private static boolean testTimeValue(int timeAttrValue, int timePos) {
221         // Test if value is negative.
222
if (timeAttrValue < 0) {
223             log.warn("Negative timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false");
224             return false;
225         }
226         if (timePos == HOUR_POS) {
227             if (timeAttrValue >23) {
228                 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false");
229                 return false;
230             }
231         } else if (timePos == MINUTE_POS) {
232             if (timeAttrValue >59) {
233                 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false");
234                 return false;
235             }
236         } else if (timePos == SECOND_POS) {
237             if (timeAttrValue >59) {
238                 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false");
239                 return false;
240             }
241         } else if (timePos == MILLI_POS) {
242             if (timeAttrValue >999) {
243                 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false");
244                 return false;
245             }
246         } else { //Invalid timePosition provided -> returning false
247
log.warn("Invalid timePos provided -> timePos="+timePos+" returning false");
248             return false;
249         }
250
251         return true;
252     }
253
254     /**
255      * Entry point for calling this class from commandline.
256      * For testing
257      */

258     public static void main(String JavaDoc args[]) {
259         //Use java org.mmbase.util.RelativeTime
260
Enumeration e;
261         String JavaDoc timeKey;
262         int timeValue;
263         Properties testProps = new Properties();
264         testProps.put("23:59:59.999", new Integer JavaDoc(86399999));
265         testProps.put("0:0:0.0" , new Integer JavaDoc(0));
266         testProps.put("23:0:0.0" , new Integer JavaDoc(82800000));
267         testProps.put("0:59:0.0" , new Integer JavaDoc(3540000));
268         testProps.put("0:0:59.0" , new Integer JavaDoc(59000));
269         testProps.put("0:0:0.999" , new Integer JavaDoc(999));
270         testProps.put("1:33:59.5" , new Integer JavaDoc(5639500));
271         testProps.put("1:33:59.52" , new Integer JavaDoc(5639520));
272         testProps.put("1:33:59.521" , new Integer JavaDoc(5639521));
273         testProps.put("1:33:59.012" , new Integer JavaDoc(5639012));
274         testProps.put("1:33:09.002" , new Integer JavaDoc(5589002));
275         testProps.put("24:33:09.002", new Integer JavaDoc(88389002));
276         testProps.put("0:0:2.100" , new Integer JavaDoc(2100));
277         testProps.put("0:0:2.010" , new Integer JavaDoc(2010));
278
279         log.info("----------------------------------------");
280         log.info("|Testing RelativeTime util class |");
281         log.info("----------------------------------------");
282
283         //log.info("Testing getHours(args[0]) = "+getHours(Integer.parseInt(args[0])));
284

285         if ((args.length <1) || (args.length >1)) {
286             log.info("Usage: RelativeTime methodName");
287             log.info("Methods available: getHours,getMinutes,getSeconds,getMillis,convertIntToTime,convertTimeToInt,convertTimeToInt2");
288         } else {
289             if (args[0].equals("getHours")) {
290                 log.info("Testing method: "+args[0]);
291                 e = testProps.keys();
292                 while (e.hasMoreElements()) {
293                     timeKey = (String JavaDoc)e.nextElement();
294                     timeValue = ((Integer JavaDoc)testProps.get(timeKey)).intValue();
295                     log.info("getHours using time = "+timeKey+" ->"+getHours(timeValue));
296                 }
297             } else if (args[0].equals("getMinutes")) {
298                 log.info("Testing method: "+args[0]);
299                 e = testProps.keys();
300                 while (e.hasMoreElements()) {
301                     timeKey = (String JavaDoc)e.nextElement();
302                     timeValue = ((Integer JavaDoc)testProps.get(timeKey)).intValue();
303                     log.info("getMinutes using time = "+timeKey+" ->"+getMinutes(timeValue));
304                 }
305             } else if (args[0].equals("getSeconds")) {
306                 log.info("Testing method: "+args[0]);
307                 e = testProps.keys();
308                 while (e.hasMoreElements()) {
309                     timeKey = (String JavaDoc)e.nextElement();
310                     timeValue = ((Integer JavaDoc)testProps.get(timeKey)).intValue();
311                     log.info("getSeconds using time = "+timeKey+" ->"+getSeconds(timeValue));
312                 }
313             } else if (args[0].equals("getMillis")) {
314                 log.info("Testing method: "+args[0]);
315                 e = testProps.keys();
316                 while (e.hasMoreElements()) {
317                     timeKey = (String JavaDoc)e.nextElement();
318                     timeValue = ((Integer JavaDoc)testProps.get(timeKey)).intValue();
319                     log.info("getMillis using time = "+timeKey+" ->"+getMillis(timeValue));
320                 }
321             } else if (args[0].equals("convertIntToTime")) {
322                 log.info("Testing method: "+args[0]);
323                 e = testProps.elements();
324                 while (e.hasMoreElements()) {
325                     timeValue = ((Integer JavaDoc)e.nextElement()).intValue();
326                     log.info("convertIntToTime using timeValue = "+timeValue+" ->"+convertIntToTime(timeValue));
327                 }
328             } else if (args[0].equals("convertTimeToInt")) {
329                 log.info("Testing method: "+args[0]);
330                 e = testProps.keys();
331                 while (e.hasMoreElements()) {
332                     timeKey = (String JavaDoc)e.nextElement();
333                     timeValue = convertTimeToInt(timeKey);
334                     log.info("convertTimeToInt using timeKey="+timeKey+" , timeValue="+timeValue+" in testProps? "+testProps.contains(new Integer JavaDoc(timeValue)));
335                 }
336             } else if (args[0].equals("convertTimeToInt2")) {
337                 log.info("Testing method: "+args[0]);
338                 timeValue = convertTimeToInt(4,33,9,32);
339                 log.info("convertTimeToInt2 using 4:33:9:32 , timeValue="+timeValue);
340             }
341         }
342     }
343 }
344
Popular Tags