src/net/fortuna/ical4j/model/Parameter.java

changeset 0
fb9019fb1bf7
child 4
45d57ecba757
equal deleted inserted replaced
-1:000000000000 0:45375dc68018
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.net.URISyntaxException;
35
36 import net.fortuna.ical4j.util.Strings;
37
38 import org.apache.commons.lang.builder.EqualsBuilder;
39 import org.apache.commons.lang.builder.HashCodeBuilder;
40
41 /**
42 * Defines an iCalendar parameter. Subclasses of this class provide additional validation and typed values for specific
43 * iCalendar parameters.
44 *
45 * Note that subclasses must provide a reference to the factory used to create the
46 * parameter to support parameter cloning (copy). If no factory is specified an
47 * {@link UnsupportedOperationException} will be thrown by the {@link #copy()} method.
48 *
49 * @author Ben Fortuna
50 *
51 * $Id$ [Apr 5, 2004]
52 */
53 public abstract class Parameter extends Content {
54
55 private static final long serialVersionUID = -2058497904769713528L;
56
57 /**
58 * Region abbreviation.
59 */
60 public static final String ABBREV = "ABBREV";
61
62 /**
63 * Alternate text representation.
64 */
65 public static final String ALTREP = "ALTREP";
66
67 /**
68 * Common name.
69 */
70 public static final String CN = "CN";
71
72 /**
73 * Calendar user type.
74 */
75 public static final String CUTYPE = "CUTYPE";
76
77 /**
78 * Delegator.
79 */
80 public static final String DELEGATED_FROM = "DELEGATED-FROM";
81
82 /**
83 * Delegatee.
84 */
85 public static final String DELEGATED_TO = "DELEGATED-TO";
86
87 /**
88 * Directory entry.
89 */
90 public static final String DIR = "DIR";
91
92 /**
93 * Inline encoding.
94 */
95 public static final String ENCODING = "ENCODING";
96
97 /**
98 * Format type.
99 */
100 public static final String FMTTYPE = "FMTTYPE";
101
102 /**
103 * Free/busy time type.
104 */
105 public static final String FBTYPE = "FBTYPE";
106
107 /**
108 * Language for text.
109 */
110 public static final String LANGUAGE = "LANGUAGE";
111
112 /**
113 * Group or list membership.
114 */
115 public static final String MEMBER = "MEMBER";
116
117 /**
118 * Participation status.
119 */
120 public static final String PARTSTAT = "PARTSTAT";
121
122 /**
123 * Recurrence identifier range.
124 */
125 public static final String RANGE = "RANGE";
126
127 /**
128 * Alarm trigger relationship.
129 */
130 public static final String RELATED = "RELATED";
131
132 /**
133 * Relationship type.
134 */
135 public static final String RELTYPE = "RELTYPE";
136
137 /**
138 * Participation role.
139 */
140 public static final String ROLE = "ROLE";
141
142 /**
143 * RSVP expectation.
144 */
145 public static final String RSVP = "RSVP";
146
147 /**
148 * Schedule agent.
149 */
150 public static final String SCHEDULE_AGENT = "SCHEDULE-AGENT";
151
152 /**
153 * Schedule status.
154 */
155 public static final String SCHEDULE_STATUS = "SCHEDULE-STATUS";
156
157 /**
158 * Sent by.
159 */
160 public static final String SENT_BY = "SENT-BY";
161
162 /**
163 * Type.
164 */
165 public static final String TYPE = "TYPE";
166
167 /**
168 * Reference to time zone object.
169 */
170 public static final String TZID = "TZID";
171
172 /**
173 * Property value data type.
174 */
175 public static final String VALUE = "VALUE";
176
177 /**
178 * Reference to vvenue component.
179 */
180 public static final String VVENUE = "VVENUE";
181
182 /**
183 * Prefix to all experimental parameters.
184 */
185 public static final String EXPERIMENTAL_PREFIX = "X-";
186
187 private String name;
188
189 private final ParameterFactory factory;
190
191 /**
192 * @param aName the parameter identifier
193 * @param factory the factory used to create the parameter
194 */
195 public Parameter(final String aName, ParameterFactory factory) {
196 this.name = aName;
197 this.factory = factory;
198 }
199
200 /**
201 * {@inheritDoc}
202 */
203 public final String toString() {
204 final StringBuffer b = new StringBuffer();
205 b.append(getName());
206 b.append('=');
207 if (isQuotable()) {
208 b.append(Strings.quote(Strings.valueOf(getValue())));
209 }
210 else {
211 b.append(Strings.valueOf(getValue()));
212 }
213 return b.toString();
214 }
215
216 /**
217 * Indicates whether the current parameter value should be quoted.
218 * @return true if the value should be quoted, otherwise false
219 */
220 protected boolean isQuotable() {
221 return Strings.PARAM_QUOTE_PATTERN.matcher(Strings.valueOf(getValue()))
222 .find();
223 }
224
225 /**
226 * @return Returns the name.
227 */
228 public final String getName() {
229 return name;
230 }
231
232 /**
233 * {@inheritDoc}
234 */
235 public final boolean equals(final Object arg0) {
236 if (arg0 instanceof Parameter) {
237 final Parameter p = (Parameter) arg0;
238 return new EqualsBuilder().append(getName(), p.getName())
239 .append(getValue(), p.getValue()).isEquals();
240 }
241 return super.equals(arg0);
242 }
243
244 /**
245 * {@inheritDoc}
246 */
247 public final int hashCode() {
248 // as parameter name is case-insensitive generate hash for uppercase..
249 return new HashCodeBuilder().append(getName().toUpperCase()).append(
250 getValue()).toHashCode();
251 }
252
253 /**
254 * Deep copy of parameter.
255 * @return new parameter
256 * @throws URISyntaxException where an invalid URI is encountered
257 */
258 public Parameter copy() throws URISyntaxException {
259 if (factory == null) {
260 throw new UnsupportedOperationException("No factory specified");
261 }
262 return factory.createParameter(getName(), getValue());
263 }
264 }

mercurial