View Javadoc

1   /*
2    * $Id: PeriodList.java [23-Apr-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.text.ParseException;
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.StringTokenizer;
41  
42  /***
43   * Defines a list of iCalendar periods.
44   * @author benfortuna
45   */
46  public class PeriodList {
47  
48      private List periods;
49  
50      /***
51       * Default constructor.
52       */
53      public PeriodList() {
54          periods = new ArrayList();
55      }
56  
57      /***
58       * Parses the specified string representation to create
59       * a list of periods.
60       * @param aValue a string representation of a list of
61       * periods
62       * @throws ParseException thrown when an invalid string
63       * representation of a period list is specified
64       */
65      public PeriodList(final String aValue) throws ParseException {
66          periods = new ArrayList();
67  
68          for (StringTokenizer t = new StringTokenizer(aValue, ","); t
69                  .hasMoreTokens();) {
70              periods.add(new Period(t.nextToken()));
71          }
72      }
73  
74      /***
75       * @see java.util.AbstractCollection#toString()
76       */
77      public final String toString() {
78  
79          StringBuffer b = new StringBuffer();
80  
81          for (Iterator i = periods.iterator(); i.hasNext();) {
82  
83              b.append(((Period) i.next()).toString());
84  
85              if (i.hasNext()) {
86                  b.append(',');
87              }
88          }
89  
90          return b.toString();
91      }
92  
93      /***
94       * Add a period to the list.
95       * @param period the period to add
96       * @return true
97       * @see List#add(java.lang.Object)
98       */
99      public final boolean add(final Period period) {
100         return periods.add(period);
101     }
102 
103     /***
104      * @return boolean indicates if the list is empty
105      * @see List#isEmpty()
106      */
107     public final boolean isEmpty() {
108         return periods.isEmpty();
109     }
110 
111     /***
112      * @return an iterator
113      * @see List#iterator()
114      */
115     public final Iterator iterator() {
116         return periods.iterator();
117     }
118 
119     /***
120      * Remove a period from the list
121      * @param period the period to remove
122      * @return true if the list contained the specified period
123      * @see List#remove(java.lang.Object)
124      */
125     public final boolean remove(final Period period) {
126         return periods.remove(period);
127     }
128 
129     /***
130      * @return the number of periods in the list
131      * @see List#size()
132      */
133     public final int size() {
134         return periods.size();
135     }
136 }