View Javadoc

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