View Javadoc

1   /*
2    * $Id: ParameterList.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 parameters.
42   *
43   * @author benf
44   */
45  public class ParameterList {
46  
47      private List parameters;
48  
49      /***
50       * Constructor.
51       */
52      public ParameterList() {
53          parameters = 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 = parameters.iterator(); i.hasNext();) {
64  
65              buffer.append(';');
66              buffer.append(i.next().toString());
67          }
68  
69          return buffer.toString();
70      }
71  
72      /***
73       * Returns the first parameter with the specified name.
74       *
75       * @param aName
76       *            name of the parameter
77       * @return the first matching parameter or null if no matching parameters
78       */
79      public final Parameter getParameter(final String aName) {
80  
81          for (Iterator i = parameters.iterator(); i.hasNext();) {
82              Parameter p = (Parameter) i.next();
83  
84              if (aName.equals(p.getName())) {
85                  return p;
86              }
87          }
88  
89          return null;
90      }
91  
92      /***
93       * Returns a list of parameters with the specified name.
94       * @param name
95       *            name of parameters to return
96       * @return a parameter list
97       */
98      public final ParameterList getParameters(final String name) {
99          ParameterList list = new ParameterList();
100 
101         for (Iterator i = parameters.iterator(); i.hasNext();) {
102             Parameter p = (Parameter) i.next();
103 
104             if (p.getName().equals(name)) {
105                 list.add(p);
106             }
107         }
108         return list;
109     }
110 
111     /***
112      * Add a parameter to the list.
113      * @param parameter the parameter to add
114      * @return true
115      * @see List#add(java.lang.Object)
116      */
117     public final boolean add(final Parameter parameter) {
118         return parameters.add(parameter);
119     }
120 
121     /***
122      * @return boolean indicates if the list is empty
123      * @see List#isEmpty()
124      */
125     public final boolean isEmpty() {
126         return parameters.isEmpty();
127     }
128 
129     /***
130      * @return an iterator
131      * @see List#iterator()
132      */
133     public final Iterator iterator() {
134         return parameters.iterator();
135     }
136 
137     /***
138      * Remove a parameter from the list
139      * @param parameter the parameter to remove
140      * @return true if the list contained the specified parameter
141      * @see List#remove(java.lang.Object)
142      */
143     public final boolean remove(final Parameter parameter) {
144         return parameters.remove(parameter);
145     }
146 
147     /***
148      * @return the number of parameters in the list
149      * @see List#size()
150      */
151     public final int size() {
152         return parameters.size();
153     }
154 }