View Javadoc

1   /*
2    * $Id: ComponentList.java [Apr 5, 2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.model;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /***
41   * Defines a list of iCalendar components.
42   *
43   * @author benf
44   */
45  public class ComponentList {
46  
47      private List components;
48  
49      /***
50       * Constructor.
51       */
52      public ComponentList() {
53          components = new ArrayList();
54      }
55  
56      /***
57       * @see java.util.AbstractCollection#toString()
58       */
59      public final String toString() {
60  
61          StringBuffer buffer = new StringBuffer();
62  
63          for (Iterator i = components.iterator(); i.hasNext();) {
64  
65              buffer.append(i.next().toString());
66          }
67  
68          return buffer.toString();
69      }
70  
71      /***
72       * Returns the first component of specified name.
73       * @param aName name of component to return
74       * @return a component or null if no matching component
75       * found
76       */
77      public final Component getComponent(final String aName) {
78  
79          for (Iterator i = components.iterator(); i.hasNext();) {
80              Component c = (Component) i.next();
81  
82              if (c.getName().equals(aName)) {
83                  return c;
84              }
85          }
86  
87          return null;
88      }
89  
90      /***
91       * Add a component to the list.
92       * @param component the component to add
93       * @return true
94       * @see List#add(java.lang.Object)
95       */
96      public final boolean add(final Component component) {
97          return components.add(component);
98      }
99  
100     /***
101      * @return boolean indicates if the list is empty
102      * @see List#isEmpty()
103      */
104     public final boolean isEmpty() {
105         return components.isEmpty();
106     }
107 
108     /***
109      * @return an iterator
110      * @see List#iterator()
111      */
112     public final Iterator iterator() {
113         return components.iterator();
114     }
115 
116     /***
117      * Remove a component from the list
118      * @param component the component to remove
119      * @return true if the list contained the specified component
120      * @see List#remove(java.lang.Object)
121      */
122     public final boolean remove(final Component component) {
123         return components.remove(component);
124     }
125 
126     /***
127      * @return the number of components in the list
128      * @see List#size()
129      */
130     public final int size() {
131         return components.size();
132     }
133 
134     /***
135      * Provides a list containing all components contained
136      * in this component list.
137      * @return a list
138      */
139     public final List toList() {
140         return new ArrayList(components);
141     }
142 }