1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/ParameterList.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,232 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model; 1.36 + 1.37 +import java.io.Serializable; 1.38 +import java.net.URISyntaxException; 1.39 +import java.util.ArrayList; 1.40 +import java.util.Collections; 1.41 +import java.util.Iterator; 1.42 +import java.util.List; 1.43 + 1.44 +import org.apache.commons.lang.ObjectUtils; 1.45 +import org.apache.commons.lang.builder.HashCodeBuilder; 1.46 + 1.47 +import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; 1.48 + 1.49 +/** 1.50 + * $Id$ [Apr 5, 2004] 1.51 + * 1.52 + * Defines a list of iCalendar parameters. A parameter list may be specified as unmodifiable at instantiation - useful 1.53 + * for constant properties that you don't want modified. 1.54 + * @author Ben Fortuna 1.55 + */ 1.56 +public class ParameterList implements Serializable { 1.57 + 1.58 + private static final long serialVersionUID = -1913059830016450169L; 1.59 + 1.60 + private final List parameters; 1.61 + 1.62 + /** 1.63 + * Default constructor. Creates a modifiable parameter list. 1.64 + */ 1.65 + public ParameterList() { 1.66 + this(false); 1.67 + } 1.68 + 1.69 + /** 1.70 + * Constructor. 1.71 + * @param unmodifiable indicates whether the list should be mutable 1.72 + */ 1.73 + public ParameterList(final boolean unmodifiable) { 1.74 + if (unmodifiable) { 1.75 + parameters = Collections.unmodifiableList(new ArrayList()); 1.76 + } 1.77 + else { 1.78 + parameters = new CopyOnWriteArrayList(); 1.79 + } 1.80 + } 1.81 + 1.82 + /** 1.83 + * Creates a deep copy of the specified parameter list. That is, copies of all parameters in the specified list are 1.84 + * added to this list. 1.85 + * @param list a parameter list to copy parameters from 1.86 + * @param unmodifiable indicates whether the list should be mutable 1.87 + * @throws URISyntaxException where a parameter in the list specifies an invalid URI value 1.88 + */ 1.89 + public ParameterList(final ParameterList list, final boolean unmodifiable) 1.90 + throws URISyntaxException { 1.91 + 1.92 + final List parameterList = new CopyOnWriteArrayList(); 1.93 + for (final Iterator i = list.iterator(); i.hasNext();) { 1.94 + final Parameter parameter = (Parameter) i.next(); 1.95 + parameterList.add(parameter.copy()); 1.96 + } 1.97 + if (unmodifiable) { 1.98 + parameters = Collections.unmodifiableList(parameterList); 1.99 + } 1.100 + else { 1.101 + parameters = parameterList; 1.102 + } 1.103 + } 1.104 + 1.105 + /** 1.106 + * {@inheritDoc} 1.107 + */ 1.108 + public final String toString() { 1.109 + final StringBuffer buffer = new StringBuffer(); 1.110 + for (final Iterator i = parameters.iterator(); i.hasNext();) { 1.111 + buffer.append(';'); 1.112 + buffer.append(i.next().toString()); 1.113 + } 1.114 + return buffer.toString(); 1.115 + } 1.116 + 1.117 + /** 1.118 + * Returns the first parameter with the specified name. 1.119 + * @param aName name of the parameter 1.120 + * @return the first matching parameter or null if no matching parameters 1.121 + */ 1.122 + public final Parameter getParameter(final String aName) { 1.123 + for (final Iterator i = parameters.iterator(); i.hasNext();) { 1.124 + final Parameter p = (Parameter) i.next(); 1.125 + if (aName.equalsIgnoreCase(p.getName())) { 1.126 + return p; 1.127 + } 1.128 + } 1.129 + return null; 1.130 + } 1.131 + 1.132 + /** 1.133 + * Returns a list of parameters with the specified name. 1.134 + * @param name name of parameters to return 1.135 + * @return a parameter list 1.136 + */ 1.137 + public final ParameterList getParameters(final String name) { 1.138 + final ParameterList list = new ParameterList(); 1.139 + for (final Iterator i = parameters.iterator(); i.hasNext();) { 1.140 + final Parameter p = (Parameter) i.next(); 1.141 + if (p.getName().equalsIgnoreCase(name)) { 1.142 + list.add(p); 1.143 + } 1.144 + } 1.145 + return list; 1.146 + } 1.147 + 1.148 + /** 1.149 + * Add a parameter to the list. Note that this method will not remove existing parameters of the same type. To 1.150 + * achieve this use { 1.151 + * @link ParameterList#replace(Parameter) } 1.152 + * @param parameter the parameter to add 1.153 + * @return true 1.154 + * @see List#add(java.lang.Object) 1.155 + */ 1.156 + public final boolean add(final Parameter parameter) { 1.157 + if (parameter == null) { 1.158 + throw new IllegalArgumentException("Trying to add null Parameter"); 1.159 + } 1.160 + return parameters.add(parameter); 1.161 + } 1.162 + 1.163 + /** 1.164 + * Replace any parameters of the same type with the one specified. 1.165 + * @param parameter parameter to add to this list in place of all others with the same name 1.166 + * @return true if successfully added to this list 1.167 + */ 1.168 + public final boolean replace(final Parameter parameter) { 1.169 + for (final Iterator i = getParameters(parameter.getName()).iterator(); i.hasNext();) { 1.170 + remove((Parameter) i.next()); 1.171 + } 1.172 + return add(parameter); 1.173 + } 1.174 + 1.175 + /** 1.176 + * @return boolean indicates if the list is empty 1.177 + * @see List#isEmpty() 1.178 + */ 1.179 + public final boolean isEmpty() { 1.180 + return parameters.isEmpty(); 1.181 + } 1.182 + 1.183 + /** 1.184 + * @return an iterator 1.185 + * @see List#iterator() 1.186 + */ 1.187 + public final Iterator iterator() { 1.188 + return parameters.iterator(); 1.189 + } 1.190 + 1.191 + /** 1.192 + * Remove a parameter from the list. 1.193 + * @param parameter the parameter to remove 1.194 + * @return true if the list contained the specified parameter 1.195 + * @see List#remove(java.lang.Object) 1.196 + */ 1.197 + public final boolean remove(final Parameter parameter) { 1.198 + return parameters.remove(parameter); 1.199 + } 1.200 + 1.201 + /** 1.202 + * Remove all parameters with the specified name. 1.203 + * @param paramName the name of parameters to remove 1.204 + */ 1.205 + public final void removeAll(final String paramName) { 1.206 + final ParameterList params = getParameters(paramName); 1.207 + parameters.removeAll(params.parameters); 1.208 + } 1.209 + 1.210 + /** 1.211 + * @return the number of parameters in the list 1.212 + * @see List#size() 1.213 + */ 1.214 + public final int size() { 1.215 + return parameters.size(); 1.216 + } 1.217 + 1.218 + /** 1.219 + * {@inheritDoc} 1.220 + */ 1.221 + public final boolean equals(final Object arg0) { 1.222 + if (arg0 instanceof ParameterList) { 1.223 + final ParameterList p = (ParameterList) arg0; 1.224 + return ObjectUtils.equals(parameters, p.parameters); 1.225 + } 1.226 + return super.equals(arg0); 1.227 + } 1.228 + 1.229 + /** 1.230 + * {@inheritDoc} 1.231 + */ 1.232 + public final int hashCode() { 1.233 + return new HashCodeBuilder().append(parameters).toHashCode(); 1.234 + } 1.235 +}