KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > essiembre > library > scheduler > SchedulerUtil


1 /*
2  * Copyright (C) 2003, 2004 Pascal Essiembre, Essiembre Consultant Inc.
3  *
4  * This file is part of Essiembre Scheduler.
5  *
6  * Essiembre Scheduler is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Essiembre Scheduler is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Essiembre Scheduler; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307 USA
20  */

21 package com.essiembre.library.scheduler;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26
27 /**
28  * Utility methods used by this scheduler library.
29  * @author Pascal Essiembre
30  * @since 1.1
31  */

32 public final class SchedulerUtil {
33
34     /** Number of milliseconds in a second */
35     private final static long SECOND = 1000L;
36     /** Number of milliseconds in a minute */
37     private final static long MINUTE = 60 * SECOND;
38     /** Number of milliseconds in a hour */
39     private final static long HOUR = 60 * MINUTE;
40     /** Number of milliseconds in a day */
41     private final static long DAY = 24 * HOUR;
42     /** Map between time unit type and milliseconds */
43     private final static Map JavaDoc TIME_UNIT_MAP = new HashMap JavaDoc();
44     static {
45         TIME_UNIT_MAP.put(new Character JavaDoc('s'), new Long JavaDoc(SECOND));
46         TIME_UNIT_MAP.put(new Character JavaDoc('m'), new Long JavaDoc(MINUTE));
47         TIME_UNIT_MAP.put(new Character JavaDoc('h'), new Long JavaDoc(HOUR));
48         TIME_UNIT_MAP.put(new Character JavaDoc('d'), new Long JavaDoc(DAY));
49     }
50     
51     /**
52      * Constructor.
53      */

54     private SchedulerUtil() {
55         super();
56     }
57
58     /**
59      * Parses a <code>String</code> represntation of time period or delay,
60      * in milliseconds.
61      * If the time only contains digits, it is simply converted to a long.
62      * However, the value can be made of one or several numbers followed with
63      * an alpha character representing a time unit, all merged together. The
64      * supported time units are:
65      * <ul>
66      * <li><strong>d</strong>: a day
67      * <li><strong>h</strong>: an hour
68      * <li><strong>m</strong>: a minute
69      * <li><strong>s</strong>: a second
70      * </ul>
71      * No time unit for a number always means milliseconds. Some examples are:
72      * <blockquote>
73      * <strong>1d</strong>: 1 day (or 86,400,000 milliseconds)<br>
74      * <strong>2h30m</strong>: 2 hours and 30 minutes<br>
75      * <strong>30s500</strong>: 30 seconds and 500 milliseconds<br>
76      * </blockquote>
77      * @param time period or delay to parse
78      * @return a <code>long</code> representation of a delay or period
79      */

80     public static long parseTime(String JavaDoc time){
81
82         long longTime = 0L;
83         StringBuffer JavaDoc number = new StringBuffer JavaDoc("0");
84         for(int i = 0; i < time.length(); i++) {
85             char ch = time.charAt(i);
86             if (Character.isDigit(ch)) {
87                 number.append(ch);
88             } else {
89                 Character JavaDoc unitType = new Character JavaDoc(ch);
90                 if (TIME_UNIT_MAP.containsKey(unitType)) {
91                     longTime += Long.parseLong(number.toString())
92                              * ((Long JavaDoc) TIME_UNIT_MAP.get(unitType)).longValue();
93                     number = new StringBuffer JavaDoc("0");
94                 } else {
95                      throw new NumberFormatException JavaDoc(
96                             "\"" + ch + "\" is not a valid time unit type.");
97                 }
98             }
99         }
100         // Add remaining milliseconds, if any
101
longTime += Long.parseLong(number.toString());
102         return longTime;
103     }
104     
105 }
106
Popular Tags