1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package org.exolab.castor.types;
38
39 import org.exolab.castor.types.TimeDuration;
40 import org.exolab.castor.xml.ValidationException;
41
42 import java.text.ParseException;
43
44 import javax.naming.OperationNotSupportedException;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public abstract class RecurringDurationBase implements java.io.Serializable {
64
65
66 private TimeDuration _period = null;
67
68 private TimeDuration _duration = null;
69
70
71
72
73 private short _hour = 0;
74
75
76
77
78 private short _minute = 0;
79
80
81
82
83 private short _second = 0;
84
85 private short _millsecond = 0;
86
87 private short _zoneHour = 0;
88
89 private short _zoneMinute = 0;
90
91 private boolean _utc = false;
92
93 private boolean _zoneNegative = false;
94
95 private boolean _isNegative = false;
96
97
98
99
100 protected RecurringDurationBase() {
101
102 }
103
104
105
106
107
108
109
110 protected RecurringDurationBase(TimeDuration duration, TimeDuration period) {
111 try {
112 this.setDuration(duration);
113 this.setPeriodInternal(period);
114 } catch (UnsupportedOperationException e) {
115 String err = "Recurring Duration: " + e;
116 throw new IllegalArgumentException(err);
117 }
118 }
119
120
121
122
123
124
125
126
127
128 protected RecurringDurationBase(String duration, String period) throws IllegalArgumentException {
129 try {
130 this.setDuration(TimeDuration.parseTimeDuration(duration));
131 this.setPeriodInternal(TimeDuration.parseTimeDuration(period));
132 } catch (Exception e) {
133 String err = "In RecurringDurationBase: " + e;
134 throw new IllegalArgumentException(err);
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148 protected RecurringDurationBase(String duration, String period, short[] values)
149 throws OperationNotSupportedException {
150 new RecurringDuration(duration, period);
151 if (values.length != 6) {
152 throw new IllegalArgumentException("Wrong numbers of values");
153 }
154 this.setValues(values);
155 }
156
157
158
159
160
161
162
163 private void setPeriodInternal(TimeDuration period) {
164 _period = period;
165 }
166
167
168
169
170
171
172
173
174 public void setPeriod(TimeDuration period) throws UnsupportedOperationException {
175 setPeriodInternal(period);
176 }
177
178
179
180
181
182
183
184
185 public void setPeriod(String period) throws UnsupportedOperationException {
186 try {
187 setPeriodInternal(TimeDuration.parseTimeDuration(period));
188 } catch (ParseException e) {
189 String err = "RecurringDuration, setPeriod: " + e;
190 throw new IllegalArgumentException(err);
191 }
192 }
193
194
195
196
197
198
199
200
201 public void setDuration(TimeDuration duration) throws UnsupportedOperationException {
202 _duration = duration;
203 }
204
205
206
207
208
209
210
211
212 public void setDuration(String duration) throws UnsupportedOperationException {
213 try {
214 _duration = TimeDuration.parseTimeDuration(duration);
215 } catch (ParseException e) {
216 String err = "RecurringDuration, setDuration: " + e;
217 throw new IllegalArgumentException(err);
218 }
219 }
220
221
222
223
224
225
226
227
228 public void setHour(short hour) throws UnsupportedOperationException {
229 String err = "";
230 if (hour > 23) {
231 err = "the hour field (" + hour + ")must be strictly lower than 24";
232 throw new IllegalArgumentException(err);
233 }
234 _hour = hour;
235 }
236
237
238
239
240
241
242
243
244 public void setMinute(short minute) throws UnsupportedOperationException {
245 String err = "";
246 if (minute == -1 && _hour != -1) {
247 err = "minute cannot be omitted if the previous field is not omitted.";
248 throw new IllegalArgumentException(err);
249 } else if (minute > 59) {
250 err = "the minute field (" + minute + ") must be lower than 59";
251 throw new IllegalArgumentException(err);
252 }
253 _minute = minute;
254 }
255
256
257
258
259
260
261
262
263
264 public void setSecond(short second, short millsecond) throws UnsupportedOperationException {
265 String err = "";
266 if ((second == -1) && (_minute != -1)) {
267 err = "second cannot be omitted if the previous field is not omitted.";
268 throw new IllegalArgumentException(err);
269 } else if (second > 60) {
270 err = "the second field (" + second + ")must be lower than 60";
271 throw new IllegalArgumentException(err);
272 }
273 _second = second;
274 _millsecond = millsecond;
275 }
276
277
278
279
280
281
282
283
284
285 public void setZone(short hour, short minute) throws UnsupportedOperationException {
286 String err = "";
287 if (hour > 23) {
288 err = "the zone hour field (" + hour + ")must be strictly lower than 24";
289 throw new IllegalArgumentException(err);
290 }
291 _zoneHour = hour;
292
293 if (minute > 59) {
294 err = "the minute field (" + minute + ")must be lower than 59";
295 throw new IllegalArgumentException(err);
296 }
297 _zoneMinute = minute;
298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public void setValues(short[] values) throws UnsupportedOperationException {
317 this.setHour(values[0]);
318 this.setMinute(values[1]);
319 this.setSecond(values[2], values[3]);
320 this.setZone(values[4], values[5]);
321 }
322
323
324
325
326 public void setNegative() {
327 _isNegative = true;
328 }
329
330
331
332
333
334
335
336 public void setZoneNegative() throws UnsupportedOperationException {
337 _zoneNegative = true;
338 }
339
340
341
342
343 public void setUTC() {
344 _utc = true;
345 }
346
347
348 public TimeDuration getPeriod() {
349 return _period;
350 }
351
352 public TimeDuration getDuration() {
353 return _duration;
354 }
355
356 public short getHour() {
357 return _hour;
358 }
359
360 public short getMinute() {
361 return _minute;
362 }
363
364 public short getSeconds() {
365 return _second;
366 }
367
368 public short getMilli() {
369 return _millsecond;
370 }
371
372 public short getZoneHour() {
373 return _zoneHour;
374 }
375
376 public short getZoneMinute() {
377 return _zoneMinute;
378 }
379
380
381
382
383
384
385 public short[] getValues() {
386 short[] result = new short[6];
387 result[0] = this.getHour();
388 result[1] = this.getMinute();
389 result[2] = this.getSeconds();
390 result[3] = this.getMilli();
391 result[4] = this.getZoneHour();
392 result[5] = this.getZoneMinute();
393 return result;
394 }
395
396
397
398
399
400
401 public boolean isUTC() {
402 _utc = _zoneHour == 0 && _zoneMinute == 0;
403 return _utc;
404 }
405
406 public boolean isNegative() {
407 return _isNegative;
408 }
409
410 public boolean isZoneNegative() {
411 return _zoneNegative;
412 }
413
414
415
416
417
418
419 public boolean equals(Object object) {
420 if (object instanceof RecurringDurationBase) {
421 try {
422 return equal((RecurringDurationBase) object);
423 } catch (ValidationException e) {
424 e.printStackTrace();
425 return false;
426 }
427 }
428 return false;
429 }
430
431
432
433
434
435
436
437
438
439
440 public boolean equal(RecurringDurationBase reccD) throws ValidationException {
441 boolean result = false;
442 if (!(this.getPeriod().equals(reccD.getPeriod()))
443 || !(this.getDuration().equals(reccD.getDuration()))) {
444 String err = "Recurring Duration which have different values "
445 + "for the duration and period can not be compared";
446 throw new ValidationException(err);
447 }
448
449 result = (this.getHour() == reccD.getHour());
450 result = result && (this.getMinute() == reccD.getMinute());
451 result = result && (this.getSeconds() == reccD.getSeconds());
452 result = result && (this.getMilli() == reccD.getMilli());
453 result = result && (this.isNegative() == this.isNegative());
454 if (!reccD.isUTC()) {
455 result = result && (!this.isUTC());
456 result = result && (this.getZoneHour() == reccD.getZoneHour());
457 result = result && (this.getZoneMinute() == reccD.getZoneMinute());
458 }
459 return result;
460 }
461
462
463
464
465
466
467
468
469
470 public boolean isGreater(RecurringDurationBase reccD) throws ValidationException {
471 if (!(this.getPeriod().equals(reccD.getPeriod()))
472 || !(this.getDuration().equals(reccD.getDuration()))) {
473 String err = "Recurring Duration which have different values "
474 + "for the duration and period can not be compared";
475 throw new ValidationException(err);
476 }
477
478 boolean result = false;
479 short[] val_this = this.getValues();
480 short[] val_reccD = reccD.getValues();
481 for (int i = 0; result != true || i < val_this.length; i++) {
482 result = val_this[i] > val_reccD[i];
483 if (val_this[i] < val_reccD[i]) {
484 return false;
485 }
486 }
487 return result;
488 }
489
490 }