1
2
3
4
5 package org.rcfaces.core.internal.util;
6
7 import java.text.DecimalFormat;
8 import java.text.NumberFormat;
9 import java.text.ParseException;
10 import java.util.Locale;
11 import java.util.StringTokenizer;
12
13
14
15
16
17
18
19 public class Delay {
20 private static final String REVISION = "$Revision: 1.18 $";
21
22
23 public static final long EXPIRE_TIME_DELETE = -1;
24
25
26
27
28
29 public static final long NONE = 0;
30
31
32 public static final long SECOND = 1000l;
33
34
35 public static final long MINUTE = 60 * SECOND;
36
37
38 public static final long HOUR = 60 * MINUTE;
39
40
41 public static final long DAY = 24 * HOUR;
42
43
44 public static final long WEEK = 7 * DAY;
45
46
47
48
49
50 public static final long MONTH = 30 * DAY;
51
52
53
54
55
56 public static final long YEAR = 365 * DAY;
57
58 private static final NumberFormat delayNumberFormat;
59
60
61
62
63
64
65 public static long parseDelay(String value) throws ParseException {
66 long total = 0;
67 int current = 0;
68 boolean cv = false;
69
70 value = value.trim();
71
72 if (value.equalsIgnoreCase("none")) {
73 return (int) Delay.NONE;
74 }
75
76 StringTokenizer st = new StringTokenizer(value, " wdhmsWDHMS", true);
77
78 for (; st.hasMoreTokens();) {
79 String token = st.nextToken();
80
81 if (Character.isDigit(token.charAt(0))) {
82 if (cv == true) {
83 throw new ParseException(
84 "Two value in same expression. expression='"
85 + value + "' token='" + token + "'", 0);
86 }
87 cv = true;
88 current = Integer.parseInt(token);
89
90 continue;
91 }
92
93 if (token.trim().length() < 1) {
94 continue;
95 }
96
97 if (token.equalsIgnoreCase("y")) {
98 if (cv == false) {
99 throw new ParseException(
100 "Specified 'y' token, but no value defined ! expression='"
101 + value + "' token='" + token + "'", 0);
102 }
103 cv = false;
104 total += current * Delay.YEAR;
105 continue;
106 }
107
108 if (token.equalsIgnoreCase("w")) {
109 if (cv == false) {
110 throw new ParseException(
111 "Specified 'w' token, but no value defined ! expression='"
112 + value + "' token='" + token + "'", 0);
113 }
114 cv = false;
115 total += current * Delay.WEEK;
116 continue;
117 }
118
119 if (token.equalsIgnoreCase("d")) {
120 if (cv == false) {
121 throw new ParseException(
122 "Specified 'd' token, but no value defined ! expression='"
123 + value + "' token='" + token + "'", 0);
124 }
125 total += current * Delay.DAY;
126 cv = false;
127 continue;
128 }
129
130 if (token.equalsIgnoreCase("h")) {
131 if (cv == false) {
132 throw new ParseException(
133 "Specified 'h' token, but no value defined ! expression='"
134 + value + "' token='" + token + "'", 0);
135 }
136 total += current * Delay.HOUR;
137 cv = false;
138 continue;
139 }
140
141 if (token.equalsIgnoreCase("m")) {
142 if (cv == false) {
143 throw new ParseException(
144 "Specified 'm' token, but no value defined ! expression='"
145 + value + "' token='" + token + "'", 0);
146 }
147 total += current * Delay.MINUTE;
148 cv = false;
149 continue;
150 }
151
152 if (token.equalsIgnoreCase("s")) {
153 if (cv == false) {
154 throw new ParseException(
155 "Specified 's' token, but no value defined ! expression='"
156 + value + "' token='" + token + "'", 0);
157 }
158 total += current * Delay.SECOND;
159 cv = false;
160 continue;
161 }
162
163 throw new ParseException("Specified unknown token ! expression='"
164 + value + "' token='" + token + "'", 0);
165 }
166
167 if (cv == true) {
168 total += current;
169 }
170
171 return total;
172 }
173
174 public static final String format(long delay) {
175
176 double d;
177 String unit;
178 if (delay >= Delay.YEAR) {
179 d = delay / (double) Delay.YEAR;
180 unit = "year";
181
182 } else if (delay >= Delay.MONTH) {
183 d = delay / (double) Delay.MONTH;
184 unit = "month";
185
186 } else if (delay >= Delay.WEEK) {
187 d = delay / (double) Delay.WEEK;
188 unit = "week";
189
190 } else if (delay >= Delay.HOUR * 24) {
191 d = delay / (double) Delay.DAY;
192 unit = "day";
193
194 } else if (delay >= Delay.MINUTE * 60) {
195 d = delay / (double) Delay.HOUR;
196 unit = "hour";
197
198 } else if (delay >= Delay.SECOND * 60) {
199 d = delay / (double) Delay.MINUTE;
200 unit = "minute";
201
202 } else if (delay >= Delay.SECOND) {
203 d = delay / (double) Delay.SECOND;
204 unit = "minute";
205
206 } else if (delay > 0) {
207 d = delay;
208 unit = "millisecond";
209
210 } else {
211 d = delay;
212 unit = "never";
213 }
214
215 long floor = (int) Math.floor(d);
216 if (floor != d) {
217 return delayNumberFormat.format(d) + " " + unit
218 + ((d > 1.0) ? "s" : "");
219 }
220 return floor + " " + unit + ((d > 1.0) ? "s" : "");
221 }
222
223 static {
224 delayNumberFormat = NumberFormat.getInstance(Locale.ENGLISH);
225 if (delayNumberFormat instanceof DecimalFormat) {
226 ((DecimalFormat) delayNumberFormat).applyPattern("###0.00");
227 }
228
229 }
230 }