001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.binding.method;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: /**
024: * An ordered list of method parameters.
025: *
026: * @author Keith
027: */
028: public class Parameters {
029:
030: /**
031: * Canonical instance for an empty parameters list.
032: */
033: public static final Parameters NONE = new Parameters(0);
034:
035: /**
036: * The list.
037: */
038: private List parameters;
039:
040: /**
041: * Create a parameter list of the default size (3 elements).
042: */
043: public Parameters() {
044: this (3);
045: }
046:
047: /**
048: * Create a parameter list with the specified size.
049: * @param size the size
050: */
051: public Parameters(int size) {
052: this .parameters = new ArrayList(size);
053: }
054:
055: /**
056: * Create a parameter list with one parameter.
057: * @param parameter the single parameter
058: */
059: public Parameters(Parameter parameter) {
060: this .parameters = new ArrayList(1);
061: add(parameter);
062: }
063:
064: /**
065: * Create a parameter list from the parameter array.
066: * @param parameters the parameters
067: */
068: public Parameters(Parameter[] parameters) {
069: this .parameters = new ArrayList(parameters.length);
070: addAll(parameters);
071: }
072:
073: /**
074: * Add a new parameter to this list.
075: * @param parameter the parameter
076: */
077: public boolean add(Parameter parameter) {
078: return this .parameters.add(parameter);
079: }
080:
081: /**
082: * Add new parameters to this list.
083: * @param parameters the parameters
084: */
085: public boolean addAll(Parameter[] parameters) {
086: return this .parameters.addAll(Arrays.asList(parameters));
087: }
088:
089: /**
090: * Return a parameter iterator.
091: * @return the iterator
092: */
093: public Iterator iterator() {
094: return parameters.iterator();
095: }
096:
097: /**
098: * Get an array containing each parameter type. The resulting array
099: * could contain null values if the corresponding parameters did
100: * not specify a parameter type.
101: * @return the types
102: */
103: public Class[] getTypesArray() {
104: int i = 0;
105: Class[] types = new Class[parameters.size()];
106: for (Iterator it = parameters.iterator(); it.hasNext();) {
107: Parameter param = (Parameter) it.next();
108: types[i] = param.getType();
109: i++;
110: }
111: return types;
112: }
113:
114: /**
115: * Returns the number of parameters in this list.
116: * @return the size
117: */
118: public int size() {
119: return parameters.size();
120: }
121:
122: /**
123: * Return the parameter at the provided index.
124: * @param index the parameter index
125: * @return the parameter at that index
126: * @throws IndexOutOfBoundsException if the provided index is out of bounds
127: */
128: public Parameter getParameter(int index)
129: throws IndexOutOfBoundsException {
130: return (Parameter) parameters.get(index);
131: }
132:
133: public boolean equals(Object obj) {
134: if (!(obj instanceof Parameters)) {
135: return false;
136: }
137: Parameters other = (Parameters) obj;
138: return parameters.equals(other.parameters);
139: }
140:
141: public int hashCode() {
142: return parameters.hashCode();
143: }
144:
145: public String toString() {
146: return parameters.toString();
147: }
148: }
|