COLLECTIONS-427 patch applied.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1377485 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
dec0641e2f
commit
8338b9a252
|
@ -75,6 +75,9 @@
|
|||
<action issue="COLLECTIONS-405" dev="brentworden" type="add" due-to="Adam Dyga">
|
||||
Added "ListUtils#select" and "ListUtils#selectRejected" methods.
|
||||
</action>
|
||||
<action issue="COLLECTIONS-427" dev="brentworden" type="fix" due-to="Mert Guldur">
|
||||
Fixed performance issue in "SetUniqueList#retainAll" method for large collections.
|
||||
</action>
|
||||
</release>
|
||||
</body>
|
||||
</document>
|
||||
|
|
|
@ -29,14 +29,14 @@ import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
|
|||
import org.apache.commons.collections.set.UnmodifiableSet;
|
||||
|
||||
/**
|
||||
* Decorates a <code>List</code> to ensure that no duplicates are present
|
||||
* much like a <code>Set</code>.
|
||||
* Decorates a <code>List</code> to ensure that no duplicates are present much
|
||||
* like a <code>Set</code>.
|
||||
* <p>
|
||||
* The <code>List</code> interface makes certain assumptions/requirements.
|
||||
* This implementation breaks these in certain ways, but this is merely the
|
||||
* result of rejecting duplicates.
|
||||
* Each violation is explained in the method, but it should not affect you.
|
||||
* Bear in mind that Sets require immutable objects to function correctly.
|
||||
* The <code>List</code> interface makes certain assumptions/requirements. This
|
||||
* implementation breaks these in certain ways, but this is merely the result of
|
||||
* rejecting duplicates. Each violation is explained in the method, but it
|
||||
* should not affect you. Bear in mind that Sets require immutable objects to
|
||||
* function correctly.
|
||||
* <p>
|
||||
* The {@link org.apache.commons.collections.set.ListOrderedSet ListOrderedSet}
|
||||
* class provides an alternative approach, by wrapping an existing Set and
|
||||
|
@ -58,15 +58,19 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
protected final Set<E> set;
|
||||
|
||||
/**
|
||||
* Factory method to create a SetList using the supplied list to retain order.
|
||||
* Factory method to create a SetList using the supplied list to retain
|
||||
* order.
|
||||
* <p>
|
||||
* If the list contains duplicates, these are removed (first indexed one kept).
|
||||
* A <code>HashSet</code> is used for the set behaviour.
|
||||
* If the list contains duplicates, these are removed (first indexed one
|
||||
* kept). A <code>HashSet</code> is used for the set behaviour.
|
||||
*
|
||||
* @param <E> the element type
|
||||
* @param list the list to decorate, must not be null
|
||||
* @param <E>
|
||||
* the element type
|
||||
* @param list
|
||||
* the list to decorate, must not be null
|
||||
* @return a new {@link SetUniqueList}
|
||||
* @throws IllegalArgumentException if list is null
|
||||
* @throws IllegalArgumentException
|
||||
* if list is null
|
||||
*/
|
||||
public static <E> SetUniqueList<E> setUniqueList(List<E> list) {
|
||||
if (list == null) {
|
||||
|
@ -82,15 +86,19 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
return sl;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies) the List and specifies the set to use.
|
||||
* Constructor that wraps (not copies) the List and specifies the set to
|
||||
* use.
|
||||
* <p>
|
||||
* The set and list must both be correctly initialised to the same elements.
|
||||
*
|
||||
* @param set the set to decorate, must not be null
|
||||
* @param list the list to decorate, must not be null
|
||||
* @throws IllegalArgumentException if set or list is null
|
||||
* @param set
|
||||
* the set to decorate, must not be null
|
||||
* @param list
|
||||
* the list to decorate, must not be null
|
||||
* @throws IllegalArgumentException
|
||||
* if set or list is null
|
||||
*/
|
||||
protected SetUniqueList(List<E> list, Set<E> set) {
|
||||
super(list);
|
||||
|
@ -100,7 +108,7 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
this.set = set;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* Gets an unmodifiable view as a Set.
|
||||
*
|
||||
|
@ -110,16 +118,16 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
return UnmodifiableSet.unmodifiableSet(set);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* Adds an element to the list if it is not already present.
|
||||
* <p>
|
||||
* <i>(Violation)</i>
|
||||
* The <code>List</code> interface requires that this method returns
|
||||
* <code>true</code> always. However this class may return <code>false</code>
|
||||
* because of the <code>Set</code> behaviour.
|
||||
* <i>(Violation)</i> The <code>List</code> interface requires that this
|
||||
* method returns <code>true</code> always. However this class may return
|
||||
* <code>false</code> because of the <code>Set</code> behaviour.
|
||||
*
|
||||
* @param object the object to add
|
||||
* @param object
|
||||
* the object to add
|
||||
* @return true if object was added
|
||||
*/
|
||||
@Override
|
||||
|
@ -135,14 +143,17 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds an element to a specific index in the list if it is not already present.
|
||||
* Adds an element to a specific index in the list if it is not already
|
||||
* present.
|
||||
* <p>
|
||||
* <i>(Violation)</i>
|
||||
* The <code>List</code> interface makes the assumption that the element is
|
||||
* always inserted. This may not happen with this implementation.
|
||||
* <i>(Violation)</i> The <code>List</code> interface makes the assumption
|
||||
* that the element is always inserted. This may not happen with this
|
||||
* implementation.
|
||||
*
|
||||
* @param index the index to insert at
|
||||
* @param object the object to add
|
||||
* @param index
|
||||
* the index to insert at
|
||||
* @param object
|
||||
* the object to add
|
||||
*/
|
||||
@Override
|
||||
public void add(int index, E object) {
|
||||
|
@ -159,11 +170,12 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
* Only elements that are not already in this list will be added, and
|
||||
* duplicates from the specified collection will be ignored.
|
||||
* <p>
|
||||
* <i>(Violation)</i>
|
||||
* The <code>List</code> interface makes the assumption that the elements
|
||||
* are always inserted. This may not happen with this implementation.
|
||||
* <i>(Violation)</i> The <code>List</code> interface makes the assumption
|
||||
* that the elements are always inserted. This may not happen with this
|
||||
* implementation.
|
||||
*
|
||||
* @param coll the collection to add in iterator order
|
||||
* @param coll
|
||||
* the collection to add in iterator order
|
||||
* @return true if this collection changed
|
||||
*/
|
||||
@Override
|
||||
|
@ -178,12 +190,14 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
* Only elements that are not already in this list will be added, and
|
||||
* duplicates from the specified collection will be ignored.
|
||||
* <p>
|
||||
* <i>(Violation)</i>
|
||||
* The <code>List</code> interface makes the assumption that the elements
|
||||
* are always inserted. This may not happen with this implementation.
|
||||
* <i>(Violation)</i> The <code>List</code> interface makes the assumption
|
||||
* that the elements are always inserted. This may not happen with this
|
||||
* implementation.
|
||||
*
|
||||
* @param index the index to insert at
|
||||
* @param coll the collection to add in iterator order
|
||||
* @param index
|
||||
* the index to insert at
|
||||
* @param coll
|
||||
* the collection to add in iterator order
|
||||
* @return true if this collection changed
|
||||
*/
|
||||
@Override
|
||||
|
@ -197,17 +211,18 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
return super.addAll(index, temp);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* Sets the value at the specified index avoiding duplicates.
|
||||
* <p>
|
||||
* The object is set into the specified index.
|
||||
* Afterwards, any previous duplicate is removed
|
||||
* If the object is not already in the list then a normal set occurs.
|
||||
* If it is present, then the old version is removed.
|
||||
* The object is set into the specified index. Afterwards, any previous
|
||||
* duplicate is removed If the object is not already in the list then a
|
||||
* normal set occurs. If it is present, then the old version is removed.
|
||||
*
|
||||
* @param index the index to insert at
|
||||
* @param object the object to set
|
||||
* @param index
|
||||
* the index to insert at
|
||||
* @param object
|
||||
* the object to set
|
||||
* @return the previous object
|
||||
*/
|
||||
@Override
|
||||
|
@ -254,9 +269,26 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> coll) {
|
||||
boolean result = super.retainAll(coll);
|
||||
set.retainAll(coll);
|
||||
return result;
|
||||
Set<Object> setRetainAll = new HashSet<Object>();
|
||||
for (Iterator<?> it = coll.iterator(); it.hasNext();) {
|
||||
Object next = it.next();
|
||||
if (set.contains(next)) {
|
||||
setRetainAll.add(next);
|
||||
}
|
||||
}
|
||||
if (setRetainAll.size() == set.size()) {
|
||||
return false;
|
||||
}
|
||||
if (setRetainAll.size() == 0) {
|
||||
clear();
|
||||
} else {
|
||||
for (Iterator<E> it = iterator(); it.hasNext();) {
|
||||
if (!setRetainAll.contains(it.next())) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -301,9 +333,12 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
* Create a new {@link Set} with the same type as the provided {@code set}
|
||||
* and populate it with all elements of {@code list}.
|
||||
*
|
||||
* @param set the {@link Set} to be used as return type, must not be null
|
||||
* @param list the {@link List} to populate the {@link Set}
|
||||
* @return a new {@link Set} populated with all elements of the provided {@link List}
|
||||
* @param set
|
||||
* the {@link Set} to be used as return type, must not be null
|
||||
* @param list
|
||||
* the {@link List} to populate the {@link Set}
|
||||
* @return a new {@link Set} populated with all elements of the provided
|
||||
* {@link List}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Set<E> createSetBasedOnList(Set<E> set, List<E> list) {
|
||||
|
@ -323,7 +358,7 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
return subSet;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* Inner class iterator.
|
||||
*/
|
||||
|
@ -354,7 +389,8 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
/**
|
||||
* Inner class iterator.
|
||||
*/
|
||||
static class SetListListIterator<E> extends AbstractListIteratorDecorator<E> {
|
||||
static class SetListListIterator<E> extends
|
||||
AbstractListIteratorDecorator<E> {
|
||||
|
||||
protected final Set<E> set;
|
||||
protected E last = null;
|
||||
|
@ -393,7 +429,8 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
|
|||
|
||||
@Override
|
||||
public void set(E object) {
|
||||
throw new UnsupportedOperationException("ListIterator does not support set");
|
||||
throw new UnsupportedOperationException(
|
||||
"ListIterator does not support set");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -523,6 +523,79 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
|
|||
assertFalse(subUniqueList.contains("World")); // fails
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testRetainAll() {
|
||||
List<E> list = new ArrayList<E>(10);
|
||||
SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
uniqueList.add((E)Integer.valueOf(i));
|
||||
}
|
||||
|
||||
Collection<E> retained = new ArrayList<E>(5);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
retained.add((E)Integer.valueOf(i * 2));
|
||||
}
|
||||
|
||||
assertTrue(uniqueList.retainAll(retained));
|
||||
assertEquals(5, uniqueList.size());
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(0)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(2)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(4)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(6)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(8)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testRetainAllWithInitialList() {
|
||||
// initialized with empty list
|
||||
List<E> list = new ArrayList<E>(10);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
list.add((E)Integer.valueOf(i));
|
||||
}
|
||||
SetUniqueList<E> uniqueList = SetUniqueList.setUniqueList(list);
|
||||
for (int i = 5; i < 10; ++i) {
|
||||
uniqueList.add((E)Integer.valueOf(i));
|
||||
}
|
||||
|
||||
Collection<E> retained = new ArrayList<E>(5);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
retained.add((E)Integer.valueOf(i * 2));
|
||||
}
|
||||
|
||||
assertTrue(uniqueList.retainAll(retained));
|
||||
assertEquals(5, uniqueList.size());
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(0)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(2)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(4)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(6)));
|
||||
assertTrue(uniqueList.contains(Integer.valueOf(8)));
|
||||
}
|
||||
|
||||
/*
|
||||
* test case for https://issues.apache.org/jira/browse/COLLECTIONS-427
|
||||
*/
|
||||
public void testRetainAllCollections427() {
|
||||
int size = 50000;
|
||||
ArrayList<Integer> list = new ArrayList<Integer>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
SetUniqueList<Integer> uniqueList = SetUniqueList.setUniqueList(list);
|
||||
ArrayList<Integer> toRetain = new ArrayList<Integer>();
|
||||
for (int i = size; i < 2*size; i++) {
|
||||
toRetain.add(i);
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
uniqueList.retainAll(toRetain);
|
||||
long stop = System.currentTimeMillis();
|
||||
|
||||
// make sure retainAll completes under 5 seconds
|
||||
// TODO if test is migrated to JUnit 4, add a Timeout rule.
|
||||
// http://kentbeck.github.com/junit/javadoc/latest/org/junit/rules/Timeout.html
|
||||
assertTrue((stop - start) < 5000);
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class SetUniqueList307 extends SetUniqueList<E> {
|
||||
public SetUniqueList307(List<E> list, Set<E> set) {
|
||||
|
|
Loading…
Reference in New Issue