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

Tue, 10 Feb 2015 19:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:58:00 +0100
changeset 4
45d57ecba757
parent 0
fb9019fb1bf7
permissions
-rw-r--r--

Upgrade the upgraded ical4j component to use org.apache.commons.lang3.

michael@0 1 /**
michael@0 2 * Copyright (c) 2012, Ben Fortuna
michael@0 3 * All rights reserved.
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 *
michael@0 9 * o Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 *
michael@0 12 * o Redistributions in binary form must reproduce the above copyright
michael@0 13 * notice, this list of conditions and the following disclaimer in the
michael@0 14 * documentation and/or other materials provided with the distribution.
michael@0 15 *
michael@0 16 * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0 17 * may be used to endorse or promote products derived from this software
michael@0 18 * without specific prior written permission.
michael@0 19 *
michael@0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32 package net.fortuna.ical4j.model;
michael@0 33
michael@0 34 import java.io.Serializable;
michael@0 35 import java.net.URISyntaxException;
michael@0 36 import java.util.ArrayList;
michael@0 37 import java.util.Collections;
michael@0 38 import java.util.Iterator;
michael@0 39 import java.util.List;
michael@0 40
michael@4 41 import org.apache.commons.lang3.ObjectUtils;
michael@4 42 import org.apache.commons.lang3.builder.HashCodeBuilder;
michael@0 43
michael@0 44 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
michael@0 45
michael@0 46 /**
michael@0 47 * $Id$ [Apr 5, 2004]
michael@0 48 *
michael@0 49 * Defines a list of iCalendar parameters. A parameter list may be specified as unmodifiable at instantiation - useful
michael@0 50 * for constant properties that you don't want modified.
michael@0 51 * @author Ben Fortuna
michael@0 52 */
michael@0 53 public class ParameterList implements Serializable {
michael@0 54
michael@0 55 private static final long serialVersionUID = -1913059830016450169L;
michael@0 56
michael@0 57 private final List parameters;
michael@0 58
michael@0 59 /**
michael@0 60 * Default constructor. Creates a modifiable parameter list.
michael@0 61 */
michael@0 62 public ParameterList() {
michael@0 63 this(false);
michael@0 64 }
michael@0 65
michael@0 66 /**
michael@0 67 * Constructor.
michael@0 68 * @param unmodifiable indicates whether the list should be mutable
michael@0 69 */
michael@0 70 public ParameterList(final boolean unmodifiable) {
michael@0 71 if (unmodifiable) {
michael@0 72 parameters = Collections.unmodifiableList(new ArrayList());
michael@0 73 }
michael@0 74 else {
michael@0 75 parameters = new CopyOnWriteArrayList();
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 /**
michael@0 80 * Creates a deep copy of the specified parameter list. That is, copies of all parameters in the specified list are
michael@0 81 * added to this list.
michael@0 82 * @param list a parameter list to copy parameters from
michael@0 83 * @param unmodifiable indicates whether the list should be mutable
michael@0 84 * @throws URISyntaxException where a parameter in the list specifies an invalid URI value
michael@0 85 */
michael@0 86 public ParameterList(final ParameterList list, final boolean unmodifiable)
michael@0 87 throws URISyntaxException {
michael@0 88
michael@0 89 final List parameterList = new CopyOnWriteArrayList();
michael@0 90 for (final Iterator i = list.iterator(); i.hasNext();) {
michael@0 91 final Parameter parameter = (Parameter) i.next();
michael@0 92 parameterList.add(parameter.copy());
michael@0 93 }
michael@0 94 if (unmodifiable) {
michael@0 95 parameters = Collections.unmodifiableList(parameterList);
michael@0 96 }
michael@0 97 else {
michael@0 98 parameters = parameterList;
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * {@inheritDoc}
michael@0 104 */
michael@0 105 public final String toString() {
michael@0 106 final StringBuffer buffer = new StringBuffer();
michael@0 107 for (final Iterator i = parameters.iterator(); i.hasNext();) {
michael@0 108 buffer.append(';');
michael@0 109 buffer.append(i.next().toString());
michael@0 110 }
michael@0 111 return buffer.toString();
michael@0 112 }
michael@0 113
michael@0 114 /**
michael@0 115 * Returns the first parameter with the specified name.
michael@0 116 * @param aName name of the parameter
michael@0 117 * @return the first matching parameter or null if no matching parameters
michael@0 118 */
michael@0 119 public final Parameter getParameter(final String aName) {
michael@0 120 for (final Iterator i = parameters.iterator(); i.hasNext();) {
michael@0 121 final Parameter p = (Parameter) i.next();
michael@0 122 if (aName.equalsIgnoreCase(p.getName())) {
michael@0 123 return p;
michael@0 124 }
michael@0 125 }
michael@0 126 return null;
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * Returns a list of parameters with the specified name.
michael@0 131 * @param name name of parameters to return
michael@0 132 * @return a parameter list
michael@0 133 */
michael@0 134 public final ParameterList getParameters(final String name) {
michael@0 135 final ParameterList list = new ParameterList();
michael@0 136 for (final Iterator i = parameters.iterator(); i.hasNext();) {
michael@0 137 final Parameter p = (Parameter) i.next();
michael@0 138 if (p.getName().equalsIgnoreCase(name)) {
michael@0 139 list.add(p);
michael@0 140 }
michael@0 141 }
michael@0 142 return list;
michael@0 143 }
michael@0 144
michael@0 145 /**
michael@0 146 * Add a parameter to the list. Note that this method will not remove existing parameters of the same type. To
michael@0 147 * achieve this use {
michael@0 148 * @link ParameterList#replace(Parameter) }
michael@0 149 * @param parameter the parameter to add
michael@0 150 * @return true
michael@0 151 * @see List#add(java.lang.Object)
michael@0 152 */
michael@0 153 public final boolean add(final Parameter parameter) {
michael@0 154 if (parameter == null) {
michael@0 155 throw new IllegalArgumentException("Trying to add null Parameter");
michael@0 156 }
michael@0 157 return parameters.add(parameter);
michael@0 158 }
michael@0 159
michael@0 160 /**
michael@0 161 * Replace any parameters of the same type with the one specified.
michael@0 162 * @param parameter parameter to add to this list in place of all others with the same name
michael@0 163 * @return true if successfully added to this list
michael@0 164 */
michael@0 165 public final boolean replace(final Parameter parameter) {
michael@0 166 for (final Iterator i = getParameters(parameter.getName()).iterator(); i.hasNext();) {
michael@0 167 remove((Parameter) i.next());
michael@0 168 }
michael@0 169 return add(parameter);
michael@0 170 }
michael@0 171
michael@0 172 /**
michael@0 173 * @return boolean indicates if the list is empty
michael@0 174 * @see List#isEmpty()
michael@0 175 */
michael@0 176 public final boolean isEmpty() {
michael@0 177 return parameters.isEmpty();
michael@0 178 }
michael@0 179
michael@0 180 /**
michael@0 181 * @return an iterator
michael@0 182 * @see List#iterator()
michael@0 183 */
michael@0 184 public final Iterator iterator() {
michael@0 185 return parameters.iterator();
michael@0 186 }
michael@0 187
michael@0 188 /**
michael@0 189 * Remove a parameter from the list.
michael@0 190 * @param parameter the parameter to remove
michael@0 191 * @return true if the list contained the specified parameter
michael@0 192 * @see List#remove(java.lang.Object)
michael@0 193 */
michael@0 194 public final boolean remove(final Parameter parameter) {
michael@0 195 return parameters.remove(parameter);
michael@0 196 }
michael@0 197
michael@0 198 /**
michael@0 199 * Remove all parameters with the specified name.
michael@0 200 * @param paramName the name of parameters to remove
michael@0 201 */
michael@0 202 public final void removeAll(final String paramName) {
michael@0 203 final ParameterList params = getParameters(paramName);
michael@0 204 parameters.removeAll(params.parameters);
michael@0 205 }
michael@0 206
michael@0 207 /**
michael@0 208 * @return the number of parameters in the list
michael@0 209 * @see List#size()
michael@0 210 */
michael@0 211 public final int size() {
michael@0 212 return parameters.size();
michael@0 213 }
michael@0 214
michael@0 215 /**
michael@0 216 * {@inheritDoc}
michael@0 217 */
michael@0 218 public final boolean equals(final Object arg0) {
michael@0 219 if (arg0 instanceof ParameterList) {
michael@0 220 final ParameterList p = (ParameterList) arg0;
michael@0 221 return ObjectUtils.equals(parameters, p.parameters);
michael@0 222 }
michael@0 223 return super.equals(arg0);
michael@0 224 }
michael@0 225
michael@0 226 /**
michael@0 227 * {@inheritDoc}
michael@0 228 */
michael@0 229 public final int hashCode() {
michael@0 230 return new HashCodeBuilder().append(parameters).toHashCode();
michael@0 231 }
michael@0 232 }

mercurial