1 /*
2 * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3 * Vita
4 */
5 package net.sourceforge.jsdp;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9
10 /**
11 * A <tt>TimeDescription</tt> represents the fields present within a SDP time
12 * description.
13 *
14 * @since 0.1.0
15 *
16 * @version 1.1
17 *
18 * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
19 */
20 public class TimeDescription implements Description {
21
22 /** The class Stream Unique Identifier, SUID */
23 private static final long serialVersionUID = 6860935545790980038L;
24
25 /** The time field */
26 protected Time t;
27
28 /** The repeat time fields */
29 protected ArrayList repeatTimes;
30
31 /**
32 * Creates a new <tt>TimeDescription</tt>.
33 *
34 */
35 public TimeDescription() {
36
37 super();
38
39 setTime(new Time());
40 repeatTimes = new ArrayList();
41 }
42
43 /**
44 * Creates a new <tt>TimeDescription</tt>.
45 *
46 * @param t the time field
47 *
48 * @throws IllegalArgumentException if the time field is <tt>null</tt>
49 */
50 public TimeDescription(final Time t) throws IllegalArgumentException {
51
52 this();
53 setTime(t);
54 }
55
56 /**
57 * Creates a new <tt>TimeDescription</tt>. This constructor is used to
58 * implement the {@link #clone()} method, because this class has a default
59 * constructor that performs some operations that aren't required while
60 * cloning a description.
61 *
62 * @param td the <tt>TimeDescription</tt> to clone
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 * Add a repeat time field.
74 *
75 * @param field the field to add
76 *
77 * @throws IllegalArgumentException if the field is <tt>null</tt>
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 * Remove all repeat time fields contained in the time description.
90 */
91 public void clearRepeatTimes() {
92
93 repeatTimes.clear();
94 }
95
96 /**
97 * Returns a clone of this description.
98 *
99 * @return a clone of this description
100 */
101 public Object clone() {
102
103 return new TimeDescription(this);
104 }
105
106 /**
107 * Returns the repeat time fields.
108 *
109 * @return an array that contains the repeat time fields contained in the
110 * time description
111 */
112 public RepeatTime[] getRepeatTimes() {
113
114 return (RepeatTime[]) repeatTimes.toArray(new RepeatTime[repeatTimes.size()]);
115 }
116
117 /**
118 * Returns the time field.
119 *
120 * @return the time field
121 */
122 public Time getTime() {
123
124 return t;
125 }
126
127 /**
128 * Sets the repeat time fields.
129 *
130 * @param fields the repeat time fields to set
131 *
132 * @throws IllegalArgumentException if one or more repeat time field is
133 * <tt>null</tt>
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 /* Remove current repeat times */
147 repeatTimes.clear();
148
149 /* Add repeat times */
150 for (int i = 0; i < length; i++) {
151 addRepeatTime(fields[i]);
152 }
153 }
154 catch (IllegalArgumentException exception) {
155
156 /* An error is occured, so we "rollback" */
157 repeatTimes = backup;
158
159 /* Rethrow the exception */
160 throw exception;
161 }
162 }
163
164 /**
165 * Sets the time field.
166 *
167 * @param t the time field
168 *
169 * @throws IllegalArgumentException if the time field is <tt>null</tt>
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 * Returns a string representation of this description
182 *
183 * @return a string representation of the description
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 }