KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > Period


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.config.types;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.util.L10N;
34 import com.caucho.util.QDate;
35
36 /**
37  * Representations of time in milliseconds.
38  */

39 public class Period {
40   static L10N L = new L10N(Period.class);
41
42   public static final long SECOND = 1000L;
43   public static final long MINUTE = 60L * SECOND;
44   public static final long HOUR = 60L * MINUTE;
45   public static final long DAY = 24L * HOUR;
46   /** 30 days */
47   public static final long MONTH = DAY * 30L;
48   /** 365 days */
49   public static final long YEAR = DAY * 365L;
50   public static final long INFINITE = (Long.MAX_VALUE / 2 / 1000) * 1000;
51   public static final long FOREVER = INFINITE;
52
53   private static final QDate _localCalendar = QDate.createLocal();
54   
55   private long _period;
56
57   public Period()
58   {
59   }
60
61   public Period(long period)
62   {
63     _period = period;
64   }
65
66   /**
67    * Returns the default units (default is 1000)
68    */

69   public long getDefaultUnits()
70   {
71     return 1000;
72   }
73   
74   /**
75    * Sets the text.
76    */

77   public void addText(String JavaDoc text)
78     throws ConfigException
79   {
80     _period = toPeriod(text, getDefaultUnits());
81   }
82
83   /**
84    * Replace with the real path.
85    */

86   public long getPeriod()
87   {
88     return _period;
89   }
90
91   /**
92    * Converts a period string to a time.
93    *
94    * <table>
95    * <tr><td>ms<td>milliseconds
96    * <tr><td>s<td>seconds
97    * <tr><td>m<td>minutes
98    * <tr><td>h<td>hours
99    * <tr><td>D<td>days
100    * <tr><td>W<td>weeks
101    * <tr><td>M<td>months
102    * <tr><td>Y<td>years
103    * </table>
104    */

105   public static long toPeriod(String JavaDoc value)
106     throws ConfigException
107   {
108     return toPeriod(value, 1000);
109   }
110
111   /**
112    * Converts a period string to a time.
113    *
114    * <table>
115    * <tr><td>ms<td>milliseconds
116    * <tr><td>s<td>seconds
117    * <tr><td>m<td>minutes
118    * <tr><td>h<td>hours
119    * <tr><td>D<td>days
120    * <tr><td>W<td>weeks
121    * <tr><td>M<td>months
122    * <tr><td>Y<td>years
123    * </table>
124    */

125   public static long toPeriod(String JavaDoc value, long defaultUnits)
126     throws ConfigException
127   {
128     if (value == null)
129       return 0;
130     
131     long sign = 1;
132     long period = 0;
133
134     int i = 0;
135     int length = value.length();
136     
137     if (length > 0 && value.charAt(i) == '-') {
138       sign = -1;
139       i++;
140     }
141
142     while (i < length) {
143       long delta = 0;
144       char ch;
145
146       for (; i < length && (ch = value.charAt(i)) >= '0' && ch <= '9'; i++)
147     delta = 10 * delta + ch - '0';
148
149       if (length <= i)
150     period += defaultUnits * delta;
151       else {
152         ch = value.charAt(i++);
153     switch (ch) {
154     case 's':
155       period += 1000 * delta;
156       break;
157
158     case 'm':
159           if (i < value.length() && value.charAt(i) == 's') {
160             i++;
161             period += delta;
162           }
163           else
164             period += 60 * 1000 * delta;
165       break;
166
167     case 'h':
168       period += 60L * 60 * 1000 * delta;
169       break;
170
171     case 'D':
172       period += DAY * delta;
173       break;
174
175         case 'W':
176       period += 7L * DAY * delta;
177       break;
178
179     case 'M':
180       period += 30L * DAY * delta;
181       break;
182
183     case 'Y':
184       period += 365L * DAY * delta;
185       break;
186
187         default:
188           throw new ConfigException(L.l("Unknown unit `{0}' in period `{1}'. Valid units are:\n '10ms' milliseconds\n '10s' seconds\n '10m' minutes\n '10h' hours\n '10D' days\n '10W' weeks\n '10M' months\n '10Y' years",
189                                         String.valueOf(ch), value));
190     }
191       }
192     }
193
194     period = sign * period;
195
196     // server/137w
197
/*
198     if (period < 0)
199       return INFINITE;
200     else
201       return period;
202     */

203     
204     return period;
205   }
206
207   /**
208    * Calculates the next period end. The calculation is in local time.
209    *
210    * @param now the current time in GMT ms since the epoch
211    *
212    * @return the time of the next period in GMT ms since the epoch
213    */

214   public static long periodEnd(long now, long period)
215   {
216     return periodEnd(now, period, _localCalendar);
217   }
218   
219   /**
220    * Calculates the next period end. The calculation is in local time.
221    *
222    * @param now the current time in GMT ms since the epoch
223    *
224    * @return the time of the next period in GMT ms since the epoch
225    */

226   public static long periodEnd(long now, long period, QDate cal)
227   {
228     if (period < 0)
229       return Long.MAX_VALUE;
230     else if (period == 0)
231       return now;
232
233     if (period < 30 * DAY) {
234       synchronized (cal) {
235         cal.setGMTTime(now);
236
237         long localTime = cal.getLocalTime();
238
239         localTime = localTime + (period - (localTime + 4 * DAY) % period);
240
241         cal.setLocalTime(localTime);
242
243         return cal.getGMTTime();
244       }
245     }
246
247     if (period % (30 * DAY) == 0) {
248       int months = (int) (period / (30 * DAY));
249
250       synchronized (cal) {
251         cal.setGMTTime(now);
252         long year = cal.getYear();
253         int month = cal.getMonth();
254
255         cal.setLocalTime(0);
256       
257         cal.setDate(year, month + months, 1);
258
259         return cal.getGMTTime();
260       }
261     }
262
263     if (period % (365 * DAY) == 0) {
264       long years = (period / (365 * DAY));
265
266       synchronized (cal) {
267         cal.setGMTTime(now);
268         long year = cal.getYear();
269
270         cal.setLocalTime(0);
271
272         long newYear = year + (years - year % years);
273       
274         cal.setDate(newYear, 0, 1);
275
276         return cal.getGMTTime();
277       }
278     }
279
280     synchronized (cal) {
281       cal.setGMTTime(now);
282
283       long localTime = cal.getLocalTime();
284
285       localTime = localTime + (period - (localTime + 4 * DAY) % period);
286
287       cal.setLocalTime(localTime);
288
289       return cal.getGMTTime();
290     }
291   }
292
293   public String JavaDoc toString()
294   {
295     return "Period[" + _period + "]";
296   }
297 }
298
Popular Tags