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.LinkedHashSet;
020:
021: // OpenGIS dependencies
022: import org.opengis.util.Cloneable;
023:
024: // Geotools dependencies
025: import org.geotools.resources.Utilities;
026: import org.geotools.resources.i18n.Errors;
027: import org.geotools.resources.i18n.ErrorKeys;
028:
029: /**
030: * Acts as a typed {@link java.util.Set} while we wait for Java 5.0.
031: *
032: * @since 2.1
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/CheckedHashSet.java $
034: * @version $Id: CheckedHashSet.java 25193 2007-04-18 13:37:38Z desruisseaux $
035: * @author Jody Garnett (Refractions Research)
036: * @author Martin Desruisseaux
037: *
038: * @todo Provides synchronization facility on arbitrary lock, for use with the metadata package.
039: * The lock would be the metadata that owns this collection. Be carefull to update the lock
040: * after a clone (this work may be done in {@code MetadataEntity.unmodifiable(Object)}).
041: */
042: public class CheckedHashSet extends LinkedHashSet implements
043: CheckedCollection, Cloneable {
044: /**
045: * Serial version UID for compatibility with different versions.
046: */
047: private static final long serialVersionUID = -9014541457174735097L;
048:
049: /**
050: * The element type.
051: */
052: private final Class type;
053:
054: /**
055: * Constructs a set of the specified type.
056: *
057: * @param type The element type (should not be null).
058: */
059: public CheckedHashSet(final Class type) {
060: super ();
061: this .type = type;
062: ensureNonNull();
063: }
064:
065: /**
066: * Constructs a set of the specified type and initial capacity.
067: *
068: * @param type The element type (should not be null).
069: * @param capacity The initial capacity.
070: *
071: * @since 2.4
072: */
073: public CheckedHashSet(final Class type, final int capacity) {
074: super (capacity);
075: this .type = type;
076: ensureNonNull();
077: }
078:
079: /**
080: * Returns the element type given at construction time.
081: *
082: * @since 2.4
083: */
084: public Class getElementType() {
085: return type;
086: }
087:
088: /**
089: * Make sure that {@link #type} is non-null.
090: */
091: private void ensureNonNull() {
092: if (type == null) {
093: throw new IllegalArgumentException(Errors.format(
094: ErrorKeys.NULL_ARGUMENT_$1, "type"));
095: }
096: }
097:
098: /**
099: * Checks the type of the specified object. The default implementation ensure
100: * that the object is assignable to the type specified at construction time.
101: *
102: * @param element the object to check, or {@code null}.
103: * @throws IllegalArgumentException if the specified element is not of the expected type.
104: */
105: protected void ensureValidType(final Object element)
106: throws IllegalArgumentException {
107: if (element != null && !type.isInstance(element)) {
108: throw new IllegalArgumentException(Errors.format(
109: ErrorKeys.ILLEGAL_CLASS_$2, Utilities
110: .getShortClassName(element), Utilities
111: .getShortName(type)));
112: }
113: }
114:
115: /**
116: * Adds the specified element to this set if it is not already present.
117: *
118: * @param element element to be added to this set.
119: * @return {@code true} if the set did not already contain the specified element.
120: * @throws IllegalArgumentException if the specified element is not of the expected type.
121: */
122: public boolean add(final Object element) {
123: ensureValidType(element);
124: return super.add(element);
125: }
126: }
|