1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9
10
11
12
13
14
15
16
17
18
19
20 public class TimeDescription implements Description {
21
22
23 private static final long serialVersionUID = 6860935545790980038L;
24
25
26 protected Time t;
27
28
29 protected ArrayList repeatTimes;
30
31
32
33
34
35 public TimeDescription() {
36
37 super();
38
39 setTime(new Time());
40 repeatTimes = new ArrayList();
41 }
42
43
44
45
46
47
48
49
50 public TimeDescription(final Time t) throws IllegalArgumentException {
51
52 this();
53 setTime(t);
54 }
55
56
57
58
59
60
61
62
63
64 protected TimeDescription(final TimeDescription td) {
65
66 super();
67
68 t = (Time) td.t.clone();
69 repeatTimes = (ArrayList) td.repeatTimes.clone();
70 }
71
72
73
74
75
76
77
78
79 public void addRepeatTime(final RepeatTime field) throws IllegalArgumentException {
80
81 if (field == null) {
82 throw new IllegalArgumentException("A repeat time field cannot be null");
83 }
84
85 repeatTimes.add(field);
86 }
87
88
89
90
91 public void clearRepeatTimes() {
92
93 repeatTimes.clear();
94 }
95
96
97
98
99
100
101 public Object clone() {
102
103 return new TimeDescription(this);
104 }
105
106
107
108
109
110
111
112 public RepeatTime[] getRepeatTimes() {
113
114 return (RepeatTime[]) repeatTimes.toArray(new RepeatTime[repeatTimes.size()]);
115 }
116
117
118
119
120
121
122 public Time getTime() {
123
124 return t;
125 }
126
127
128
129
130
131
132
133
134
135 public void setRepeatTimes(final RepeatTime[] fields) throws IllegalArgumentException {
136
137 if (fields == null) {
138 throw new IllegalArgumentException("Repeat time fields cannot be null");
139 }
140
141 int length = fields.length;
142 ArrayList backup = (ArrayList) repeatTimes.clone();
143
144 try {
145
146
147 repeatTimes.clear();
148
149
150 for (int i = 0; i < length; i++) {
151 addRepeatTime(fields[i]);
152 }
153 }
154 catch (IllegalArgumentException exception) {
155
156
157 repeatTimes = backup;
158
159
160 throw exception;
161 }
162 }
163
164
165
166
167
168
169
170
171 public void setTime(final Time t) throws IllegalArgumentException {
172
173 if (t == null) {
174 throw new IllegalArgumentException("The time field cannot be null");
175 }
176
177 this.t = t;
178 }
179
180
181
182
183
184
185 public String toString() {
186
187 StringBuffer result = new StringBuffer(t.toString());
188 result.append(Field.END_OF_FIELD);
189
190 for (Iterator i = repeatTimes.iterator(); i.hasNext();) {
191
192 result.append( i.next());
193 result.append(Field.END_OF_FIELD);
194 }
195
196 return result.toString();
197 }
198 }