michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model; michael@0: michael@0: import java.io.Serializable; michael@0: import java.net.URISyntaxException; michael@0: import java.util.ArrayList; michael@0: import java.util.Collections; michael@0: import java.util.Iterator; michael@0: import java.util.List; michael@0: michael@4: import org.apache.commons.lang3.ObjectUtils; michael@4: import org.apache.commons.lang3.builder.HashCodeBuilder; michael@0: michael@0: import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; michael@0: michael@0: /** michael@0: * $Id$ [Apr 5, 2004] michael@0: * michael@0: * Defines a list of iCalendar parameters. A parameter list may be specified as unmodifiable at instantiation - useful michael@0: * for constant properties that you don't want modified. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class ParameterList implements Serializable { michael@0: michael@0: private static final long serialVersionUID = -1913059830016450169L; michael@0: michael@0: private final List parameters; michael@0: michael@0: /** michael@0: * Default constructor. Creates a modifiable parameter list. michael@0: */ michael@0: public ParameterList() { michael@0: this(false); michael@0: } michael@0: michael@0: /** michael@0: * Constructor. michael@0: * @param unmodifiable indicates whether the list should be mutable michael@0: */ michael@0: public ParameterList(final boolean unmodifiable) { michael@0: if (unmodifiable) { michael@0: parameters = Collections.unmodifiableList(new ArrayList()); michael@0: } michael@0: else { michael@0: parameters = new CopyOnWriteArrayList(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Creates a deep copy of the specified parameter list. That is, copies of all parameters in the specified list are michael@0: * added to this list. michael@0: * @param list a parameter list to copy parameters from michael@0: * @param unmodifiable indicates whether the list should be mutable michael@0: * @throws URISyntaxException where a parameter in the list specifies an invalid URI value michael@0: */ michael@0: public ParameterList(final ParameterList list, final boolean unmodifiable) michael@0: throws URISyntaxException { michael@0: michael@0: final List parameterList = new CopyOnWriteArrayList(); michael@0: for (final Iterator i = list.iterator(); i.hasNext();) { michael@0: final Parameter parameter = (Parameter) i.next(); michael@0: parameterList.add(parameter.copy()); michael@0: } michael@0: if (unmodifiable) { michael@0: parameters = Collections.unmodifiableList(parameterList); michael@0: } michael@0: else { michael@0: parameters = parameterList; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final String toString() { michael@0: final StringBuffer buffer = new StringBuffer(); michael@0: for (final Iterator i = parameters.iterator(); i.hasNext();) { michael@0: buffer.append(';'); michael@0: buffer.append(i.next().toString()); michael@0: } michael@0: return buffer.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the first parameter with the specified name. michael@0: * @param aName name of the parameter michael@0: * @return the first matching parameter or null if no matching parameters michael@0: */ michael@0: public final Parameter getParameter(final String aName) { michael@0: for (final Iterator i = parameters.iterator(); i.hasNext();) { michael@0: final Parameter p = (Parameter) i.next(); michael@0: if (aName.equalsIgnoreCase(p.getName())) { michael@0: return p; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * Returns a list of parameters with the specified name. michael@0: * @param name name of parameters to return michael@0: * @return a parameter list michael@0: */ michael@0: public final ParameterList getParameters(final String name) { michael@0: final ParameterList list = new ParameterList(); michael@0: for (final Iterator i = parameters.iterator(); i.hasNext();) { michael@0: final Parameter p = (Parameter) i.next(); michael@0: if (p.getName().equalsIgnoreCase(name)) { michael@0: list.add(p); michael@0: } michael@0: } michael@0: return list; michael@0: } michael@0: michael@0: /** michael@0: * Add a parameter to the list. Note that this method will not remove existing parameters of the same type. To michael@0: * achieve this use { michael@0: * @link ParameterList#replace(Parameter) } michael@0: * @param parameter the parameter to add michael@0: * @return true michael@0: * @see List#add(java.lang.Object) michael@0: */ michael@0: public final boolean add(final Parameter parameter) { michael@0: if (parameter == null) { michael@0: throw new IllegalArgumentException("Trying to add null Parameter"); michael@0: } michael@0: return parameters.add(parameter); michael@0: } michael@0: michael@0: /** michael@0: * Replace any parameters of the same type with the one specified. michael@0: * @param parameter parameter to add to this list in place of all others with the same name michael@0: * @return true if successfully added to this list michael@0: */ michael@0: public final boolean replace(final Parameter parameter) { michael@0: for (final Iterator i = getParameters(parameter.getName()).iterator(); i.hasNext();) { michael@0: remove((Parameter) i.next()); michael@0: } michael@0: return add(parameter); michael@0: } michael@0: michael@0: /** michael@0: * @return boolean indicates if the list is empty michael@0: * @see List#isEmpty() michael@0: */ michael@0: public final boolean isEmpty() { michael@0: return parameters.isEmpty(); michael@0: } michael@0: michael@0: /** michael@0: * @return an iterator michael@0: * @see List#iterator() michael@0: */ michael@0: public final Iterator iterator() { michael@0: return parameters.iterator(); michael@0: } michael@0: michael@0: /** michael@0: * Remove a parameter from the list. michael@0: * @param parameter the parameter to remove michael@0: * @return true if the list contained the specified parameter michael@0: * @see List#remove(java.lang.Object) michael@0: */ michael@0: public final boolean remove(final Parameter parameter) { michael@0: return parameters.remove(parameter); michael@0: } michael@0: michael@0: /** michael@0: * Remove all parameters with the specified name. michael@0: * @param paramName the name of parameters to remove michael@0: */ michael@0: public final void removeAll(final String paramName) { michael@0: final ParameterList params = getParameters(paramName); michael@0: parameters.removeAll(params.parameters); michael@0: } michael@0: michael@0: /** michael@0: * @return the number of parameters in the list michael@0: * @see List#size() michael@0: */ michael@0: public final int size() { michael@0: return parameters.size(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean equals(final Object arg0) { michael@0: if (arg0 instanceof ParameterList) { michael@0: final ParameterList p = (ParameterList) arg0; michael@0: return ObjectUtils.equals(parameters, p.parameters); michael@0: } michael@0: return super.equals(arg0); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final int hashCode() { michael@0: return new HashCodeBuilder().append(parameters).toHashCode(); michael@0: } michael@0: }