Author: desruisseaux
Date: Tue Apr 23 09:05:42 2013
New Revision: 1470856
URL: http://svn.apache.org/r1470856
Log:
Remove the check for write permission in CheckedArrayList and CheckedHashSet.
It was not needed because ModifiableMetadata creates unmodifiable copies of
those collections when 'freeze()' is invoked anyway.
Modified:
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedHashSet.java
Modified:
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java?rev=1470856&r1=1470855&r2=1470856&view=diff
==============================================================================
---
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
[UTF-8] (original)
+++
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
[UTF-8] Tue Apr 23 09:05:42 2013
@@ -28,9 +28,7 @@ import org.apache.sis.util.resources.Err
import org.apache.sis.internal.util.CheckedHashSet;
import org.apache.sis.internal.util.CheckedArrayList;
-import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
import static org.apache.sis.util.collection.Containers.isNullOrEmpty;
-import static org.apache.sis.util.collection.Containers.hashMapCapacity;
import static org.apache.sis.internal.jaxb.MarshalContext.isMarshalling;
@@ -85,6 +83,12 @@ import static org.apache.sis.internal.ja
@ThreadSafe
public abstract class ModifiableMetadata extends AbstractMetadata implements
Cloneable {
/**
+ * Initial capacity of lists and sets. We use a small value because those
+ * collections will typically contain few elements (often just a
singleton).
+ */
+ private static final int INITIAL_CAPACITY = 4;
+
+ /**
* A null implementation for the {@link #FREEZING} constant.
*/
private static final class Null extends ModifiableMetadata {
@@ -266,7 +270,7 @@ public abstract class ModifiableMetadata
if (target != null) {
target.clear();
} else {
- target = new MutableList<>(elementType, source.size());
+ target = new CheckedArrayList<>(elementType,
source.size());
}
target.addAll(source);
}
@@ -314,7 +318,7 @@ public abstract class ModifiableMetadata
if (target != null) {
target.clear();
} else {
- target = new MutableSet<>(elementType, source.size());
+ target = new CheckedHashSet<>(elementType, source.size());
}
target.addAll(source);
}
@@ -379,9 +383,9 @@ public abstract class ModifiableMetadata
} else {
final int capacity = source.size();
if (useSet(elementType)) {
- target = new MutableSet<>(elementType, capacity);
+ target = new CheckedHashSet<>(elementType, capacity);
} else {
- target = new MutableList<>(elementType, capacity);
+ target = new CheckedArrayList<>(elementType, capacity);
}
}
target.addAll(source);
@@ -405,7 +409,7 @@ public abstract class ModifiableMetadata
if (isNullOrEmpty(source)) {
return null;
}
- final List<E> target = new MutableList<>(elementType, source.size());
+ final List<E> target = new CheckedArrayList<>(elementType,
source.size());
target.addAll(source);
return target;
}
@@ -425,7 +429,7 @@ public abstract class ModifiableMetadata
if (isNullOrEmpty(source)) {
return null;
}
- final Set<E> target = new MutableSet<>(elementType, source.size());
+ final Set<E> target = new CheckedHashSet<>(elementType, source.size());
target.addAll(source);
return target;
}
@@ -451,9 +455,9 @@ public abstract class ModifiableMetadata
final Collection<E> target;
final int capacity = source.size();
if (useSet(elementType)) {
- target = new MutableSet<>(elementType, capacity);
+ target = new CheckedHashSet<>(elementType, capacity);
} else {
- target = new MutableList<>(elementType, capacity);
+ target = new CheckedArrayList<>(elementType, capacity);
}
target.addAll(source);
return target;
@@ -478,9 +482,9 @@ public abstract class ModifiableMetadata
}
final Collection<E> collection;
if (useSet(elementType)) {
- collection = new MutableSet<>(elementType);
+ collection = new CheckedHashSet<>(elementType, INITIAL_CAPACITY);
} else {
- collection = new MutableList<>(elementType);
+ collection = new CheckedArrayList<>(elementType, INITIAL_CAPACITY);
}
collection.add(value);
return collection;
@@ -503,7 +507,7 @@ public abstract class ModifiableMetadata
return null;
}
if (isModifiable()) {
- return new MutableList<>(elementType);
+ return new CheckedArrayList<>(elementType, INITIAL_CAPACITY);
}
return Collections.emptyList();
}
@@ -525,7 +529,7 @@ public abstract class ModifiableMetadata
return null;
}
if (isModifiable()) {
- return new MutableSet<>(elementType);
+ return new CheckedHashSet<>(elementType, INITIAL_CAPACITY);
}
return Collections.emptySet();
}
@@ -558,13 +562,13 @@ public abstract class ModifiableMetadata
final boolean isModifiable = isModifiable();
if (useSet(elementType)) {
if (isModifiable) {
- return new MutableSet<>(elementType);
+ return new CheckedHashSet<>(elementType, INITIAL_CAPACITY);
} else {
return Collections.emptySet();
}
} else {
if (isModifiable) {
- return new MutableList<>(elementType);
+ return new CheckedArrayList<>(elementType, INITIAL_CAPACITY);
} else {
return Collections.emptyList();
}
@@ -572,58 +576,6 @@ public abstract class ModifiableMetadata
}
/**
- * A set checking element validity and write permission before to change
any value.
- */
- private final class MutableSet<E> extends CheckedHashSet<E> {
- private static final long serialVersionUID = 3032602282358733056L;
-
- MutableSet(Class<E> type) {
- super(type, 4); // Use a small capacity because we typically have
few elements.
- }
-
- MutableSet(Class<E> type, int capacity) {
- super(type, hashMapCapacity(capacity));
- }
-
- @Override
- protected void checkWritePermission() throws
UnsupportedOperationException {
- ModifiableMetadata.this.checkWritePermission();
- }
-
- @Override
- protected void ensureValid(final E element) throws
IllegalArgumentException {
- ensureNonNull("element", element);
- super.ensureValid(element);
- }
- }
-
- /**
- * A list checking element validity and write permission before to change
any value.
- */
- private final class MutableList<E> extends CheckedArrayList<E> {
- private static final long serialVersionUID = 5800381255701183058L;
-
- MutableList(Class<E> type) {
- super(type, 4); // Use a small capacity because we typically have
few elements.
- }
-
- MutableList(Class<E> type, int capacity) {
- super(type, capacity);
- }
-
- @Override
- protected void checkWritePermission() throws
UnsupportedOperationException {
- ModifiableMetadata.this.checkWritePermission();
- }
-
- @Override
- protected void ensureValid(final E element) throws
IllegalArgumentException {
- ensureNonNull("element", element);
- super.ensureValid(element);
- }
- }
-
- /**
* Returns {@code true} if we should use a {@link Set} instead than a
{@link List}
* for elements of the given type.
*/
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java?rev=1470856&r1=1470855&r2=1470856&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] (original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] Tue Apr 23 09:05:42 2013
@@ -17,13 +17,9 @@
package org.apache.sis.internal.util;
import java.util.List;
-import java.util.Iterator;
-import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.NoSuchElementException;
-import org.apache.sis.util.Decorator;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.collection.CheckedContainer;
@@ -35,12 +31,11 @@ import static org.apache.sis.util.Argume
* The type checks are performed at run-time in addition to the compile-time
checks.
*
* <p>Using this class is similar to wrapping an {@link ArrayList} using the
methods provided
- * in the standard {@link Collections} class, except for the following
advantages:</p>
+ * in the standard {@link Collections} class, except for the following
differences:</p>
*
* <ul>
* <li>Avoid one level of indirection.</li>
- * <li>Checks for write permission.</li>
- * <li>Overrideable methods for controlling the type checks and write
permission checks.</li>
+ * <li>Does not accept null elements.</li>
* </ul>
*
* @param <E> The type of elements in the list.
@@ -52,7 +47,7 @@ import static org.apache.sis.util.Argume
*
* @see Collections#checkedList(List, Class)
*/
-public class CheckedArrayList<E> extends ArrayList<E> implements
CheckedContainer<E>, Cloneable {
+public final class CheckedArrayList<E> extends ArrayList<E> implements
CheckedContainer<E> {
/**
* Serial version UID for compatibility with different versions.
*/
@@ -95,16 +90,15 @@ public class CheckedArrayList<E> extends
}
/**
- * Ensures that the given element can be added to this list.
- * The default implementation ensures that the object is {@code null} or
assignable
- * to the type specified at construction time. Subclasses can override
this method
- * if they need to perform additional checks.
+ * Ensures that the given element is non-null and assignable to the type
+ * specified at construction time.
*
* @param element the object to check, or {@code null}.
* @throws IllegalArgumentException if the specified element can not be
added to this list.
*/
- protected void ensureValid(final E element) throws
IllegalArgumentException {
- if (element != null && !type.isInstance(element)) {
+ private void ensureValid(final E element) throws IllegalArgumentException {
+ if (!type.isInstance(element)) {
+ ensureNonNull("element", element);
throw new IllegalArgumentException(Errors.format(
Errors.Keys.IllegalArgumentClass_3, "element", type,
element.getClass()));
}
@@ -123,143 +117,6 @@ public class CheckedArrayList<E> extends
}
/**
- * Checks if changes in this list are allowed. This method is
automatically invoked before any
- * operation that may change the content. If the write operation is
allowed, then this method
- * shall returns normally. Otherwise an {@link
UnsupportedOperationException} is thrown.
- *
- * <p>The default implementation does nothing, thus allowing this list to
be modified.
- * Subclasses can override this method if they want to control write
permissions.</p>
- *
- * @throws UnsupportedOperationException if this list is unmodifiable.
- */
- protected void checkWritePermission() throws UnsupportedOperationException
{
- }
-
- /**
- * An iterator with a check for write permission prior element removal.
- * This class wraps the iterator provided by {@link ArrayList#iterator()},
and is
- * also the base class for the wrapper around {@link
ArrayList#listIterator()}.
- *
- * @see CheckedArrayList#iterator()
- */
- @Decorator(Iterator.class)
- private class Iter<I extends Iterator<E>> implements Iterator<E> {
- /** The {@link ArrayList} iterator. */
- protected final I iterator;
-
- /** Creates a new wrapper for the given {@link ArrayList} iterator. */
- Iter(final I iterator) {
- this.iterator = iterator;
- }
-
- /** Returns {@code true} if there is more elements in the iteration. */
- @Override
- public final boolean hasNext() {
- return iterator.hasNext();
- }
-
- /** Returns the next element in the iteration. */
- @Override
- public final E next() throws NoSuchElementException {
- return iterator.next();
- }
-
- /** Removes the previous element if the enclosing {@link
CheckedArrayList} allows write operations. */
- @Override
- public final void remove() throws UnsupportedOperationException {
- checkWritePermission();
- iterator.remove();
- }
- }
-
- /**
- * A list iterator with a check for write permission prior element removal.
- * This class wraps the iterator provided by {@link
ArrayList#listIterator()}.
- *
- * @see CheckedArrayList#listIterator()
- * @see CheckedArrayList#listIterator(int)
- */
- @Decorator(ListIterator.class)
- private class ListIter extends Iter<ListIterator<E>> implements
ListIterator<E> {
- /** Creates a new wrapper for the given {@link ArrayList} list
iterator. */
- ListIter(final ListIterator<E> iterator) {
- super(iterator);
- }
-
- /** Returns the index of the element to be returned by {@link
#next()}. */
- @Override
- public int nextIndex() {
- return iterator.nextIndex();
- }
-
- /** Returns the index of the element to be returned by {@link
#previous()}. */
- @Override
- public int previousIndex() {
- return iterator.previousIndex();
- }
-
- /** Returns {@code true} if there is elements before current position.
*/
- @Override
- public boolean hasPrevious() {
- return iterator.hasPrevious();
- }
-
- /** Returns the previous element in the iteration. */
- @Override
- public E previous() throws NoSuchElementException {
- return iterator.previous();
- }
-
- /** See the {@link CheckedArrayList#set(int, Object)} method contract.
*/
- @Override
- public void set(final E element) throws IllegalArgumentException,
UnsupportedOperationException {
- ensureValid(element);
- checkWritePermission();
- iterator.set(element);
- }
-
- /** See the {@link CheckedArrayList#add(Object)} method contract. */
- @Override
- public void add(final E element) throws IllegalArgumentException,
UnsupportedOperationException {
- ensureValid(element);
- checkWritePermission();
- iterator.add(element);
- }
- }
-
- /**
- * Returns an iterator over the elements in this list.
- * The returned iterator will support {@linkplain Iterator#remove()
element removal}
- * only if the {@link #checkWritePermission()} method does not throw
exception.
- */
- @Override
- public Iterator<E> iterator() {
- return new Iter<>(super.iterator());
- }
-
- /**
- * Returns an iterator over the elements in this list.
- * The returned iterator will support {@linkplain ListIterator#remove()
element removal},
- * {@linkplain ListIterator#add(Object) addition} or {@linkplain
ListIterator#set(Object)
- * modification} only if the {@link #checkWritePermission()} method does
not throw exception.
- */
- @Override
- public ListIterator<E> listIterator() {
- return new ListIter(super.listIterator());
- }
-
- /**
- * Returns an iterator over the elements in this list, starting at the
given index.
- * The returned iterator will support {@linkplain ListIterator#remove()
element removal},
- * {@linkplain ListIterator#add(Object) addition} or {@linkplain
ListIterator#set(Object)
- * modification} only if the {@link #checkWritePermission()} method does
not throw exception.
- */
- @Override
- public ListIterator<E> listIterator(final int index) {
- return new ListIter(super.listIterator(index));
- }
-
- /**
* Replaces the element at the specified position in this list with the
specified element.
*
* @param index index of element to replace.
@@ -267,14 +124,10 @@ public class CheckedArrayList<E> extends
* @return the element previously at the specified position.
* @throws IndexOutOfBoundsException if index out of range.
* @throws IllegalArgumentException if the specified element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public E set(final int index, final E element)
- throws IllegalArgumentException, UnsupportedOperationException
- {
+ public E set(final int index, final E element) throws
IllegalArgumentException {
ensureValid(element);
- checkWritePermission();
return super.set(index, element);
}
@@ -284,14 +137,10 @@ public class CheckedArrayList<E> extends
* @param element element to be appended to this list.
* @return always {@code true}.
* @throws IllegalArgumentException if the specified element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public boolean add(final E element)
- throws IllegalArgumentException, UnsupportedOperationException
- {
+ public boolean add(final E element) throws IllegalArgumentException {
ensureValid(element);
- checkWritePermission();
return super.add(element);
}
@@ -302,14 +151,10 @@ public class CheckedArrayList<E> extends
* @param element element to be inserted.
* @throws IndexOutOfBoundsException if index out of range.
* @throws IllegalArgumentException if the specified element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public void add(final int index, final E element)
- throws IllegalArgumentException, UnsupportedOperationException
- {
+ public void add(final int index, final E element) throws
IllegalArgumentException {
ensureValid(element);
- checkWritePermission();
super.add(index, element);
}
@@ -320,14 +165,10 @@ public class CheckedArrayList<E> extends
* @param collection the elements to be inserted into this list.
* @return {@code true} if this list changed as a result of the call.
* @throws IllegalArgumentException if at least one element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public boolean addAll(final Collection<? extends E> collection)
- throws IllegalArgumentException, UnsupportedOperationException
- {
+ public boolean addAll(final Collection<? extends E> collection) throws
IllegalArgumentException {
ensureValidCollection(collection);
- checkWritePermission();
return super.addAll(collection);
}
@@ -339,69 +180,10 @@ public class CheckedArrayList<E> extends
* @param collection elements to be inserted into this list.
* @return {@code true} if this list changed as a result of the call.
* @throws IllegalArgumentException if at least one element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public boolean addAll(final int index, final Collection<? extends E>
collection)
- throws IllegalArgumentException, UnsupportedOperationException
- {
+ public boolean addAll(final int index, final Collection<? extends E>
collection) throws IllegalArgumentException {
ensureValidCollection(collection);
- checkWritePermission();
return super.addAll(index, collection);
}
-
- /**
- * Removes the element at the specified position in this list.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public E remove(int index) throws UnsupportedOperationException {
- checkWritePermission();
- return super.remove(index);
- }
-
- /**
- * Removes the first occurrence of the specified element from this list.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean remove(Object o) throws UnsupportedOperationException {
- checkWritePermission();
- return super.remove(o);
- }
-
- /**
- * Removes all of this list's elements that are also contained in the
specified collection.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean removeAll(Collection<?> c) throws
UnsupportedOperationException {
- checkWritePermission();
- return super.removeAll(c);
- }
-
- /**
- * Retains only the elements in this list that are contained in the
specified collection.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean retainAll(Collection<?> c) throws
UnsupportedOperationException {
- checkWritePermission();
- return super.retainAll(c);
- }
-
- /**
- * Removes all of the elements from this list.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public void clear() throws UnsupportedOperationException {
- checkWritePermission();
- super.clear();
- }
}
Modified:
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedHashSet.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedHashSet.java?rev=1470856&r1=1470855&r2=1470856&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedHashSet.java
[UTF-8] (original)
+++
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedHashSet.java
[UTF-8] Tue Apr 23 09:05:42 2013
@@ -17,12 +17,8 @@
package org.apache.sis.internal.util;
import java.util.Set;
-import java.util.Iterator;
-import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
-import java.util.NoSuchElementException;
-import org.apache.sis.util.Decorator;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.collection.CheckedContainer;
@@ -34,12 +30,11 @@ import static org.apache.sis.util.Argume
* The type checks are performed at run-time in addition to the compile-time
checks.
*
* <p>Using this class is similar to wrapping a {@link LinkedHashSet} using
the methods provided
- * in the standard {@link Collections} class, except for the following
advantages:</p>
+ * in the standard {@link Collections} class, except for the following
differences:</p>
*
* <ul>
* <li>Avoid one level of indirection.</li>
- * <li>Checks for write permission.</li>
- * <li>Overrideable methods for controlling the type checks and write
permission checks.</li>
+ * <li>Does not accept null elements.</li>
* </ul>
*
* @param <E> The type of elements in the set.
@@ -51,7 +46,7 @@ import static org.apache.sis.util.Argume
*
* @see Collections#checkedSet(Set, Class)
*/
-public class CheckedHashSet<E> extends LinkedHashSet<E> implements
CheckedContainer<E>, Cloneable {
+public final class CheckedHashSet<E> extends LinkedHashSet<E> implements
CheckedContainer<E> {
/**
* Serial version UID for compatibility with different versions.
*/
@@ -94,167 +89,23 @@ public class CheckedHashSet<E> extends L
}
/**
- * Ensures that the given element can be added to this set.
- * The default implementation ensures that the object is {@code null} or
assignable
- * to the type specified at construction time. Subclasses can override
this method
- * if they need to perform additional checks.
- *
- * @param element the object to check, or {@code null}.
- * @throws IllegalArgumentException if the specified element can not be
added to this set.
- */
- protected void ensureValid(final E element) throws
IllegalArgumentException {
- if (element != null && !type.isInstance(element)) {
- throw new IllegalArgumentException(Errors.format(
- Errors.Keys.IllegalArgumentClass_3, "element", type,
element.getClass()));
- }
- }
-
- /**
- * Ensures that all elements of the given collection can be added to this
set.
- *
- * @param collection the collection to check, or {@code null}.
- * @throws IllegalArgumentException if at least one element can not be
added to this set.
- */
- private void ensureValidCollection(final Collection<? extends E>
collection) throws IllegalArgumentException {
- for (final E element : collection) {
- ensureValid(element);
- }
- }
-
- /**
- * Checks if changes in this set are allowed. This method is automatically
invoked before any
- * operation that may change the content. If the write operation is
allowed, then this method
- * shall returns normally. Otherwise an {@link
UnsupportedOperationException} is thrown.
- *
- * <p>The default implementation does nothing, thus allowing this set to
be modified.
- * Subclasses can override this method if they want to control write
permissions.</p>
- *
- * @throws UnsupportedOperationException if this set is unmodifiable.
- */
- protected void checkWritePermission() throws UnsupportedOperationException
{
- }
-
- /**
- * An iterator with a check for write permission prior element removal.
- * This class wraps the iterator provided by {@link
LinkedHashSet#iterator()}.
- *
- * @see CheckedHashSet#iterator()
- */
- @Decorator(Iterator.class)
- private final class Iter implements Iterator<E> {
- /** The {@link LinkedHashSet} iterator. */
- private final Iterator<E> iterator;
-
- /** Creates a new wrapper for the given {@link LinkedHashSet}
iterator. */
- Iter(final Iterator<E> iterator) {
- this.iterator = iterator;
- }
-
- /** Returns {@code true} if there is more elements in the iteration. */
- @Override
- public boolean hasNext() {
- return iterator.hasNext();
- }
-
- /** Returns the next element in the iteration. */
- @Override
- public E next() throws NoSuchElementException {
- return iterator.next();
- }
-
- /** Removes the previous element if the enclosing {@link
CheckedHashSet} allows write operations. */
- @Override
- public void remove() throws UnsupportedOperationException {
- checkWritePermission();
- iterator.remove();
- }
- }
-
- /**
- * Returns an iterator over the elements in this set.
- * The returned iterator will support {@linkplain Iterator#remove()
element removal}
- * only if the {@link #checkWritePermission()} method does not throw
exception.
- */
- @Override
- public Iterator<E> iterator() {
- return new Iter(super.iterator());
- }
-
- /**
* Adds the specified element to this set if it is not already present.
*
* @param element element to be added to this set.
* @return {@code true} if the set did not already contain the specified
element.
* @throws IllegalArgumentException if the specified element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
*/
@Override
- public boolean add(final E element)
- throws IllegalArgumentException, UnsupportedOperationException
- {
- ensureValid(element);
- checkWritePermission();
+ public boolean add(final E element) throws IllegalArgumentException {
+ if (!type.isInstance(element)) {
+ ensureNonNull("element", element);
+ throw new IllegalArgumentException(Errors.format(
+ Errors.Keys.IllegalArgumentClass_3, "element", type,
element.getClass()));
+ }
return super.add(element);
}
- /**
- * Appends all of the elements in the specified collection to this set.
- *
- * @param collection the elements to be inserted into this set.
- * @return {@code true} if this set changed as a result of the call.
- * @throws IllegalArgumentException if at least one element is not of the
expected type.
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean addAll(final Collection<? extends E> collection)
- throws IllegalArgumentException, UnsupportedOperationException
- {
- ensureValidCollection(collection);
- checkWritePermission();
- return super.addAll(collection);
- }
-
- /**
- * Removes the specified element from this set.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean remove(Object o) throws UnsupportedOperationException {
- checkWritePermission();
- return super.remove(o);
- }
-
- /**
- * Removes all of this set's elements that are also contained in the
specified collection.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public boolean removeAll(Collection<?> c) throws
UnsupportedOperationException {
- checkWritePermission();
- return super.removeAll(c);
- }
-
- /**
- * Retains only the elements in this set that are contained in the
specified collection.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
+ /*
+ * No need to override 'addAll', since it is implemented on top of 'add'.
*/
- @Override
- public boolean retainAll(Collection<?> c) throws
UnsupportedOperationException {
- checkWritePermission();
- return super.retainAll(c);
- }
-
- /**
- * Removes all of the elements from this set.
- *
- * @throws UnsupportedOperationException if this collection is
unmodifiable.
- */
- @Override
- public void clear() throws UnsupportedOperationException {
- checkWritePermission();
- super.clear();
- }
}