1
2
3
4
5
6 package net.sourceforge.jsdp;
7
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class MediaDescription implements Description {
33
34
35 private static final long serialVersionUID = 2260842521161330113L;
36
37
38 protected Media m;
39
40
41 protected Information i;
42
43
44 protected Connection c;
45
46
47 protected HashMap bandwiths;
48
49
50 protected Key k;
51
52
53 protected HashMap attributes;
54
55
56
57
58 private MediaDescription() {
59
60 super();
61 }
62
63
64
65
66
67
68
69
70 public MediaDescription(final Media media) throws IllegalArgumentException {
71
72 super();
73
74 setMedia(media);
75
76 bandwiths = new HashMap();
77 attributes = new HashMap();
78 }
79
80
81
82
83
84
85
86
87
88 public void addAttribute(final Attribute field) throws IllegalArgumentException {
89
90 if (field == null) {
91 throw new IllegalArgumentException("An attribute field cannot be null");
92 }
93
94
95 String name = field.getName();
96
97
98 if (attributes.containsKey(name)) {
99
100 List values = null;
101 Object maybe = attributes.get(name);
102
103
104 if (maybe instanceof List) {
105
106 values = (List) maybe;
107 }
108
109 else {
110
111
112 Attribute previous = (Attribute) maybe;
113
114
115
116
117
118 values = new LinkedList();
119
120
121 values.add(previous);
122
123 attributes.put(field.getName(), values);
124 }
125
126 values.add(field);
127
128 }
129
130 else {
131
132 attributes.put(field.getName(), field);
133 }
134 }
135
136
137
138
139
140
141
142
143
144 public void addBandwith(final Bandwith field) throws IllegalArgumentException {
145
146 if (field == null) {
147 throw new IllegalArgumentException("A bandwith field cannot be null");
148 }
149
150 bandwiths.put(field.getModifier(), field);
151 }
152
153
154
155
156 public void clearAttributes() {
157
158 attributes.clear();
159 }
160
161
162
163
164 public void clearBandwiths() {
165
166 bandwiths.clear();
167 }
168
169
170
171
172
173
174 public Object clone() {
175
176 MediaDescription clone = new MediaDescription();
177
178 clone.m = (Media) this.m.clone();
179 clone.i = (Information) this.i.clone();
180 clone.c = (Connection) this.c.clone();
181 clone.bandwiths = (HashMap) this.bandwiths.clone();
182 clone.k = (Key) this.k.clone();
183 clone.attributes = (HashMap) this.attributes.clone();
184
185 return clone;
186 }
187
188
189
190
191
192
193
194
195
196 public Attribute getAttribute(final String name) {
197
198 Attribute result = null;
199 Object maybe = attributes.get(name);
200
201 if (maybe != null) {
202
203 if (maybe instanceof List) {
204
205 List values = (List) maybe;
206
207 result = (Attribute) values.get(0);
208 }
209 else {
210
211 result = (Attribute) maybe;
212 }
213 }
214
215 return result;
216 }
217
218
219
220
221
222
223
224
225
226 public Attribute getAttribute(final String name, final int index) {
227
228 Attribute result = null;
229 Object maybe = attributes.get(name);
230
231 if ((maybe != null) && (maybe instanceof List)) {
232
233 List values = (List) maybe;
234
235 result = (Attribute) values.get(index);
236 }
237
238 return result;
239 }
240
241
242
243
244
245
246
247 public Attribute[] getAttributes() {
248
249 Object maybe = null;
250 List values = null;
251
252 Collection result = new LinkedList();
253
254 for (Iterator i = attributes.values().iterator(); i.hasNext();) {
255
256 maybe = i.next();
257
258 if (maybe instanceof Attribute) {
259
260 result.add(maybe);
261 }
262 else {
263
264 values = (List) maybe;
265
266 for (Iterator j = values.iterator(); j.hasNext();) {
267
268 result.add(j.next());
269 }
270 }
271 }
272
273 return (Attribute[]) result.toArray(new Attribute[result.size()]);
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 public Attribute[] getAttributes(final String name) {
290
291 Attribute[] result;
292 Object maybe = attributes.get(name);
293
294 if (maybe != null) {
295
296 if (maybe instanceof List) {
297
298 List values = (List) maybe;
299
300 result = (Attribute[]) values.toArray(new Attribute[values.size()]);
301 }
302 else {
303
304 result = new Attribute[1];
305 result[0] = (Attribute) maybe;
306 }
307 }
308 else {
309 result = new Attribute[0];
310 }
311
312 return result;
313 }
314
315
316
317
318
319
320
321
322 public int getAttributesCount(final String name) {
323
324 int result = 0;
325 Object maybe = attributes.get(name);
326
327 if (maybe != null) {
328
329 if (maybe instanceof List) {
330
331 List values = (List) maybe;
332
333 result = values.size();
334 }
335 else {
336
337 result = 1;
338 }
339 }
340
341 return result;
342 }
343
344
345
346
347
348
349
350
351
352 public Bandwith getBandwith(final String modifier) {
353
354 return (Bandwith) this.bandwiths.get(modifier);
355 }
356
357
358
359
360
361
362
363 public Bandwith[] getBandwiths() {
364
365 Bandwith[] values = new Bandwith[bandwiths.size()];
366 Iterator iterator = bandwiths.values().iterator();
367
368 for (int j = 0; j < values.length; j++) {
369 values[j] = (Bandwith) iterator.next();
370 }
371
372 return values;
373 }
374
375
376
377
378
379
380 public Connection getConnection() {
381
382 return c;
383 }
384
385
386
387
388
389
390 public Information getInformation() {
391
392 return i;
393 }
394
395
396
397
398
399
400 public Key getKey() {
401
402 return k;
403 }
404
405
406
407
408
409
410 public Media getMedia() {
411
412 return m;
413 }
414
415
416
417
418
419
420
421
422
423 public boolean hasAttribute(String name) {
424
425 boolean result = false;
426
427 if (name != null) {
428 result = attributes.containsKey(name);
429 }
430
431 return result;
432 }
433
434
435
436
437
438
439
440 public boolean hasConnection() {
441
442 return c != null;
443 }
444
445
446
447
448
449
450
451 public boolean hasInformation() {
452
453 return i != null;
454 }
455
456
457
458
459
460
461
462 public boolean hasKey() {
463
464 return k != null;
465 }
466
467
468
469
470
471
472
473
474
475
476 public Attribute removeAttribute(final String name) {
477
478 Attribute a = null;
479 Object maybe = attributes.get(name);
480
481 if (maybe != null) {
482
483 if (maybe instanceof List) {
484
485 List values = (List) maybe;
486
487
488 a = (Attribute) values.remove(0);
489
490
491 if (values.isEmpty()) {
492 attributes.remove(name);
493 }
494 }
495 else {
496 a = (Attribute) attributes.remove(name);
497 }
498 }
499
500 return a;
501 }
502
503
504
505
506
507
508
509
510
511
512 public Attribute removeAttribute(final String name, final int index) {
513
514 Attribute a = null;
515 Object maybe = attributes.get(name);
516
517 if ((maybe != null) && (maybe instanceof List)) {
518
519 List values = (List) maybe;
520
521 a = (Attribute) values.remove(index);
522 }
523
524 return a;
525 }
526
527
528
529
530
531
532 public Attribute[] removeAttributes(final String name) {
533
534
535 Attribute[] result = getAttributes(name);
536
537
538 attributes.remove(name);
539
540 return result;
541 }
542
543
544
545
546
547
548
549
550
551 public Bandwith removeBandwith(final String modifier) {
552 return (Bandwith) bandwiths.remove(modifier);
553 }
554
555
556
557
558
559
560
561
562
563 public void setAttributes(Attribute[] fields) {
564
565 if (fields == null) {
566 throw new IllegalArgumentException("Attribute fields cannot be null");
567 }
568
569 int length = fields.length;
570 HashMap backup = (HashMap) attributes.clone();
571
572 try {
573
574
575 attributes.clear();
576
577
578 for (int i = 0; i < length; i++) {
579 addAttribute(fields[i]);
580 }
581 }
582 catch (IllegalArgumentException exception) {
583
584
585 attributes = backup;
586
587
588 throw exception;
589 }
590 }
591
592
593
594
595
596
597
598
599
600 public void setBandwiths(Bandwith[] fields) {
601
602 if (fields == null) {
603 throw new IllegalArgumentException("Bandwith fields cannot be null");
604 }
605
606 int length = fields.length;
607 HashMap backup = (HashMap) bandwiths.clone();
608
609 try {
610
611
612 bandwiths.clear();
613
614
615 for (int i = 0; i < length; i++) {
616 addBandwith(fields[i]);
617 }
618 }
619 catch (IllegalArgumentException exception) {
620
621
622 bandwiths = backup;
623
624
625 throw exception;
626 }
627 }
628
629
630
631
632
633
634 public void setConnection(final Connection c) {
635
636 this.c = c;
637 }
638
639
640
641
642
643
644 public void setInformation(final Information i) {
645
646 this.i = i;
647 }
648
649
650
651
652
653
654 public void setKey(final Key k) {
655
656 this.k = k;
657 }
658
659
660
661
662
663
664
665
666 public void setMedia(final Media m) throws IllegalArgumentException {
667
668 if (m == null) {
669 throw new IllegalArgumentException("The media field cannot be null");
670 }
671
672 this.m = m;
673 }
674
675
676
677
678
679
680 public String toString() {
681
682
683 StringBuffer result = new StringBuffer(m.toString());
684 result.append(Field.END_OF_FIELD);
685
686
687 if (i != null) {
688 result.append(i.toString());
689 result.append(Field.END_OF_FIELD);
690 }
691
692
693 if (c != null) {
694 result.append(c.toString());
695 result.append(Field.END_OF_FIELD);
696 }
697
698 int size = 0;
699
700 Bandwith[] myBandwiths = getBandwiths();
701 size = myBandwiths.length;
702
703
704 for (int i = 0; i < size; i++) {
705 result.append(myBandwiths[i]);
706 result.append(Field.END_OF_FIELD);
707 }
708
709
710 if (k != null) {
711 result.append(k.toString());
712 result.append(Field.END_OF_FIELD);
713
714 }
715
716 Attribute[] myAttributes = getAttributes();
717 size = myAttributes.length;
718
719
720 for (int i = 0; i < size; i++) {
721 result.append(myAttributes[i]);
722 result.append(Field.END_OF_FIELD);
723 }
724
725 return result.toString();
726 }
727 }