View Javadoc

1   /*
2    * $Id: DateList.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.Date;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.StringTokenizer;
42  
43  import net.fortuna.ical4j.model.parameter.Value;
44  import net.fortuna.ical4j.util.DateFormat;
45  import net.fortuna.ical4j.util.DateTimeFormat;
46  
47  /***
48   * Defines a list of iCalendar dates.
49   * @author benfortuna
50   */
51  public class DateList {
52  
53      private Value type;
54      private List dates;
55  
56      /***
57       * Default constructor.
58       * @param aType specifies the type of dates (either date or
59       * date-time)
60       */
61      public DateList(final Value aType) {
62          dates = new ArrayList();
63  
64          this.type = aType;
65      }
66  
67      /***
68       * Parses the specified string representation to create
69       * a list of dates.
70       * @param aValue a string representation of a list of
71       * dates
72       * @param aType specifies the type of dates (either date or
73       * date-time)
74       * @throws ParseException if an invalid date representation
75       * exists in the date list string
76       */
77      public DateList(final String aValue, final Value aType)
78          throws ParseException {
79          dates = new ArrayList();
80  
81          this.type = aType;
82  
83          for (StringTokenizer t = new StringTokenizer(aValue, ","); t
84                  .hasMoreTokens();) {
85              if (type != null && Value.DATE.equals(type.getValue())) {
86                  dates.add(DateFormat.getInstance().parse(t.nextToken()));
87              }
88              else {
89                  dates.add(DateTimeFormat.getInstance().parse(t.nextToken()));
90              }
91          }
92      }
93  
94      /***
95       * @see java.util.AbstractCollection#toString()
96       */
97      public final String toString() {
98  
99          StringBuffer b = new StringBuffer();
100 
101         for (Iterator i = dates.iterator(); i.hasNext();) {
102 
103             if (type != null && Value.DATE.equals(type.getValue())) {
104                 b.append(DateFormat.getInstance().format((Date) i.next()));
105             }
106             else {
107                 b.append(DateTimeFormat.getInstance().format((Date) i.next()));
108             }
109 
110             if (i.hasNext()) {
111                 b.append(',');
112             }
113         }
114 
115         return b.toString();
116     }
117 
118     /***
119      * Add a date to the list.
120      * @param date the date to add
121      * @return true
122      * @see List#add(java.lang.Object)
123      */
124     public final boolean add(final Date date) {
125         return dates.add(date);
126     }
127 
128     /***
129      * @return boolean indicates if the list is empty
130      * @see List#isEmpty()
131      */
132     public final boolean isEmpty() {
133         return dates.isEmpty();
134     }
135 
136     /***
137      * @return an iterator
138      * @see List#iterator()
139      */
140     public final Iterator iterator() {
141         return dates.iterator();
142     }
143 
144     /***
145      * Remove a date from the list
146      * @param date the date to remove
147      * @return true if the list contained the specified date
148      * @see List#remove(java.lang.Object)
149      */
150     public final boolean remove(final Date date) {
151         return dates.remove(date);
152     }
153 
154     /***
155      * @return the number of dates in the list
156      * @see List#size()
157      */
158     public final int size() {
159         return dates.size();
160     }
161 
162     /***
163      * Returns the VALUE parameter specifying the type
164      * of dates (ie. date or date-time) stored in this
165      * date list.
166      * @return Returns a Value parameter.
167      */
168     public final Value getType() {
169         return type;
170     }
171 }