|
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 properties. |
|
45 * @author Ben Fortuna |
|
46 */ |
|
47 public class PropertyList extends ArrayList implements Serializable { |
|
48 |
|
49 private static final long serialVersionUID = -8875923766224921031L; |
|
50 |
|
51 /** |
|
52 * Default constructor. |
|
53 */ |
|
54 public PropertyList() { |
|
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 PropertyList(final int initialCapacity) { |
|
62 super(initialCapacity); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Creates a deep copy of the specified property list. |
|
67 * @param properties a property list |
|
68 * @throws ParseException where property data cannot be parsed |
|
69 * @throws IOException where property data cannot be read |
|
70 * @throws URISyntaxException where a property contains an invalid URI |
|
71 */ |
|
72 public PropertyList(PropertyList properties) throws ParseException, IOException, URISyntaxException { |
|
73 super(); |
|
74 for (final Iterator i = properties.iterator(); i.hasNext();) { |
|
75 final Property p = (Property) i.next(); |
|
76 add(p.copy()); |
|
77 } |
|
78 } |
|
79 |
|
80 /** |
|
81 * {@inheritDoc} |
|
82 */ |
|
83 public final String toString() { |
|
84 final StringBuffer buffer = new StringBuffer(); |
|
85 for (final Iterator i = iterator(); i.hasNext();) { |
|
86 buffer.append(i.next().toString()); |
|
87 } |
|
88 return buffer.toString(); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Returns the first property of specified name. |
|
93 * @param aName name of property to return |
|
94 * @return a property or null if no matching property found |
|
95 */ |
|
96 public final Property getProperty(final String aName) { |
|
97 for (final Iterator i = iterator(); i.hasNext();) { |
|
98 final Property p = (Property) i.next(); |
|
99 if (p.getName().equalsIgnoreCase(aName)) { |
|
100 return p; |
|
101 } |
|
102 } |
|
103 return null; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Returns a list of properties with the specified name. |
|
108 * @param name name of properties to return |
|
109 * @return a property list |
|
110 */ |
|
111 public final PropertyList getProperties(final String name) { |
|
112 final PropertyList list = new PropertyList(); |
|
113 for (final Iterator i = iterator(); i.hasNext();) { |
|
114 final Property p = (Property) i.next(); |
|
115 if (p.getName().equalsIgnoreCase(name)) { |
|
116 list.add(p); |
|
117 } |
|
118 } |
|
119 return list; |
|
120 } |
|
121 |
|
122 /** |
|
123 * Add a property to the list. |
|
124 * @param property the property to add |
|
125 * @return true |
|
126 * @see java.util.List#add(java.lang.Object) |
|
127 */ |
|
128 public final boolean add(final Property property) { |
|
129 return add((Object) property); |
|
130 } |
|
131 |
|
132 /** |
|
133 * Overrides superclass to throw an <code>IllegalArgumentException</code> where argument is not a |
|
134 * <code>net.fortuna.ical4j.model.Property</code>. |
|
135 * @param property a property to add |
|
136 * @return true if the property is added, otherwise false |
|
137 * @see java.util.List#add(E) |
|
138 */ |
|
139 public final boolean add(final Object property) { |
|
140 if (!(property instanceof Property)) { |
|
141 throw new IllegalArgumentException("Argument not a " |
|
142 + Property.class.getName()); |
|
143 } |
|
144 return super.add(property); |
|
145 } |
|
146 |
|
147 /** |
|
148 * Remove a property from the list. |
|
149 * @param property the property to remove |
|
150 * @return true if the list contained the specified property |
|
151 * @see java.util.List#remove(java.lang.Object) |
|
152 */ |
|
153 public final boolean remove(final Property property) { |
|
154 return remove((Object) property); |
|
155 } |
|
156 } |