Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r555925 | skestle | 2007-07-13 03:39:24 -0700 (Fri, 13 Jul 2007) | 2 lines Added Edwin Tellman's patch for COLLECTIONS-243. It all seems pretty reasonable, and it should all be checked again as the project is worked through ------------------------------------------------------------------------ r471166 | scolebourne | 2006-11-04 03:33:22 -0800 (Sat, 04 Nov 2006) | 1 line Removed Typed* containers such as TypedList and TypedMap as generics now provides type safety ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815069 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9c4574b68d
commit
97850dae19
|
@ -27,7 +27,6 @@ import org.apache.commons.collections.list.LazyList;
|
||||||
import org.apache.commons.collections.list.PredicatedList;
|
import org.apache.commons.collections.list.PredicatedList;
|
||||||
import org.apache.commons.collections.list.SynchronizedList;
|
import org.apache.commons.collections.list.SynchronizedList;
|
||||||
import org.apache.commons.collections.list.TransformedList;
|
import org.apache.commons.collections.list.TransformedList;
|
||||||
import org.apache.commons.collections.list.TypedList;
|
|
||||||
import org.apache.commons.collections.list.UnmodifiableList;
|
import org.apache.commons.collections.list.UnmodifiableList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,7 +50,7 @@ public class ListUtils {
|
||||||
* This uses the {@link Collections Collections} implementation
|
* This uses the {@link Collections Collections} implementation
|
||||||
* and is provided for completeness.
|
* and is provided for completeness.
|
||||||
*/
|
*/
|
||||||
public static final List EMPTY_LIST = Collections.EMPTY_LIST;
|
public static final List<Object> EMPTY_LIST = Collections.<Object>emptyList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <code>ListUtils</code> should not normally be instantiated.
|
* <code>ListUtils</code> should not normally be instantiated.
|
||||||
|
@ -69,18 +68,14 @@ public class ListUtils {
|
||||||
* @return the intersection of those two lists
|
* @return the intersection of those two lists
|
||||||
* @throws NullPointerException if either list is null
|
* @throws NullPointerException if either list is null
|
||||||
*/
|
*/
|
||||||
public static List intersection(final List list1, final List list2) {
|
public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) {
|
||||||
final ArrayList result = new ArrayList();
|
final List<E> result = new ArrayList<E>();
|
||||||
final Iterator iterator = list2.iterator();
|
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
for (E e : list2) {
|
||||||
final Object o = iterator.next();
|
if (list1.contains(e)) {
|
||||||
|
result.add(e);
|
||||||
if (list1.contains(o)) {
|
|
||||||
result.add(o);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,14 +94,11 @@ public class ListUtils {
|
||||||
* @return a new list containing the results
|
* @return a new list containing the results
|
||||||
* @throws NullPointerException if either list is null
|
* @throws NullPointerException if either list is null
|
||||||
*/
|
*/
|
||||||
public static List subtract(final List list1, final List list2) {
|
public static <E> List<E> subtract(final List<E> list1, final List<? extends E> list2) {
|
||||||
final ArrayList result = new ArrayList(list1);
|
final ArrayList<E> result = new ArrayList<E>(list1);
|
||||||
final Iterator iterator = list2.iterator();
|
for (E e : list2) {
|
||||||
|
result.remove(e);
|
||||||
while (iterator.hasNext()) {
|
|
||||||
result.remove(iterator.next());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +111,7 @@ public class ListUtils {
|
||||||
* @return a new list containing the sum of those lists
|
* @return a new list containing the sum of those lists
|
||||||
* @throws NullPointerException if either list is null
|
* @throws NullPointerException if either list is null
|
||||||
*/
|
*/
|
||||||
public static List sum(final List list1, final List list2) {
|
public static <E> List<E> sum(final List<? extends E> list1, final List<? extends E> list2) {
|
||||||
return subtract(union(list1, list2), intersection(list1, list2));
|
return subtract(union(list1, list2), intersection(list1, list2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,8 +125,8 @@ public class ListUtils {
|
||||||
* @return a new list containing the union of those lists
|
* @return a new list containing the union of those lists
|
||||||
* @throws NullPointerException if either list is null
|
* @throws NullPointerException if either list is null
|
||||||
*/
|
*/
|
||||||
public static List union(final List list1, final List list2) {
|
public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2) {
|
||||||
final ArrayList result = new ArrayList(list1);
|
final ArrayList<E> result = new ArrayList<E>(list1);
|
||||||
result.addAll(list2);
|
result.addAll(list2);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +160,7 @@ public class ListUtils {
|
||||||
* @param list2 the second list, may be null
|
* @param list2 the second list, may be null
|
||||||
* @return whether the lists are equal by value comparison
|
* @return whether the lists are equal by value comparison
|
||||||
*/
|
*/
|
||||||
public static boolean isEqualList(final Collection list1, final Collection list2) {
|
public static boolean isEqualList(final Collection<?> list1, final Collection<?> list2) {
|
||||||
if (list1 == list2) {
|
if (list1 == list2) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -176,8 +168,8 @@ public class ListUtils {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator it1 = list1.iterator();
|
Iterator<?> it1 = list1.iterator();
|
||||||
Iterator it2 = list2.iterator();
|
Iterator<?> it2 = list2.iterator();
|
||||||
Object obj1 = null;
|
Object obj1 = null;
|
||||||
Object obj2 = null;
|
Object obj2 = null;
|
||||||
|
|
||||||
|
@ -205,16 +197,15 @@ public class ListUtils {
|
||||||
* @param list the list to generate the hashCode for, may be null
|
* @param list the list to generate the hashCode for, may be null
|
||||||
* @return the hash code
|
* @return the hash code
|
||||||
*/
|
*/
|
||||||
public static int hashCodeForList(final Collection list) {
|
public static <E> int hashCodeForList(final Collection<E> list) {
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int hashCode = 1;
|
int hashCode = 1;
|
||||||
Iterator it = list.iterator();
|
Iterator<E> it = list.iterator();
|
||||||
Object obj = null;
|
|
||||||
|
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
obj = it.next();
|
E obj = it.next();
|
||||||
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
|
hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
|
||||||
}
|
}
|
||||||
return hashCode;
|
return hashCode;
|
||||||
|
@ -236,11 +227,10 @@ public class ListUtils {
|
||||||
* @throws NullPointerException if either parameter is null
|
* @throws NullPointerException if either parameter is null
|
||||||
* @since Commons Collections 3.2
|
* @since Commons Collections 3.2
|
||||||
*/
|
*/
|
||||||
public static List retainAll(Collection collection, Collection retain) {
|
public static <E> List<E> retainAll(Collection<E> collection, Collection<?> retain) {
|
||||||
List list = new ArrayList(Math.min(collection.size(), retain.size()));
|
List<E> list = new ArrayList<E>(Math.min(collection.size(), retain.size()));
|
||||||
|
|
||||||
for (Iterator iter = collection.iterator(); iter.hasNext();) {
|
for (E obj : collection) {
|
||||||
Object obj = iter.next();
|
|
||||||
if (retain.contains(obj)) {
|
if (retain.contains(obj)) {
|
||||||
list.add(obj);
|
list.add(obj);
|
||||||
}
|
}
|
||||||
|
@ -264,11 +254,10 @@ public class ListUtils {
|
||||||
* @throws NullPointerException if either parameter is null
|
* @throws NullPointerException if either parameter is null
|
||||||
* @since Commons Collections 3.2
|
* @since Commons Collections 3.2
|
||||||
*/
|
*/
|
||||||
public static List removeAll(Collection collection, Collection remove) {
|
public static <E> List<E> removeAll(Collection<E> collection, Collection<?> remove) {
|
||||||
List list = new ArrayList();
|
List<E> list = new ArrayList<E>();
|
||||||
for (Iterator iter = collection.iterator(); iter.hasNext();) {
|
for (E obj : collection) {
|
||||||
Object obj = iter.next();
|
if (!remove.contains(obj)) {
|
||||||
if (remove.contains(obj) == false) {
|
|
||||||
list.add(obj);
|
list.add(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,7 +287,7 @@ public class ListUtils {
|
||||||
* @return a synchronized list backed by the given list
|
* @return a synchronized list backed by the given list
|
||||||
* @throws IllegalArgumentException if the list is null
|
* @throws IllegalArgumentException if the list is null
|
||||||
*/
|
*/
|
||||||
public static List synchronizedList(List list) {
|
public static <E> List<E> synchronizedList(List<E> list) {
|
||||||
return SynchronizedList.decorate(list);
|
return SynchronizedList.decorate(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,7 +300,7 @@ public class ListUtils {
|
||||||
* @return an unmodifiable list backed by the given list
|
* @return an unmodifiable list backed by the given list
|
||||||
* @throws IllegalArgumentException if the list is null
|
* @throws IllegalArgumentException if the list is null
|
||||||
*/
|
*/
|
||||||
public static List unmodifiableList(List list) {
|
public static <E> List<E> unmodifiableList(List<E> list) {
|
||||||
return UnmodifiableList.decorate(list);
|
return UnmodifiableList.decorate(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,23 +317,10 @@ public class ListUtils {
|
||||||
* @return a predicated list backed by the given list
|
* @return a predicated list backed by the given list
|
||||||
* @throws IllegalArgumentException if the List or Predicate is null
|
* @throws IllegalArgumentException if the List or Predicate is null
|
||||||
*/
|
*/
|
||||||
public static List predicatedList(List list, Predicate predicate) {
|
public static <E> List<E> predicatedList(List<E> list, Predicate<E> predicate) {
|
||||||
return PredicatedList.decorate(list, predicate);
|
return PredicatedList.decorate(list, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a typed list backed by the given list.
|
|
||||||
* <p>
|
|
||||||
* Only objects of the specified type can be added to the list.
|
|
||||||
*
|
|
||||||
* @param list the list to limit to a specific type, must not be null
|
|
||||||
* @param type the type of objects which may be added to the list
|
|
||||||
* @return a typed list backed by the specified list
|
|
||||||
*/
|
|
||||||
public static List typedList(List list, Class type) {
|
|
||||||
return TypedList.decorate(list, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a transformed list backed by the given list.
|
* Returns a transformed list backed by the given list.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -364,7 +340,7 @@ public class ListUtils {
|
||||||
* @return a transformed list backed by the given list
|
* @return a transformed list backed by the given list
|
||||||
* @throws IllegalArgumentException if the List or Transformer is null
|
* @throws IllegalArgumentException if the List or Transformer is null
|
||||||
*/
|
*/
|
||||||
public static List transformedList(List list, Transformer transformer) {
|
public static <E> List<E> transformedList(List<E> list, Transformer<? super E, ? extends E> transformer) {
|
||||||
return TransformedList.decorate(list, transformer);
|
return TransformedList.decorate(list, transformer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -397,7 +373,7 @@ public class ListUtils {
|
||||||
* @return a lazy list backed by the given list
|
* @return a lazy list backed by the given list
|
||||||
* @throws IllegalArgumentException if the List or Factory is null
|
* @throws IllegalArgumentException if the List or Factory is null
|
||||||
*/
|
*/
|
||||||
public static List lazyList(List list, Factory factory) {
|
public static <E> List<E> lazyList(List<E> list, Factory<? extends E> factory) {
|
||||||
return LazyList.decorate(list, factory);
|
return LazyList.decorate(list, factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,7 +387,7 @@ public class ListUtils {
|
||||||
* @return a fixed-size list backed by that list
|
* @return a fixed-size list backed by that list
|
||||||
* @throws IllegalArgumentException if the List is null
|
* @throws IllegalArgumentException if the List is null
|
||||||
*/
|
*/
|
||||||
public static List fixedSizeList(List list) {
|
public static <E> List<E> fixedSizeList(List<E> list) {
|
||||||
return FixedSizeList.decorate(list);
|
return FixedSizeList.decorate(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue