001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util;
017:
018: // J2SE dependencies
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: // OpenGIS dependencies
024: import org.opengis.util.Cloneable;
025:
026: // Geotools dependencies
027: import org.geotools.resources.Utilities;
028: import org.geotools.resources.i18n.Errors;
029: import org.geotools.resources.i18n.ErrorKeys;
030:
031: /**
032: * Acts as a typed {@link java.util.List} while we wait for Java 5.0.
033: *
034: * @since 2.1
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/CheckedArrayList.java $
036: * @version $Id: CheckedArrayList.java 25193 2007-04-18 13:37:38Z desruisseaux $
037: * @author Jody Garnett (Refractions Research)
038: * @author Martin Desruisseaux
039: *
040: * @todo Provides synchronization facility on arbitrary lock, for use with the metadata package.
041: * The lock would be the metadata that owns this collection. Be carefull to update the lock
042: * after a clone (this work may be done in {@code MetadataEntity.unmodifiable(Object)}).
043: */
044: public class CheckedArrayList extends ArrayList implements
045: CheckedCollection, Cloneable {
046: /**
047: * Serial version UID for compatibility with different versions.
048: */
049: private static final long serialVersionUID = -587331971085094268L;
050:
051: /**
052: * The element type.
053: */
054: private final Class type;
055:
056: /**
057: * Constructs a list of the specified type.
058: *
059: * @param type The element type (should not be null).
060: */
061: public CheckedArrayList(final Class type) {
062: super ();
063: this .type = type;
064: ensureNonNull();
065: }
066:
067: /**
068: * Constructs a list of the specified type and initial capacity.
069: *
070: * @param type The element type (should not be null).
071: * @param capacity The initial capacity.
072: *
073: * @since 2.4
074: */
075: public CheckedArrayList(final Class type, final int capacity) {
076: super (capacity);
077: this .type = type;
078: ensureNonNull();
079: }
080:
081: /**
082: * Returns the element type given at construction time.
083: *
084: * @since 2.4
085: */
086: public Class getElementType() {
087: return type;
088: }
089:
090: /**
091: * Make sure that {@link #type} is non-null.
092: */
093: private void ensureNonNull() {
094: if (type == null) {
095: throw new IllegalArgumentException(Errors.format(
096: ErrorKeys.NULL_ARGUMENT_$1, "type"));
097: }
098: }
099:
100: /**
101: * Checks the type of the specified object. The default implementation ensure
102: * that the object is assignable to the type specified at construction time.
103: *
104: * @param element the object to check, or {@code null}.
105: * @throws IllegalArgumentException if the specified element is not of the expected type.
106: */
107: protected void ensureValidType(final Object element)
108: throws IllegalArgumentException {
109: if (element != null && !type.isInstance(element)) {
110: throw new IllegalArgumentException(Errors.format(
111: ErrorKeys.ILLEGAL_CLASS_$2, Utilities
112: .getShortClassName(element), Utilities
113: .getShortName(type)));
114: }
115: }
116:
117: /**
118: * Checks the type of all elements in the specified collection.
119: *
120: * @param collection the collection to check, or {@code null}.
121: * @throws IllegalArgumentException if at least one element is not of the expected type.
122: */
123: private void ensureValid(final Collection collection)
124: throws IllegalArgumentException {
125: if (collection != null) {
126: for (final Iterator it = collection.iterator(); it
127: .hasNext();) {
128: ensureValidType(it.next());
129: }
130: }
131: }
132:
133: /**
134: * Replaces the element at the specified position in this list with the specified element.
135: *
136: * @param index index of element to replace.
137: * @param element element to be stored at the specified position.
138: * @return the element previously at the specified position.
139: * @throws IndexOutOfBoundsException if index out of range.
140: * @throws IllegalArgumentException if the specified element is not of the expected type.
141: */
142: public Object set(final int index, final Object element) {
143: ensureValidType(element);
144: return super .set(index, element);
145: }
146:
147: /**
148: * Appends the specified element to the end of this list.
149: *
150: * @param element element to be appended to this list.
151: * @return always {@code true}.
152: * @throws IllegalArgumentException if the specified element is not of the expected type.
153: */
154: public boolean add(final Object element) {
155: ensureValidType(element);
156: return super .add(element);
157: }
158:
159: /**
160: * Inserts the specified element at the specified position in this list.
161: *
162: * @param index index at which the specified element is to be inserted.
163: * @param element element to be inserted.
164: * @throws IndexOutOfBoundsException if index out of range.
165: * @throws IllegalArgumentException if the specified element is not of the expected type.
166: */
167: public void add(final int index, final Object element) {
168: ensureValidType(element);
169: super .add(index, element);
170: }
171:
172: /**
173: * Appends all of the elements in the specified collection to the end of this list,
174: * in the order that they are returned by the specified Collection's Iterator.
175: *
176: * @param collection the elements to be inserted into this list.
177: * @return {@code true} if this list changed as a result of the call.
178: * @throws IllegalArgumentException if at least one element is not of the expected type.
179: */
180: public boolean addAll(final Collection collection) {
181: ensureValid(collection);
182: return super .addAll(collection);
183: }
184:
185: /**
186: * Inserts all of the elements in the specified collection into this list,
187: * starting at the specified position.
188: *
189: * @param index index at which to insert first element fromm the specified collection.
190: * @param collection elements to be inserted into this list.
191: * @return {@code true} if this list changed as a result of the call.
192: * @throws IllegalArgumentException if at least one element is not of the expected type.
193: */
194: public boolean addAll(final int index, final Collection collection) {
195: ensureValid(collection);
196: return super.addAll(index, collection);
197: }
198: }
|