source: osm/applications/editors/josm/plugins/opendata/includes/org/geotools/parameter/ParameterValueList.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 9.2 KB
Line 
1/*
2 * GeoTools - The Open Source Java GIS Toolkit
3 * http://geotools.org
4 *
5 * (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation;
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
17package org.geotools.parameter;
18
19import java.io.Serializable;
20import java.util.AbstractList;
21import java.util.List;
22import java.util.RandomAccess;
23
24import org.opengis.parameter.GeneralParameterDescriptor;
25import org.opengis.parameter.GeneralParameterValue;
26import org.opengis.parameter.InvalidParameterCardinalityException;
27import org.opengis.parameter.InvalidParameterNameException;
28import org.opengis.parameter.ParameterDescriptorGroup;
29import org.opengis.parameter.ParameterValue;
30
31import org.geotools.referencing.AbstractIdentifiedObject;
32import org.geotools.resources.i18n.ErrorKeys;
33import org.geotools.resources.i18n.Errors;
34
35
36/**
37 * The list to be returned by {@link Parameter#values}.
38 * This class performs check on the parameter value to be added or removed.
39 * This implementation supports {@link #add} and {@link #remove} operations.
40 *
41 * @since 2.1
42 * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/referencing/src/main/java/org/geotools/parameter/ParameterValueList.java $
43 * @version $Id: ParameterValueList.java 30641 2008-06-12 17:42:27Z acuster $
44 * @author Martin Desruisseaux (IRD)
45 */
46final class ParameterValueList extends AbstractList<GeneralParameterValue>
47 implements RandomAccess, Serializable
48{
49 /**
50 * Serial number for interoperability with different versions.
51 */
52 private static final long serialVersionUID = -7446077551686135264L;
53
54 /**
55 * The descriptor.
56 */
57 private final ParameterDescriptorGroup descriptor;
58
59 /**
60 * The parameter values for this list.
61 */
62 private final List<GeneralParameterValue> values;
63
64 /**
65 * Constructs a parameter list.
66 *
67 * @param descriptor The descriptor for this list.
68 * @param values The parameter values for this list.
69 */
70 public ParameterValueList(final ParameterDescriptorGroup descriptor,
71 final List<GeneralParameterValue> values)
72 {
73 this.descriptor = descriptor;
74 this.values = values;
75 }
76
77 /*
78 * CAUTION: Some methods are NOT forwarded to 'values', and this is on purpose!
79 * This include all modification methods (add, set, remove, etc.).
80 * We must rely on the default AbstractList implementation for them.
81 */
82 @Override public boolean isEmpty () {return values.isEmpty ( );}
83 public int size () {return values.size ( );}
84 public GeneralParameterValue get (int i) {return values.get (i);}
85 @Override public int indexOf (Object o) {return values.indexOf (o);}
86 @Override public int lastIndexOf(Object o) {return values.lastIndexOf(o);}
87 @Override public boolean equals (Object o) {return values.equals (o);}
88 @Override public int hashCode () {return values.hashCode ( );}
89 @Override public String toString () {return values.toString ( );}
90
91 /**
92 * Adds a {@linkplain ParameterValue parameter value} or an other
93 * {@linkplain ParameterGroup parameter group} to this group. If an existing parameter
94 * is already included for the same {@linkplain ParameterDescriptor#getName identifier},
95 * then there is a choice:
96 * <UL>
97 * <LI>For <code>{@linkplain GeneralParameterDescriptor#getMaximumOccurs maximumOccurs}
98 * == 1</code>, the new parameter will replace the existing parameter.</LI>
99 * <LI>For <code>{@linkplain GeneralParameterDescriptor#getMaximumOccurs maximumOccurs}
100 * &gt; 1</code>, the new parameter will be added. If adding the new parameter will
101 * increase the number past what is allowable by {@code maximumOccurs}, then
102 * an {@link IllegalStateException} will be thrown.</LI>
103 * </UL>
104 *
105 * @param parameter New parameter to be added to this group.
106 * @return {@code true} if this object changed as a result of this call.
107 * @throws IllegalArgumentException if the specified parameter is not allowable by the
108 * groups descriptor.
109 * @throws InvalidParameterCardinalityException if adding this parameter would result in
110 * more parameters than allowed by {@code maximumOccurs}.
111 */
112 @Override
113 public boolean add(final GeneralParameterValue parameter) {
114 modCount++;
115 final GeneralParameterDescriptor type = parameter.getDescriptor();
116 final List<GeneralParameterDescriptor> descriptors = descriptor.descriptors();
117 final String name = type.getName().getCode();
118 if (!descriptors.contains(type)) {
119 /*
120 * For a more accurate error message, check if the operation failed because
121 * the parameter name was not found, or the parameter descriptor doesn't matches.
122 */
123 for (final GeneralParameterDescriptor descriptor : descriptors) {
124 if (AbstractIdentifiedObject.nameMatches(descriptor, name)) {
125 /*
126 * Found a matching name. Consequently, the operation failed because
127 * the descriptor was illegal.
128 */
129 throw new IllegalArgumentException(Errors.format(
130 ErrorKeys.ILLEGAL_DESCRIPTOR_FOR_PARAMETER_$1, name));
131 }
132 }
133 /*
134 * Found no matching name. Consequently, the operation failed because the name
135 * was invalid.
136 */
137 final Object value;
138 if (parameter instanceof ParameterValue) {
139 value = ((ParameterValue) parameter).getValue();
140 } else {
141 value = "(group)";
142 }
143 throw new InvalidParameterNameException(Errors.format(
144 ErrorKeys.ILLEGAL_ARGUMENT_$2, name, value), name);
145 }
146 final int max = type.getMaximumOccurs();
147 if (max == 1) {
148 /*
149 * Optional or mandatory parameter - we will simply replace what is there.
150 */
151 for (int i=values.size(); --i>=0;) {
152 final GeneralParameterValue oldValue = values.get(i);
153 final GeneralParameterDescriptor oldDescriptor = oldValue.getDescriptor();
154 if (type.equals(oldDescriptor)) {
155 assert AbstractIdentifiedObject.nameMatches(oldDescriptor, name) : parameter;
156 final boolean same = parameter.equals(oldValue);
157 values.set(i, parameter);
158 return !same;
159 }
160 }
161 // Value will be added at the end of this method.
162 } else {
163 /*
164 * Parameter added (usually a group). Check the cardinality.
165 */
166 int count = 0;
167 for (final GeneralParameterValue value : values) {
168 if (AbstractIdentifiedObject.nameMatches(value.getDescriptor(), name)) {
169 count++;
170 }
171 }
172 if (count >= max) {
173 throw new InvalidParameterCardinalityException(Errors.format(
174 ErrorKeys.TOO_MANY_OCCURENCES_$2, name, count), name);
175 }
176 }
177 values.add(parameter);
178 return true;
179 }
180
181 /**
182 * Remove the value at the specified index.
183 *
184 * @param index The index of the value to remove.
185 */
186 @Override
187 public GeneralParameterValue remove(final int index) {
188 return remove(values.get(index).getDescriptor(), index);
189 }
190
191 /**
192 * Remove the value at the specified index.
193 *
194 * @param type The descriptor of the value to remove.
195 * @param index The index of the value to remove.
196 */
197 private GeneralParameterValue remove(final GeneralParameterDescriptor type, final int index) {
198 modCount++;
199 int count = 0;
200 final String name = type.getName().getCode();
201 for (final GeneralParameterValue value : values) {
202 if (AbstractIdentifiedObject.nameMatches(value.getDescriptor(), name)) {
203 count++;
204 }
205 }
206 final int min = type.getMinimumOccurs();
207 if (count <= min) {
208 final int max = type.getMaximumOccurs();
209 throw new InvalidParameterCardinalityException(Errors.format(
210 ErrorKeys.ILLEGAL_OCCURS_FOR_PARAMETER_$4, name, count-1, min, max), name);
211 }
212 final GeneralParameterValue value = values.remove(index);
213 assert value!=null && type.equals(value.getDescriptor()) : value;
214 return value;
215 }
216}
Note: See TracBrowser for help on using the repository browser.