|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.io.Serializable; |
|
35 import java.net.URI; |
|
36 import java.net.URISyntaxException; |
|
37 import java.util.Iterator; |
|
38 import java.util.List; |
|
39 import java.util.StringTokenizer; |
|
40 |
|
41 import net.fortuna.ical4j.util.CompatibilityHints; |
|
42 import net.fortuna.ical4j.util.Strings; |
|
43 import net.fortuna.ical4j.util.Uris; |
|
44 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
|
45 |
|
46 /** |
|
47 * $Id$ [23-Apr-2004] |
|
48 * |
|
49 * Defines a list of iCalendar addresses. |
|
50 * @author Ben Fortuna |
|
51 */ |
|
52 public class AddressList implements Serializable { |
|
53 |
|
54 private static final long serialVersionUID = 81383256078213569L; |
|
55 |
|
56 private List addresses; |
|
57 |
|
58 /** |
|
59 * Default constructor. |
|
60 */ |
|
61 public AddressList() { |
|
62 addresses = new CopyOnWriteArrayList(); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Parses the specified string representation to create a list of addresses. |
|
67 * @param aValue a string representation of a list of addresses |
|
68 * @throws URISyntaxException where the specified string is not a valid representation |
|
69 */ |
|
70 public AddressList(final String aValue) throws URISyntaxException { |
|
71 addresses = new CopyOnWriteArrayList(); |
|
72 final StringTokenizer t = new StringTokenizer(aValue, ","); |
|
73 while (t.hasMoreTokens()) { |
|
74 |
|
75 try { |
|
76 addresses.add(new URI(Uris.encode(Strings |
|
77 .unquote(t.nextToken())))); |
|
78 } |
|
79 catch (URISyntaxException use) { |
|
80 // ignore invalid addresses if relaxed parsing is enabled.. |
|
81 if (!CompatibilityHints.isHintEnabled( |
|
82 CompatibilityHints.KEY_RELAXED_PARSING)) { |
|
83 |
|
84 throw use; |
|
85 } |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * {@inheritDoc} |
|
92 */ |
|
93 public final String toString() { |
|
94 final StringBuffer b = new StringBuffer(); |
|
95 for (final Iterator i = addresses.iterator(); i.hasNext();) { |
|
96 b.append(Strings.quote(Uris.decode(Strings.valueOf(i.next())))); |
|
97 if (i.hasNext()) { |
|
98 b.append(','); |
|
99 } |
|
100 } |
|
101 return b.toString(); |
|
102 } |
|
103 |
|
104 /** |
|
105 * Add an address to the list. |
|
106 * @param address the address to add |
|
107 * @return true |
|
108 * @see List#add(java.lang.Object) |
|
109 */ |
|
110 public final boolean add(final URI address) { |
|
111 return addresses.add(address); |
|
112 } |
|
113 |
|
114 /** |
|
115 * @return boolean indicates if the list is empty |
|
116 * @see List#isEmpty() |
|
117 */ |
|
118 public final boolean isEmpty() { |
|
119 return addresses.isEmpty(); |
|
120 } |
|
121 |
|
122 /** |
|
123 * @return an iterator |
|
124 * @see List#iterator() |
|
125 */ |
|
126 public final Iterator iterator() { |
|
127 return addresses.iterator(); |
|
128 } |
|
129 |
|
130 /** |
|
131 * Remove an address from the list. |
|
132 * @param address the address to remove |
|
133 * @return true if the list contained the specified address |
|
134 * @see List#remove(java.lang.Object) |
|
135 */ |
|
136 public final boolean remove(final URI address) { |
|
137 return addresses.remove(address); |
|
138 } |
|
139 |
|
140 /** |
|
141 * @return the number of addresses in the list |
|
142 * @see List#size() |
|
143 */ |
|
144 public final int size() { |
|
145 return addresses.size(); |
|
146 } |
|
147 } |