[COLLECTIONS-380] Fixed infinte recursion when creating an unmodifiable bounded collection, added unit test, fixed additional javadoc, thanks to Dave Brosius for reporting.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1311366 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-04-09 18:40:36 +00:00
parent 0b83241a1a
commit 970921c219
2 changed files with 94 additions and 3 deletions

View File

@ -52,10 +52,10 @@ public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDe
* *
* @param coll the <code>BoundedCollection</code> to decorate, must not be null * @param coll the <code>BoundedCollection</code> to decorate, must not be null
* @return a new unmodifiable bounded collection * @return a new unmodifiable bounded collection
* @throws IllegalArgumentException if bag is null * @throws IllegalArgumentException if {@code coll} is {@code null}
*/ */
public static <E> BoundedCollection<E> unmodifiableBoundedCollection(BoundedCollection<E> coll) { public static <E> BoundedCollection<E> unmodifiableBoundedCollection(BoundedCollection<E> coll) {
return unmodifiableBoundedCollection(coll); return new UnmodifiableBoundedCollection<E>(coll);
} }
/** /**
@ -66,7 +66,7 @@ public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDe
* *
* @param coll the <code>BoundedCollection</code> to decorate, must not be null * @param coll the <code>BoundedCollection</code> to decorate, must not be null
* @return a new unmodifiable bounded collection * @return a new unmodifiable bounded collection
* @throws IllegalArgumentException if bag is null * @throws IllegalArgumentException if {@code coll} is {@code null}
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> coll) { public static <E> BoundedCollection<E> unmodifiableBoundedCollection(Collection<? extends E> coll) {
@ -139,10 +139,16 @@ public final class UnmodifiableBoundedCollection<E> extends AbstractCollectionDe
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public boolean isFull() { public boolean isFull() {
return decorated().isFull(); return decorated().isFull();
} }
/**
* {@inheritDoc}
*/
public int maxSize() { public int maxSize() {
return decorated().maxSize(); return decorated().maxSize();
} }

View File

@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUtils;
import org.apache.commons.collections.buffer.BoundedBuffer;
/**
* Extension of {@link AbstractTestCollection} for exercising the
* {@link UnmodifiableBoundedCollection} implementation.
*/
public class TestUnmodifiableBoundedCollection<E> extends AbstractTestCollection<E> {
public TestUnmodifiableBoundedCollection(String testName) {
super(testName);
}
//-----------------------------------------------------------------------
@Override
public Collection<E> makeObject() {
BoundedBuffer<E> buffer = BoundedBuffer.<E>boundedBuffer(new ArrayStack<E>(), 10);
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(buffer);
}
@Override
public Collection<E> makeFullCollection() {
E[] allElements = getFullElements();
Buffer<E> buffer = BufferUtils.boundedBuffer(new ArrayStack<E>(), allElements.length);
buffer.addAll(Arrays.asList(allElements));
return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(buffer);
}
@Override
public Collection<E> makeConfirmedCollection() {
return new ArrayList<E>();
}
@Override
public Collection<E> makeConfirmedFullCollection() {
ArrayList<E> list = new ArrayList<E>();
list.addAll(Arrays.asList(getFullElements()));
return list;
}
@Override
public boolean isAddSupported() {
return false;
}
@Override
public boolean isRemoveSupported() {
return false;
}
@Override
protected boolean skipSerializedCanonicalTests() {
return true;
}
@Override
public String getCompatibilityVersion() {
return "3.1";
}
}