-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTMLPeriodFormat.java
More file actions
569 lines (540 loc) · 13.9 KB
/
RTMLPeriodFormat.java
File metadata and controls
569 lines (540 loc) · 13.9 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
Copyright 2006, Astrophysics Research Institute, Liverpool John Moores University.
This file is part of org.estar.rtml.
org.estar.rtml is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
org.estar.rtml is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with org.estar.rtml; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// RTMLPeriodFormat.java
// $Header: /space/home/eng/cjm/cvs/org_estar_rtml/RTMLPeriodFormat.java,v 1.7 2008-08-11 13:54:54 cjm Exp $
package org.estar.rtml;
import java.io.*;
import java.text.*;
import java.util.*;
/**
* This class represents a time period, as specified in RTML 3.0g. THis is represented in string form as:
* <code>
* P{(yyyy)Y{(mm)M}{(dd)D}{T{(hh)H}{(mm}M}{(ss.s..)S}
* </code>
* For the purposes of this implementation, we have assumed 30 days in 1 month, and 365 days in 1 year.
* @author Chris Mottram
* @version $Revision$
*/
public class RTMLPeriodFormat implements Serializable
{
/**
* Revision control system version id.
*/
public final static String RCSID = "$Id$";
/**
* Serial version ID. Fixed as these documents can be used as parameters in RMI calls across JVMs.
*/
static final long serialVersionUID = 5144074596612293135L;
/**
* Number of milliseconds in a second.
*/
private final static long SECOND_MS = 1000L;
/**
* Number of milliseconds in a minute.
*/
private final static long MINUTE_MS = (60L*SECOND_MS);
/**
* Number of milliseconds in an hour.
*/
private final static long HOUR_MS = (60L*MINUTE_MS);
/**
* Number of milliseconds in an day.
*/
private final static long DAY_MS = (24L*HOUR_MS);
/**
* Number of milliseconds in a month. We have assumed a month has 30 days for this implementation.
*/
private final static long MONTH_MS = (30L*DAY_MS);
/**
* Number of milliseconds in a year. We have assumed a year has 365 days for this implementation.
*/
private final static long YEAR_MS = (365L*DAY_MS);
/**
* Part of the time period.
*/
private int years = 0;
/**
* Part of the time period.
*/
private int months = 0;
/**
* Part of the time period.
*/
private int days = 0;
/**
* Part of the time period.
*/
private int hours = 0;
/**
* Part of the time period.
*/
private int minutes = 0;
/**
* Part of the time period.
*/
private double seconds = 0;
/**
* Default constructor.
*/
public RTMLPeriodFormat()
{
super();
}
/**
* Parse a string of the form:
* <code>
* P{(yyyy)Y{(mm)M}{(dd)D}{T{(hh)H}{(mm}M}{(ss.s..)S}
* </code>
* into a time period.
* @param s The string to parse.
* @exception RTMLException Thrown if the input string is not valid.
* @exception NumberFormatException Thrown if parsing a numeric value within the period fails.
* @see #removeWhitespace
* @see #setYears
* @see #setMonths
* @see #setDays
* @see #setHours
* @see #setMinutes
* @see #setSeconds
*/
public void parse(String s) throws RTMLException, NumberFormatException
{
StringTokenizer st = null;
String tokenString = null;
String delimiterString = new String("PYMDTHS");
String valueString = null;
boolean inP = false;
boolean inT = false;
int i;
double d;
// remove whitespace from ends of string
s = removeWhitespace(s);
// return delimiters
st = new StringTokenizer(s,delimiterString,true);
while(st.hasMoreTokens())
{
tokenString = st.nextToken();
// ensure P token is first token received
if(tokenString.equals("P") == true)
inP = true;
if(inP == false)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):P not parsed when received token "+
tokenString);
}
// Is it a delimiter or a value?
if(delimiterString.indexOf(tokenString) > -1) // tokenString is in delimiterString
{
if(tokenString.equals("P"))
{
inP = true;// no nothing, effectively - see above
}
else if(tokenString.equals("Y"))
{
if(inT)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):Y detected after T.");
}
i = Integer.parseInt(valueString);
setYears(i);
valueString = null;
}
else if(tokenString.equals("M"))
{
i = Integer.parseInt(valueString);
if(inT)
setMinutes(i);
else
setMonths(i);
valueString = null;
}
else if(tokenString.equals("D"))
{
if(inT)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):D detected after T.");
}
i = Integer.parseInt(valueString);
setDays(i);
valueString = null;
}
else if(tokenString.equals("T"))
{
inT = true;
if(valueString != null)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):T detected when value string "+
valueString+" not used yet.");
}
}
else if(tokenString.equals("H"))
{
if(inT == false)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):H detected before T.");
}
i = Integer.parseInt(valueString);
setHours(i);
valueString = null;
}
else if(tokenString.equals("S"))
{
if(inT == false)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):S detected before T.");
}
d = Double.parseDouble(valueString);
setSeconds(d);
valueString = null;
}
else
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):Illegal delimiter "+tokenString+".");
}
}
else
{
if(valueString != null)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):setting new value string to "+tokenString+
" when old value string "+valueString+" not used yet.");
}
// string is value - to be associated with next parsed delimiter
valueString = tokenString;
}// end else if not delimiter
}// end while tokens
if(valueString != null)
{
throw new RTMLException(this.getClass().getName()+
":parse("+s+"):value string "+valueString+
" detected without trailing delimiter.");
}
}
/**
* Get the number of milliseconds represented by this period.
* @return The number of milliseconds as a long.
* @see #years
* @see #months
* @see #days
* @see #hours
* @see #minutes
* @see #seconds
* @see #SECOND_MS
* @see #MINUTE_MS
* @see #HOUR_MS
* @see #DAY_MS
* @see #MONTH_MS
* @see #YEAR_MS
*/
public long getMilliseconds()
{
return (long)(years*YEAR_MS)+(months*MONTH_MS)+(days*DAY_MS)+
(hours*HOUR_MS)+(minutes*MINUTE_MS)+(long)(seconds*((double)SECOND_MS));
}
/**
* Set method.
* @param i An integer.
* @see #years
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setYears(int i) throws IllegalArgumentException
{
if(i<0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setYears:"+i+
" is not a legal number of years:It is negative.");
}
years = i;
}
/**
* Get method.
* @return An integer.
* @see #years
*/
public int getYears()
{
return years;
}
/**
* Set method.
* @param i An integer.
* @see #months
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setMonths(int i) throws IllegalArgumentException
{
if(i<0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setMonths:"+i+
" is not a legal number of months:It is negative.");
}
months = i;
}
/**
* Get method.
* @return An integer.
* @see #months
*/
public int getMonths()
{
return months;
}
/**
* Set method.
* @param i An integer.
* @see #days
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setDays(int i) throws IllegalArgumentException
{
if(i<0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setDays:"+i+
" is not a legal number of days:It is negative.");
}
days = i;
}
/**
* Get method.
* @return An integer.
* @see #days
*/
public int getDays()
{
return days;
}
/**
* Set method.
* @param i An integer.
* @see #hours
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setHours(int i) throws IllegalArgumentException
{
if(i<0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setHours:"+i+
" is not a legal number of hours:It is negative.");
}
hours = i;
}
/**
* Get method.
* @return An integer.
* @see #hours
*/
public int getHours()
{
return hours;
}
/**
* Set method.
* @param i An integer.
* @see #minutes
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setMinutes(int i) throws IllegalArgumentException
{
if(i<0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setMinutes:"+i+
" is not a legal number of minutes:It is negative.");
}
minutes = i;
}
/**
* Get method.
* @return An integer.
* @see #minutes
*/
public int getMinutes()
{
return minutes;
}
/**
* Set method.
* @param d A double.
* @see #seconds
* @exception IllegalArgumentException Thrown if the input number is out of range.
*/
public void setSeconds(double d) throws IllegalArgumentException
{
if(d<0.0)
{
throw new IllegalArgumentException(this.getClass().getName()+":setSeconds:"+d+
" is not a legal number of seconds:It is negative.");
}
seconds = d;
}
/**
* Get method.
* @return A double.
* @see #seconds
*/
public double getSeconds()
{
return seconds;
}
/**
* Test whether one period format is equal to another.
* @param obj The other instance we are testing against.
* @return Returns true if the contents are the same, and false if they are not.
* @see #years
* @see #months
* @see #days
* @see #hours
* @see #minutes
* @see #seconds
*/
public boolean equals(Object obj)
{
RTMLPeriodFormat other = null;
if(obj == null)
return false;
if((obj instanceof RTMLPeriodFormat) == false)
return false;
other = (RTMLPeriodFormat)obj;
if(other.getYears() != years)
return false;
if(other.getMonths() != months)
return false;
if(other.getDays() != days)
return false;
if(other.getHours() != hours)
return false;
if(other.getMinutes() != minutes)
return false;
if(Math.abs(other.getSeconds()-seconds) > 0.0001)
return false;
return true;
}
/**
* Method to print out a string representation of this period.
* @return A string.
* @see #toString(java.lang.String)
*/
public String toString()
{
return toString("");
}
/**
* Method to print out a string representation of this period, with a prefix.
* String is of the form:
* <code>
* P{(yyyy)Y{(mm)M}{(dd)D}{T{(hh)H}{(mm}M}{(ss.s..)S}
* </code>
* @param prefix A string to prefix the period.
* @return A string.
* @see #years
* @see #months
* @see #days
* @see #hours
* @see #minutes
* @see #seconds
*/
public String toString(String prefix)
{
StringBuffer sb = null;
sb = new StringBuffer();
sb.append(prefix+"P");
if(years != 0)
sb.append(years+"Y");
if(months != 0)
sb.append(months+"M");
if(days != 0)
sb.append(days+"D");
if((hours != 0) || (minutes != 0) || (seconds != 0))
sb.append("T");
if(hours != 0)
sb.append(hours+"H");
if(minutes != 0)
sb.append(minutes+"M");
if(seconds != 0)
sb.append(seconds+"S");
// special case for 0 period
if((years == 0)&&(months == 0)&&(days == 0)&&(hours == 0)&&(minutes == 0)&&(seconds == 0))
sb.append("T"+seconds+"S");
return sb.toString();
}
/**
* Method to remove whitespace before the first non-whitespace character, and after the last non-whitespace
* character. Whitespace in the middle of the non-whitespace string are <b>not</b> removed.
* Whitespace is "\n\t\r\f ".
* @param s The string.
* @return A string with start and end whitespace removed.
*/
protected String removeWhitespace(String s)
{
String whitespace = new String("\n\t\r\f ");
int sindex,eindex;
boolean done;
// set sindex to index of first character in s not a whitespace
sindex = 0;
done = false;
while((sindex < s.length()) && (done == false))
{
done = (whitespace.indexOf(s.charAt(sindex)) < 0);
if(done == false)
sindex++;
}
// set eindex to index of last character in s not a whitespace
eindex = s.length()-1;
done = false;
while((eindex >= 0) && (done == false))
{
done = (whitespace.indexOf(s.charAt(eindex)) < 0);
if(done == false)
eindex--;
}
// check if string all whitespace
if(eindex <= sindex)
return new String("");
return s.substring(sindex,eindex+1);
}
}
/*
** $Log: not supported by cvs2svn $
** Revision 1.6 2008/05/27 14:19:36 cjm
** Added serialVersionUID.
**
** Revision 1.5 2008/05/08 14:03:29 cjm
** Added special case to toString for 0 length period, to make it conform to:
** http://www.w3.org/TR/xmlschema-2/#duration
** "If the number of years, months, days, hours, minutes, or seconds in any expression equals zero,
** the number and its corresponding designator ·may· be omitted.
** However, at least one number and its designator ·must· be present."
**
** Revision 1.4 2007/01/30 18:31:19 cjm
** gnuify: Added GNU General Public License.
**
** Revision 1.3 2005/06/08 11:39:01 cjm
** Fixed comments.
**
** Revision 1.2 2005/04/27 15:22:04 cjm
** Added parse checks, ensure Y/D apppear before T, ensure H/S appear after T.
**
** Revision 1.1 2005/04/27 13:45:48 cjm
** Initial revision
**
*/