1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.Vector;
8
9 import net.sourceforge.jsdp.util.TypedTime;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class RepeatTime implements Field {
27
28
29 private static final long serialVersionUID = -7772961172651467572L;
30
31
32 protected long repeatInterval;
33
34
35 protected long activeDuration;
36
37
38 protected Vector offsets;
39
40
41 protected boolean isTyped;
42
43
44
45
46 protected RepeatTime() {
47
48 super();
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62 public RepeatTime(final long repeatInterval, final long activeDuration, final long offset) throws SDPException {
63
64 super();
65
66 this.isTyped = true;
67
68 setActiveDuration(activeDuration);
69 setRepeatInterval(repeatInterval);
70
71 this.offsets = new Vector(3);
72 addOffset(offset);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86 public RepeatTime(final long repeatInterval, final long activeDuration, final long[] offsets) throws SDPException {
87
88 super();
89
90 this.isTyped = true;
91
92 setRepeatInterval(repeatInterval);
93 setActiveDuration(activeDuration);
94
95 this.offsets = new Vector(offsets.length);
96 for (int i = 0; i < offsets.length; i++) {
97 addOffset(offsets[i]);
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110 public static RepeatTime parse(final String field) throws SDPParseException {
111
112 if (!field.startsWith("r=")) {
113 throw new SDPParseException("The string \"" + field + "\" isn't a repeat time field");
114 }
115
116 RepeatTime r = null;
117
118 long repeatInterval;
119 long activeDuration;
120 long[] offsets;
121
122 String[] values = field.substring(2).split(" ");
123
124 try {
125 switch (values.length) {
126 case 0:
127 case 1:
128 case 2:
129 throw new SDPParseException("The string \"" + field + "\" isn't a valid repeat time field");
130 default:
131
132 repeatInterval = TypedTime.getTime(values[0]);
133
134
135 activeDuration = TypedTime.getTime(values[1]);
136
137
138 offsets = new long[values.length - 2];
139 for (int i = 0; i < offsets.length; i++) {
140 offsets[i] = TypedTime.getTime(values[i + 2]);
141 }
142
143
144 r = new RepeatTime(repeatInterval, activeDuration, offsets);
145 }
146 }
147 catch (SDPException parseException) {
148 throw new SDPParseException("The string \"" + field + "\" isn't a valid repeat time field", parseException);
149 }
150
151 return r;
152 }
153
154
155
156
157
158
159
160
161 public void addOffset(final long offset) throws SDPException {
162
163 if (offset >= 0) {
164 offsets.add(new Long(offset));
165 }
166 else {
167 throw new SDPException("Offsets must be >= 0");
168 }
169 }
170
171
172
173
174
175
176 public Object clone() {
177
178 RepeatTime field = new RepeatTime();
179 field.isTyped = this.isTyped;
180 field.activeDuration = this.activeDuration;
181 field.repeatInterval = this.repeatInterval;
182 field.offsets = (Vector) this.offsets.clone();
183
184 return field;
185 }
186
187
188
189
190
191
192 public long getActiveDuration() {
193
194 return activeDuration;
195 }
196
197
198
199
200
201
202
203
204 public long[] getOffsets() {
205
206 long[] values = new long[offsets.size()];
207 for (int i = 0; i < values.length; i++) {
208 Long offset = (Long) offsets.get(i);
209 values[i] = offset.longValue();
210 }
211
212 return values;
213 }
214
215
216
217
218
219
220 public long getRepeatInterval() {
221
222 return repeatInterval;
223 }
224
225
226
227
228
229
230 public char getType() {
231
232 return Field.REPEAT_TIME_FIELD;
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 public boolean isTypedTime() {
254
255 return isTyped;
256 }
257
258
259
260
261
262
263
264
265 public void setActiveDuration(final long activeDuration) throws SDPException {
266
267 if (activeDuration < 0) {
268 throw new SDPException("The active duration cannot be negative");
269 }
270
271 this.activeDuration = activeDuration;
272
273 }
274
275
276
277
278
279
280
281
282
283
284 public void setOffset(final long[] offsets) throws SDPException {
285
286 Vector temp = new Vector(offsets.length);
287 for (int i = 0; i < offsets.length; i++) {
288 if (offsets[i] >= 0) {
289 temp.add(new Long(offsets[i]));
290 }
291 else {
292 throw new SDPException("Offsets must be >= 0");
293 }
294 }
295
296
297 this.offsets = temp;
298 }
299
300
301
302
303
304
305
306
307 public void setRepeatInterval(final long repeatInterval) throws SDPException {
308
309 if (repeatInterval < 0) {
310 throw new SDPException("The repeat interval cannot be negative");
311 }
312
313 this.repeatInterval = repeatInterval;
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328 public void setTypedTime(final boolean typedTime) {
329
330 this.isTyped = typedTime;
331 }
332
333
334
335
336
337
338
339 public String toString() {
340
341 StringBuffer result;
342
343 result = new StringBuffer(getType() + "=");
344
345 result.append(repeatInterval + " ");
346 if (this.isTyped) {
347 result.append(TypedTime.toString(activeDuration));
348 for (int i = 0; i < offsets.size(); i++) {
349 Long offset = (Long) offsets.get(i);
350 result.append(" " + TypedTime.toString(offset.longValue()));
351 }
352 }
353 else {
354 result.append(activeDuration);
355 for (int i = 0; i < offsets.size(); i++) {
356 result.append(" " + offsets.get(i));
357 }
358 }
359
360 return result.toString();
361 }
362 }