|
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.IOException; |
|
35 import java.io.Serializable; |
|
36 import java.net.URISyntaxException; |
|
37 import java.text.ParseException; |
|
38 import java.util.ArrayList; |
|
39 import java.util.Iterator; |
|
40 |
|
41 /** |
|
42 * $Id$ [Apr 5, 2004] |
|
43 * |
|
44 * Defines a list of iCalendar components. |
|
45 * @author Ben Fortuna |
|
46 */ |
|
47 public class ComponentList extends ArrayList implements Serializable { |
|
48 |
|
49 private static final long serialVersionUID = 7308557606558767449L; |
|
50 |
|
51 /** |
|
52 * Default constructor. |
|
53 */ |
|
54 public ComponentList() { |
|
55 } |
|
56 |
|
57 /** |
|
58 * Creates a new instance with the specified initial capacity. |
|
59 * @param initialCapacity the initial capacity of the list |
|
60 */ |
|
61 public ComponentList(final int initialCapacity) { |
|
62 super(initialCapacity); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Creates a deep copy of the specified component list. |
|
67 * @param components a component list to copy |
|
68 * @throws IOException where an error occurs reading component data |
|
69 * @throws ParseException where component data cannot be parsed |
|
70 * @throws URISyntaxException where component data contains an invalid URI |
|
71 */ |
|
72 public ComponentList(ComponentList components) throws ParseException, |
|
73 IOException, URISyntaxException { |
|
74 |
|
75 for (final Iterator i = components.iterator(); i.hasNext();) { |
|
76 final Component c = (Component) i.next(); |
|
77 add(c.copy()); |
|
78 } |
|
79 } |
|
80 |
|
81 /** |
|
82 * {@inheritDoc} |
|
83 */ |
|
84 public final String toString() { |
|
85 final StringBuffer buffer = new StringBuffer(); |
|
86 for (final Iterator i = iterator(); i.hasNext();) { |
|
87 buffer.append(i.next().toString()); |
|
88 } |
|
89 return buffer.toString(); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Returns the first component of specified name. |
|
94 * @param aName name of component to return |
|
95 * @return a component or null if no matching component found |
|
96 */ |
|
97 public final Component getComponent(final String aName) { |
|
98 for (final Iterator i = iterator(); i.hasNext();) { |
|
99 final Component c = (Component) i.next(); |
|
100 if (c.getName().equals(aName)) { |
|
101 return c; |
|
102 } |
|
103 } |
|
104 return null; |
|
105 } |
|
106 |
|
107 /** |
|
108 * Returns a list containing all components with specified name. |
|
109 * @param name name of components to return |
|
110 * @return a list of components with the matching name |
|
111 */ |
|
112 public final ComponentList getComponents(final String name) { |
|
113 final ComponentList components = new ComponentList(); |
|
114 for (final Iterator i = iterator(); i.hasNext();) { |
|
115 final Component c = (Component) i.next(); |
|
116 if (c.getName().equals(name)) { |
|
117 components.add(c); |
|
118 } |
|
119 } |
|
120 return components; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Add a component to the list. |
|
125 * @param component the component to add |
|
126 * @return true |
|
127 * @see List#add(java.lang.Object) |
|
128 */ |
|
129 public final boolean add(final Component component) { |
|
130 return add((Object) component); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Overrides superclass to throw an <code>IllegalArgumentException</code> where argument is not a |
|
135 * <code>net.fortuna.ical4j.model.Component</code>. |
|
136 * @param component a component to add |
|
137 * @return true if the object was added, otherwise false |
|
138 * @see List#add(E) |
|
139 */ |
|
140 public final boolean add(final Object component) { |
|
141 if (!(component instanceof Component)) { |
|
142 throw new IllegalArgumentException("Argument not a " |
|
143 + Component.class.getName()); |
|
144 } |
|
145 return super.add(component); |
|
146 } |
|
147 |
|
148 /** |
|
149 * @return boolean indicates if the list is empty |
|
150 * @see List#isEmpty() |
|
151 */ |
|
152 // public final boolean isEmpty() { |
|
153 // return components.isEmpty(); |
|
154 // } |
|
155 /** |
|
156 * @return an iterator |
|
157 * @see List#iterator() |
|
158 */ |
|
159 // public final Iterator iterator() { |
|
160 // return components.iterator(); |
|
161 // } |
|
162 /** |
|
163 * Remove a component from the list. |
|
164 * @param component the component to remove |
|
165 * @return true if the list contained the specified component |
|
166 * @see List#remove(java.lang.Object) |
|
167 */ |
|
168 public final boolean remove(final Component component) { |
|
169 return remove((Object) component); |
|
170 } |
|
171 |
|
172 /** |
|
173 * @return the number of components in the list |
|
174 * @see List#size() |
|
175 */ |
|
176 // public final int size() { |
|
177 // return components.size(); |
|
178 // } |
|
179 /** |
|
180 * Provides a list containing all components contained in this component list. |
|
181 * @return a list |
|
182 */ |
|
183 // public final List toList() { |
|
184 // return new ArrayList(components); |
|
185 // } |
|
186 } |