|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.io.Serializable; |
|
35 import java.net.URISyntaxException; |
|
36 import java.util.ArrayList; |
|
37 import java.util.Collections; |
|
38 import java.util.Iterator; |
|
39 import java.util.List; |
|
40 |
|
41 import org.apache.commons.lang.ObjectUtils; |
|
42 import org.apache.commons.lang.builder.HashCodeBuilder; |
|
43 |
|
44 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
|
45 |
|
46 /** |
|
47 * $Id$ [Apr 5, 2004] |
|
48 * |
|
49 * Defines a list of iCalendar parameters. A parameter list may be specified as unmodifiable at instantiation - useful |
|
50 * for constant properties that you don't want modified. |
|
51 * @author Ben Fortuna |
|
52 */ |
|
53 public class ParameterList implements Serializable { |
|
54 |
|
55 private static final long serialVersionUID = -1913059830016450169L; |
|
56 |
|
57 private final List parameters; |
|
58 |
|
59 /** |
|
60 * Default constructor. Creates a modifiable parameter list. |
|
61 */ |
|
62 public ParameterList() { |
|
63 this(false); |
|
64 } |
|
65 |
|
66 /** |
|
67 * Constructor. |
|
68 * @param unmodifiable indicates whether the list should be mutable |
|
69 */ |
|
70 public ParameterList(final boolean unmodifiable) { |
|
71 if (unmodifiable) { |
|
72 parameters = Collections.unmodifiableList(new ArrayList()); |
|
73 } |
|
74 else { |
|
75 parameters = new CopyOnWriteArrayList(); |
|
76 } |
|
77 } |
|
78 |
|
79 /** |
|
80 * Creates a deep copy of the specified parameter list. That is, copies of all parameters in the specified list are |
|
81 * added to this list. |
|
82 * @param list a parameter list to copy parameters from |
|
83 * @param unmodifiable indicates whether the list should be mutable |
|
84 * @throws URISyntaxException where a parameter in the list specifies an invalid URI value |
|
85 */ |
|
86 public ParameterList(final ParameterList list, final boolean unmodifiable) |
|
87 throws URISyntaxException { |
|
88 |
|
89 final List parameterList = new CopyOnWriteArrayList(); |
|
90 for (final Iterator i = list.iterator(); i.hasNext();) { |
|
91 final Parameter parameter = (Parameter) i.next(); |
|
92 parameterList.add(parameter.copy()); |
|
93 } |
|
94 if (unmodifiable) { |
|
95 parameters = Collections.unmodifiableList(parameterList); |
|
96 } |
|
97 else { |
|
98 parameters = parameterList; |
|
99 } |
|
100 } |
|
101 |
|
102 /** |
|
103 * {@inheritDoc} |
|
104 */ |
|
105 public final String toString() { |
|
106 final StringBuffer buffer = new StringBuffer(); |
|
107 for (final Iterator i = parameters.iterator(); i.hasNext();) { |
|
108 buffer.append(';'); |
|
109 buffer.append(i.next().toString()); |
|
110 } |
|
111 return buffer.toString(); |
|
112 } |
|
113 |
|
114 /** |
|
115 * Returns the first parameter with the specified name. |
|
116 * @param aName name of the parameter |
|
117 * @return the first matching parameter or null if no matching parameters |
|
118 */ |
|
119 public final Parameter getParameter(final String aName) { |
|
120 for (final Iterator i = parameters.iterator(); i.hasNext();) { |
|
121 final Parameter p = (Parameter) i.next(); |
|
122 if (aName.equalsIgnoreCase(p.getName())) { |
|
123 return p; |
|
124 } |
|
125 } |
|
126 return null; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Returns a list of parameters with the specified name. |
|
131 * @param name name of parameters to return |
|
132 * @return a parameter list |
|
133 */ |
|
134 public final ParameterList getParameters(final String name) { |
|
135 final ParameterList list = new ParameterList(); |
|
136 for (final Iterator i = parameters.iterator(); i.hasNext();) { |
|
137 final Parameter p = (Parameter) i.next(); |
|
138 if (p.getName().equalsIgnoreCase(name)) { |
|
139 list.add(p); |
|
140 } |
|
141 } |
|
142 return list; |
|
143 } |
|
144 |
|
145 /** |
|
146 * Add a parameter to the list. Note that this method will not remove existing parameters of the same type. To |
|
147 * achieve this use { |
|
148 * @link ParameterList#replace(Parameter) } |
|
149 * @param parameter the parameter to add |
|
150 * @return true |
|
151 * @see List#add(java.lang.Object) |
|
152 */ |
|
153 public final boolean add(final Parameter parameter) { |
|
154 if (parameter == null) { |
|
155 throw new IllegalArgumentException("Trying to add null Parameter"); |
|
156 } |
|
157 return parameters.add(parameter); |
|
158 } |
|
159 |
|
160 /** |
|
161 * Replace any parameters of the same type with the one specified. |
|
162 * @param parameter parameter to add to this list in place of all others with the same name |
|
163 * @return true if successfully added to this list |
|
164 */ |
|
165 public final boolean replace(final Parameter parameter) { |
|
166 for (final Iterator i = getParameters(parameter.getName()).iterator(); i.hasNext();) { |
|
167 remove((Parameter) i.next()); |
|
168 } |
|
169 return add(parameter); |
|
170 } |
|
171 |
|
172 /** |
|
173 * @return boolean indicates if the list is empty |
|
174 * @see List#isEmpty() |
|
175 */ |
|
176 public final boolean isEmpty() { |
|
177 return parameters.isEmpty(); |
|
178 } |
|
179 |
|
180 /** |
|
181 * @return an iterator |
|
182 * @see List#iterator() |
|
183 */ |
|
184 public final Iterator iterator() { |
|
185 return parameters.iterator(); |
|
186 } |
|
187 |
|
188 /** |
|
189 * Remove a parameter from the list. |
|
190 * @param parameter the parameter to remove |
|
191 * @return true if the list contained the specified parameter |
|
192 * @see List#remove(java.lang.Object) |
|
193 */ |
|
194 public final boolean remove(final Parameter parameter) { |
|
195 return parameters.remove(parameter); |
|
196 } |
|
197 |
|
198 /** |
|
199 * Remove all parameters with the specified name. |
|
200 * @param paramName the name of parameters to remove |
|
201 */ |
|
202 public final void removeAll(final String paramName) { |
|
203 final ParameterList params = getParameters(paramName); |
|
204 parameters.removeAll(params.parameters); |
|
205 } |
|
206 |
|
207 /** |
|
208 * @return the number of parameters in the list |
|
209 * @see List#size() |
|
210 */ |
|
211 public final int size() { |
|
212 return parameters.size(); |
|
213 } |
|
214 |
|
215 /** |
|
216 * {@inheritDoc} |
|
217 */ |
|
218 public final boolean equals(final Object arg0) { |
|
219 if (arg0 instanceof ParameterList) { |
|
220 final ParameterList p = (ParameterList) arg0; |
|
221 return ObjectUtils.equals(parameters, p.parameters); |
|
222 } |
|
223 return super.equals(arg0); |
|
224 } |
|
225 |
|
226 /** |
|
227 * {@inheritDoc} |
|
228 */ |
|
229 public final int hashCode() { |
|
230 return new HashCodeBuilder().append(parameters).toHashCode(); |
|
231 } |
|
232 } |