http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/IteratorUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/IteratorUtils.java b/src/java/org/apache/commons/collections/IteratorUtils.java index c84957f..eba8e37 100644 --- a/src/java/org/apache/commons/collections/IteratorUtils.java +++ b/src/java/org/apache/commons/collections/IteratorUtils.java @@ -81,26 +81,30 @@ public class IteratorUtils { * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1. * Use <code>EmptyIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1. */ - public static final ResettableIterator EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE; + public static final ResettableIterator<Object> EMPTY_ITERATOR = EmptyIterator.RESETTABLE_INSTANCE; + /** * A list iterator over no elements. * <p> * WARNING: This constant is binary incompatible with Commons Collections 2.1 and 2.1.1. * Use <code>EmptyListIterator.INSTANCE</code> for compatability with Commons Collections 2.1.1. */ - public static final ResettableListIterator EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE; + public static final ResettableListIterator<Object> EMPTY_LIST_ITERATOR = EmptyListIterator.RESETTABLE_INSTANCE; + /** * An ordered iterator over no elements. */ - public static final OrderedIterator EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE; + public static final OrderedIterator<Object> EMPTY_ORDERED_ITERATOR = EmptyOrderedIterator.INSTANCE; + /** * A map iterator over no elements. */ - public static final MapIterator EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE; + public static final MapIterator<Object, Object> EMPTY_MAP_ITERATOR = EmptyMapIterator.INSTANCE; + /** * An ordered map iterator over no elements. */ - public static final OrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE; + public static final OrderedMapIterator<Object, Object> EMPTY_ORDERED_MAP_ITERATOR = EmptyOrderedMapIterator.INSTANCE; /** * IteratorUtils is not normally instantiated. @@ -121,8 +125,8 @@ public class IteratorUtils { * * @return an iterator over nothing */ - public static ResettableIterator emptyIterator() { - return EMPTY_ITERATOR; + public static <E> ResettableIterator<E> emptyIterator() { + return EmptyIterator.<E>getResettableInstance(); } /** @@ -136,8 +140,8 @@ public class IteratorUtils { * * @return a list iterator over nothing */ - public static ResettableListIterator emptyListIterator() { - return EMPTY_LIST_ITERATOR; + public static <E> ResettableListIterator<E> emptyListIterator() { + return EmptyListIterator.<E>getResettableInstance(); } /** @@ -148,8 +152,8 @@ public class IteratorUtils { * * @return an ordered iterator over nothing */ - public static OrderedIterator emptyOrderedIterator() { - return EMPTY_ORDERED_ITERATOR; + public static <E> OrderedIterator<E> emptyOrderedIterator() { + return EmptyOrderedIterator.<E>getInstance(); } /** @@ -160,8 +164,8 @@ public class IteratorUtils { * * @return a map iterator over nothing */ - public static MapIterator emptyMapIterator() { - return EMPTY_MAP_ITERATOR; + public static <K, V> MapIterator<K, V> emptyMapIterator() { + return EmptyMapIterator.<K, V>getInstance(); } /** @@ -172,8 +176,8 @@ public class IteratorUtils { * * @return a map iterator over nothing */ - public static OrderedMapIterator emptyOrderedMapIterator() { - return EMPTY_ORDERED_MAP_ITERATOR; + public static <K, V> OrderedMapIterator<K, V> emptyOrderedMapIterator() { + return EmptyOrderedMapIterator.<K, V>getInstance(); } // Singleton @@ -190,8 +194,8 @@ public class IteratorUtils { * @param object the single object over which to iterate * @return a singleton iterator over the object */ - public static ResettableIterator singletonIterator(Object object) { - return new SingletonIterator(object); + public static <E> ResettableIterator<E> singletonIterator(E object) { + return new SingletonIterator<E>(object); } /** @@ -203,8 +207,8 @@ public class IteratorUtils { * @param object the single object over which to iterate * @return a singleton list iterator over the object */ - public static ListIterator singletonListIterator(Object object) { - return new SingletonListIterator(object); + public static <E> ListIterator<E> singletonListIterator(E object) { + return new SingletonListIterator<E>(object); } // Arrays @@ -219,8 +223,8 @@ public class IteratorUtils { * @return an iterator over the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array) { - return new ObjectArrayIterator(array); + public static <E> ResettableIterator<E> arrayIterator(E[] array) { + return new ObjectArrayIterator<E>(array); } /** @@ -234,8 +238,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if the array is not an array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array) { - return new ArrayIterator(array); + public static <E> ResettableIterator<E> arrayIterator(Object array) { + return new ArrayIterator<E>(array); } /** @@ -251,8 +255,8 @@ public class IteratorUtils { * than the length of the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array, int start) { - return new ObjectArrayIterator(array, start); + public static <E> ResettableIterator<E> arrayIterator(E[] array, int start) { + return new ObjectArrayIterator<E>(array, start); } /** @@ -269,8 +273,8 @@ public class IteratorUtils { * than the length of the array * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array, int start) { - return new ArrayIterator(array, start); + public static <E> ResettableIterator<E> arrayIterator(Object array, int start) { + return new ArrayIterator<E>(array, start); } /** @@ -287,8 +291,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object[] array, int start, int end) { - return new ObjectArrayIterator(array, start, end); + public static <E> ResettableIterator<E> arrayIterator(E[] array, int start, int end) { + return new ObjectArrayIterator<E>(array, start, end); } /** @@ -306,8 +310,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableIterator arrayIterator(Object array, int start, int end) { - return new ArrayIterator(array, start, end); + public static <E> ResettableIterator<E> arrayIterator(Object array, int start, int end) { + return new ArrayIterator<E>(array, start, end); } //----------------------------------------------------------------------- @@ -318,8 +322,8 @@ public class IteratorUtils { * @return a list iterator over the array * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array) { - return new ObjectArrayListIterator(array); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array) { + return new ObjectArrayListIterator<E>(array); } /** @@ -333,8 +337,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if the array is not an array * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array) { - return new ArrayListIterator(array); + public static <E> ResettableListIterator<E> arrayListIterator(Object array) { + return new ArrayListIterator<E>(array); } /** @@ -346,8 +350,8 @@ public class IteratorUtils { * @throws IndexOutOfBoundsException if start is less than zero * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array, int start) { - return new ObjectArrayListIterator(array, start); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start) { + return new ObjectArrayListIterator<E>(array, start); } /** @@ -363,8 +367,8 @@ public class IteratorUtils { * @throws IndexOutOfBoundsException if start is less than zero * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array, int start) { - return new ArrayListIterator(array, start); + public static <E> ResettableListIterator<E> arrayListIterator(Object array, int start) { + return new ArrayListIterator<E>(array, start); } /** @@ -378,8 +382,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object[] array, int start, int end) { - return new ObjectArrayListIterator(array, start, end); + public static <E> ResettableListIterator<E> arrayListIterator(E[] array, int start, int end) { + return new ObjectArrayListIterator<E>(array, start, end); } /** @@ -397,8 +401,8 @@ public class IteratorUtils { * @throws IllegalArgumentException if end is before start * @throws NullPointerException if array is null */ - public static ResettableListIterator arrayListIterator(Object array, int start, int end) { - return new ArrayListIterator(array, start, end); + public static <E> ResettableListIterator<E> arrayListIterator(Object array, int start, int end) { + return new ArrayListIterator<E>(array, start, end); } // Unmodifiable @@ -411,7 +415,7 @@ public class IteratorUtils { * @param iterator the iterator to make immutable * @return an immutable version of the iterator */ - public static Iterator unmodifiableIterator(Iterator iterator) { + public static <E> Iterator<E> unmodifiableIterator(Iterator<E> iterator) { return UnmodifiableIterator.decorate(iterator); } @@ -424,7 +428,7 @@ public class IteratorUtils { * @param listIterator the iterator to make immutable * @return an immutable version of the iterator */ - public static ListIterator unmodifiableListIterator(ListIterator listIterator) { + public static <E> ListIterator<E> unmodifiableListIterator(ListIterator<E> listIterator) { return UnmodifiableListIterator.decorate(listIterator); } @@ -436,7 +440,7 @@ public class IteratorUtils { * @param mapIterator the iterator to make immutable * @return an immutable version of the iterator */ - public static MapIterator unmodifiableMapIterator(MapIterator mapIterator) { + public static <K, V> MapIterator<K, V> unmodifiableMapIterator(MapIterator<K, V> mapIterator) { return UnmodifiableMapIterator.decorate(mapIterator); } @@ -451,8 +455,8 @@ public class IteratorUtils { * @return a combination iterator over the iterators * @throws NullPointerException if either iterator is null */ - public static Iterator chainedIterator(Iterator iterator1, Iterator iterator2) { - return new IteratorChain(iterator1, iterator2); + public static <E> Iterator<E> chainedIterator(Iterator<? extends E> iterator1, Iterator<? extends E> iterator2) { + return new IteratorChain<E>(iterator1, iterator2); } /** @@ -463,8 +467,8 @@ public class IteratorUtils { * @return a combination iterator over the iterators * @throws NullPointerException if iterators array is null or contains a null */ - public static Iterator chainedIterator(Iterator[] iterators) { - return new IteratorChain(iterators); + public static <E> Iterator<E> chainedIterator(Iterator<? extends E>[] iterators) { + return new IteratorChain<E>(iterators); } /** @@ -476,8 +480,8 @@ public class IteratorUtils { * @throws NullPointerException if iterators collection is null or contains a null * @throws ClassCastException if the iterators collection contains the wrong object type */ - public static Iterator chainedIterator(Collection iterators) { - return new IteratorChain(iterators); + public static <E> Iterator<E> chainedIterator(Collection<Iterator<? extends E>> iterators) { + return new IteratorChain<E>(iterators); } // Collated @@ -498,8 +502,8 @@ public class IteratorUtils { * @return a combination iterator over the iterators * @throws NullPointerException if either iterator is null */ - public static Iterator collatedIterator(Comparator comparator, Iterator iterator1, Iterator iterator2) { - return new CollatingIterator(comparator, iterator1, iterator2); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E> iterator1, Iterator<? extends E> iterator2) { + return new CollatingIterator<E>(comparator, iterator1, iterator2); } /** @@ -517,8 +521,8 @@ public class IteratorUtils { * @return a combination iterator over the iterators * @throws NullPointerException if iterators array is null or contains a null */ - public static Iterator collatedIterator(Comparator comparator, Iterator[] iterators) { - return new CollatingIterator(comparator, iterators); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, Iterator<? extends E>[] iterators) { + return new CollatingIterator<E>(comparator, iterators); } /** @@ -537,8 +541,9 @@ public class IteratorUtils { * @throws NullPointerException if iterators collection is null or contains a null * @throws ClassCastException if the iterators collection contains the wrong object type */ - public static Iterator collatedIterator(Comparator comparator, Collection iterators) { - return new CollatingIterator(comparator, iterators); + public static <E> Iterator<E> collatedIterator(Comparator<? super E> comparator, + Collection<Iterator<? extends E>> iterators) { + return new CollatingIterator<E>(comparator, iterators); } // Object Graph @@ -596,8 +601,8 @@ public class IteratorUtils { * @return a new object graph iterator * @since Commons Collections 3.1 */ - public static Iterator objectGraphIterator(Object root, Transformer transformer) { - return new ObjectGraphIterator(root, transformer); + public static <E> Iterator<E> objectGraphIterator(E root, Transformer<? super E, ? extends E> transformer) { + return new ObjectGraphIterator<E>(root, transformer); } // Transformed @@ -613,14 +618,14 @@ public class IteratorUtils { * @return a new transforming iterator * @throws NullPointerException if either parameter is null */ - public static Iterator transformedIterator(Iterator iterator, Transformer transform) { + public static <I, O> Iterator<O> transformedIterator(Iterator<? extends I> iterator, Transformer<? super I, ? extends O> transform) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (transform == null) { throw new NullPointerException("Transformer must not be null"); } - return new TransformIterator(iterator, transform); + return new TransformIterator<I, O>(iterator, transform); } // Filtered @@ -636,14 +641,14 @@ public class IteratorUtils { * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ - public static Iterator filteredIterator(Iterator iterator, Predicate predicate) { + public static <E> Iterator<E> filteredIterator(Iterator<? extends E> iterator, Predicate<? super E> predicate) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } - return new FilterIterator(iterator, predicate); + return new FilterIterator<E>(iterator, predicate); } /** @@ -657,14 +662,14 @@ public class IteratorUtils { * @return a new filtered iterator * @throws NullPointerException if either parameter is null */ - public static ListIterator filteredListIterator(ListIterator listIterator, Predicate predicate) { + public static <E> ListIterator<E> filteredListIterator(ListIterator<? extends E> listIterator, Predicate<? super E> predicate) { if (listIterator == null) { throw new NullPointerException("ListIterator must not be null"); } if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } - return new FilterListIterator(listIterator, predicate); + return new FilterListIterator<E>(listIterator, predicate); } // Looping @@ -680,11 +685,11 @@ public class IteratorUtils { * @return a new looping iterator * @throws NullPointerException if the collection is null */ - public static ResettableIterator loopingIterator(Collection coll) { + public static <E> ResettableIterator<E> loopingIterator(Collection<? extends E> coll) { if (coll == null) { throw new NullPointerException("Collection must not be null"); } - return new LoopingIterator(coll); + return new LoopingIterator<E>(coll); } /** @@ -698,13 +703,13 @@ public class IteratorUtils { * @throws NullPointerException if the list is null * @since Commons Collections 3.2 */ - public static ResettableListIterator loopingListIterator(List list) { + public static <E> ResettableListIterator<E> loopingListIterator(List<E> list) { if (list == null) { throw new NullPointerException("List must not be null"); } - return new LoopingListIterator(list); + return new LoopingListIterator<E>(list); } - + // Views //----------------------------------------------------------------------- /** @@ -713,11 +718,11 @@ public class IteratorUtils { * @param enumeration the enumeration to use * @return a new iterator */ - public static Iterator asIterator(Enumeration enumeration) { + public static <E> Iterator<E> asIterator(Enumeration<? extends E> enumeration) { if (enumeration == null) { throw new NullPointerException("Enumeration must not be null"); } - return new EnumerationIterator(enumeration); + return new EnumerationIterator<E>(enumeration); } /** @@ -728,14 +733,14 @@ public class IteratorUtils { * @param removeCollection the collection to remove elements from * @return a new iterator */ - public static Iterator asIterator(Enumeration enumeration, Collection removeCollection) { + public static <E> Iterator<E> asIterator(Enumeration<? extends E> enumeration, Collection<? super E> removeCollection) { if (enumeration == null) { throw new NullPointerException("Enumeration must not be null"); } if (removeCollection == null) { throw new NullPointerException("Collection must not be null"); } - return new EnumerationIterator(enumeration, removeCollection); + return new EnumerationIterator<E>(enumeration, removeCollection); } /** @@ -745,11 +750,11 @@ public class IteratorUtils { * @return a new enumeration * @throws NullPointerException if iterator is null */ - public static Enumeration asEnumeration(Iterator iterator) { + public static <E> Enumeration<E> asEnumeration(Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - return new IteratorEnumeration(iterator); + return new IteratorEnumeration<E>(iterator); } /** @@ -762,11 +767,11 @@ public class IteratorUtils { * @return a new iterator * @throws NullPointerException if iterator parameter is null */ - public static ListIterator toListIterator(Iterator iterator) { + public static <E> ListIterator<E> toListIterator(Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - return new ListIteratorWrapper(iterator); + return new ListIteratorWrapper<E>(iterator); } /** @@ -779,14 +784,14 @@ public class IteratorUtils { * @return an array of the iterator contents * @throws NullPointerException if iterator parameter is null */ - public static Object[] toArray(Iterator iterator) { + public static Object[] toArray(Iterator<?> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } - List list = toList(iterator, 100); + List<?> list = toList(iterator, 100); return list.toArray(); } - + /** * Gets an array based on an iterator. * <p> @@ -800,15 +805,16 @@ public class IteratorUtils { * @throws NullPointerException if arrayClass is null * @throws ClassCastException if the arrayClass is invalid */ - public static Object[] toArray(Iterator iterator, Class arrayClass) { + @SuppressWarnings("unchecked") + public static <E> E[] toArray(Iterator<? extends E> iterator, Class<E> arrayClass) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (arrayClass == null) { throw new NullPointerException("Array class must not be null"); } - List list = toList(iterator, 100); - return list.toArray((Object[]) Array.newInstance(arrayClass, list.size())); + List<E> list = toList(iterator, 100); + return list.toArray((E[]) Array.newInstance(arrayClass, list.size())); } /** @@ -821,10 +827,10 @@ public class IteratorUtils { * @return a list of the iterator contents * @throws NullPointerException if iterator parameter is null */ - public static List toList(Iterator iterator) { + public static <E> List<E> toList(Iterator<? extends E> iterator) { return toList(iterator, 10); } - + /** * Gets a list based on an iterator. * <p> @@ -837,14 +843,14 @@ public class IteratorUtils { * @throws NullPointerException if iterator parameter is null * @throws IllegalArgumentException if the size is less than 1 */ - public static List toList(Iterator iterator, int estimatedSize) { + public static <E> List<E> toList(Iterator<? extends E> iterator, int estimatedSize) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } if (estimatedSize < 1) { throw new IllegalArgumentException("Estimated size must be greater than 0"); } - List list = new ArrayList(estimatedSize); + List<E> list = new ArrayList<E>(estimatedSize); while (iterator.hasNext()) { list.add(iterator.next()); } @@ -854,7 +860,7 @@ public class IteratorUtils { /** * Gets a suitable Iterator for the given object. * <p> - * This method can handles objects as follows + * This method can handle objects as follows * <ul> * <li>null - empty iterator * <li>Iterator - returned directly @@ -870,45 +876,44 @@ public class IteratorUtils { * @param obj the object to convert to an iterator * @return a suitable iterator, never null */ - public static Iterator getIterator(Object obj) { + @SuppressWarnings("unchecked") + public static Iterator<?> getIterator(Object obj) { if (obj == null) { return emptyIterator(); - - } else if (obj instanceof Iterator) { + } + if (obj instanceof Iterator) { return (Iterator) obj; - - } else if (obj instanceof Collection) { + } + if (obj instanceof Collection) { return ((Collection) obj).iterator(); - - } else if (obj instanceof Object[]) { + } + if (obj instanceof Object[]) { return new ObjectArrayIterator((Object[]) obj); - - } else if (obj instanceof Enumeration) { + } + if (obj instanceof Enumeration) { return new EnumerationIterator((Enumeration) obj); - - } else if (obj instanceof Map) { + } + if (obj instanceof Map) { return ((Map) obj).values().iterator(); - - } else if (obj instanceof Dictionary) { + } + if (obj instanceof Dictionary) { return new EnumerationIterator(((Dictionary) obj).elements()); - - } else if (obj != null && obj.getClass().isArray()) { + } + if (obj != null && obj.getClass().isArray()) { return new ArrayIterator(obj); - - } else { - try { - Method method = obj.getClass().getMethod("iterator", (Class[]) null); - if (Iterator.class.isAssignableFrom(method.getReturnType())) { - Iterator it = (Iterator) method.invoke(obj, (Object[]) null); - if (it != null) { - return it; - } + } + try { + Method method = obj.getClass().getMethod("iterator", (Class[]) null); + if (Iterator.class.isAssignableFrom(method.getReturnType())) { + Iterator it = (Iterator) method.invoke(obj, (Object[]) null); + if (it != null) { + return it; } - } catch (Exception ex) { - // ignore } - return singletonIterator(obj); + } catch (Exception ex) { + // ignore } + return singletonIterator(obj); } }
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/ListUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/ListUtils.java b/src/java/org/apache/commons/collections/ListUtils.java index b138717..fc5868d 100644 --- a/src/java/org/apache/commons/collections/ListUtils.java +++ b/src/java/org/apache/commons/collections/ListUtils.java @@ -49,8 +49,8 @@ public class ListUtils { * This uses the {@link Collections Collections} implementation * 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. */ @@ -67,18 +67,14 @@ public class ListUtils { * @return the intersection of those two lists * @throws NullPointerException if either list is null */ - public static List intersection(final List list1, final List list2) { - final List result = new ArrayList(); - final Iterator iterator = list2.iterator(); - - while (iterator.hasNext()) { - final Object o = iterator.next(); + public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) { + final List<E> result = new ArrayList<E>(); - if (list1.contains(o)) { - result.add(o); + for (E e : list2) { + if (list1.contains(e)) { + result.add(e); } } - return result; } @@ -97,14 +93,11 @@ public class ListUtils { * @return a new list containing the results * @throws NullPointerException if either list is null */ - public static List subtract(final List list1, final List list2) { - final ArrayList result = new ArrayList(list1); - final Iterator iterator = list2.iterator(); - - while (iterator.hasNext()) { - result.remove(iterator.next()); + public static <E> List<E> subtract(final List<E> list1, final List<? extends E> list2) { + final ArrayList<E> result = new ArrayList<E>(list1); + for (E e : list2) { + result.remove(e); } - return result; } @@ -117,7 +110,7 @@ public class ListUtils { * @return a new list containing the sum of those lists * @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)); } @@ -131,8 +124,8 @@ public class ListUtils { * @return a new list containing the union of those lists * @throws NullPointerException if either list is null */ - public static List union(final List list1, final List list2) { - final ArrayList result = new ArrayList(list1); + public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2) { + final ArrayList<E> result = new ArrayList<E>(list1); result.addAll(list2); return result; } @@ -166,7 +159,7 @@ public class ListUtils { * @param list2 the second list, may be null * @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) { return true; } @@ -174,8 +167,8 @@ public class ListUtils { return false; } - Iterator it1 = list1.iterator(); - Iterator it2 = list2.iterator(); + Iterator<?> it1 = list1.iterator(); + Iterator<?> it2 = list2.iterator(); Object obj1 = null; Object obj2 = null; @@ -306,7 +299,7 @@ public class ListUtils { * @return an unmodifiable list backed by the given list * @throws IllegalArgumentException if the list is null */ - public static <E> List unmodifiableList(List<E> list) { + public static <E> List<E> unmodifiableList(List<E> list) { return UnmodifiableList.decorate(list); } @@ -339,7 +332,7 @@ public class ListUtils { * @return a transformed list backed by the given list * @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); } @@ -372,7 +365,7 @@ public class ListUtils { * @return a lazy list backed by the given list * @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); } @@ -386,7 +379,7 @@ public class ListUtils { * @return a fixed-size list backed by that list * @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); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/MapUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/MapUtils.java b/src/java/org/apache/commons/collections/MapUtils.java index 6ea9434..b3c33a9 100644 --- a/src/java/org/apache/commons/collections/MapUtils.java +++ b/src/java/org/apache/commons/collections/MapUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.collections; import java.io.PrintStream; import java.text.NumberFormat; import java.text.ParseException; +import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; @@ -91,12 +92,14 @@ public class MapUtils { * An empty unmodifiable map. * This was not provided in JDK1.2. */ - public static final Map EMPTY_MAP = UnmodifiableMap.decorate(new HashMap(1)); + public static final Map<Object, Object> EMPTY_MAP = UnmodifiableMap.decorate(new HashMap<Object, Object>(1)); + /** * An empty unmodifiable sorted map. * This is not provided in the JDK. */ - public static final SortedMap EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap()); + public static final SortedMap<Object, Object> EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap<Object, Object>()); + /** * String used to indent the verbose and debug Map prints. */ @@ -107,7 +110,7 @@ public class MapUtils { */ public MapUtils() { } - + // Type safe getters //------------------------------------------------------------------------- /** @@ -117,7 +120,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map, <code>null</code> if null map input */ - public static Object getObject(final Map map, final Object key) { + public static <K, V> V getObject(final Map<? super K, V> map, final K key) { if (map != null) { return map.get(key); } @@ -133,7 +136,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a String, <code>null</code> if null map input */ - public static String getString(final Map map, final Object key) { + public static <K> String getString(final Map<? super K, ?> map, final K key) { if (map != null) { Object answer = map.get(key); if (answer != null) { @@ -157,17 +160,17 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Boolean, <code>null</code> if null map input */ - public static Boolean getBoolean(final Map map, final Object key) { + public static <K> Boolean getBoolean(final Map<? super K, ?> map, final K key) { if (map != null) { Object answer = map.get(key); if (answer != null) { if (answer instanceof Boolean) { return (Boolean) answer; - - } else if (answer instanceof String) { + } + if (answer instanceof String) { return new Boolean((String) answer); - - } else if (answer instanceof Number) { + } + if (answer instanceof Number) { Number n = (Number) answer; return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE; } @@ -189,18 +192,17 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Number, <code>null</code> if null map input */ - public static Number getNumber(final Map map, final Object key) { + public static <K> Number getNumber(final Map<? super K, ?> map, final K key) { if (map != null) { Object answer = map.get(key); if (answer != null) { if (answer instanceof Number) { return (Number) answer; - - } else if (answer instanceof String) { + } + if (answer instanceof String) { try { String text = (String) answer; return NumberFormat.getInstance().parse(text); - } catch (ParseException e) { logInfo(e); } @@ -219,11 +221,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Byte, <code>null</code> if null map input */ - public static Byte getByte(final Map map, final Object key) { + public static <K> Byte getByte(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Byte) { + } + if (answer instanceof Byte) { return (Byte) answer; } return new Byte(answer.byteValue()); @@ -238,11 +241,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Short, <code>null</code> if null map input */ - public static Short getShort(final Map map, final Object key) { + public static <K> Short getShort(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Short) { + } + if (answer instanceof Short) { return (Short) answer; } return new Short(answer.shortValue()); @@ -257,11 +261,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Integer, <code>null</code> if null map input */ - public static Integer getInteger(final Map map, final Object key) { + public static <K> Integer getInteger(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Integer) { + } + if (answer instanceof Integer) { return (Integer) answer; } return new Integer(answer.intValue()); @@ -276,11 +281,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Long, <code>null</code> if null map input */ - public static Long getLong(final Map map, final Object key) { + public static <K> Long getLong(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Long) { + } + if (answer instanceof Long) { return (Long) answer; } return new Long(answer.longValue()); @@ -295,11 +301,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Float, <code>null</code> if null map input */ - public static Float getFloat(final Map map, final Object key) { + public static <K> Float getFloat(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Float) { + } + if (answer instanceof Float) { return (Float) answer; } return new Float(answer.floatValue()); @@ -314,11 +321,12 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Double, <code>null</code> if null map input */ - public static Double getDouble(final Map map, final Object key) { + public static <K> Double getDouble(final Map<? super K, ?> map, final K key) { Number answer = getNumber(map, key); if (answer == null) { return null; - } else if (answer instanceof Double) { + } + if (answer instanceof Double) { return (Double) answer; } return new Double(answer.doubleValue()); @@ -334,11 +342,11 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Map, <code>null</code> if null map input */ - public static Map getMap(final Map map, final Object key) { + public static <K> Map<?, ?> getMap(final Map<? super K, ?> map, final K key) { if (map != null) { Object answer = map.get(key); if (answer != null && answer instanceof Map) { - return (Map) answer; + return (Map<?, ?>) answer; } } return null; @@ -356,10 +364,10 @@ public class MapUtils { * @return the value in the map, or defaultValue if the original value * is null or the map is null */ - public static Object getObject( Map map, Object key, Object defaultValue ) { - if ( map != null ) { - Object answer = map.get( key ); - if ( answer != null ) { + public static <K, V> V getObject(Map<K, V> map, K key, V defaultValue) { + if (map != null) { + V answer = map.get(key); + if (answer != null) { return answer; } } @@ -378,9 +386,9 @@ public class MapUtils { * original value is null, the map is null or the string conversion * fails */ - public static String getString( Map map, Object key, String defaultValue ) { - String answer = getString( map, key ); - if ( answer == null ) { + public static <K> String getString(Map<? super K, ?> map, K key, String defaultValue) { + String answer = getString(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -398,9 +406,9 @@ public class MapUtils { * original value is null, the map is null or the boolean conversion * fails */ - public static Boolean getBoolean( Map map, Object key, Boolean defaultValue ) { - Boolean answer = getBoolean( map, key ); - if ( answer == null ) { + public static <K> Boolean getBoolean(Map<? super K, ?> map, K key, Boolean defaultValue) { + Boolean answer = getBoolean(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -418,9 +426,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Number getNumber( Map map, Object key, Number defaultValue ) { - Number answer = getNumber( map, key ); - if ( answer == null ) { + public static <K> Number getNumber(Map<? super K, ?> map, K key, Number defaultValue) { + Number answer = getNumber(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -438,9 +446,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Byte getByte( Map map, Object key, Byte defaultValue ) { - Byte answer = getByte( map, key ); - if ( answer == null ) { + public static <K> Byte getByte(Map<? super K, ?> map, K key, Byte defaultValue) { + Byte answer = getByte(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -458,9 +466,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Short getShort( Map map, Object key, Short defaultValue ) { - Short answer = getShort( map, key ); - if ( answer == null ) { + public static <K> Short getShort(Map<? super K, ?> map, K key, Short defaultValue) { + Short answer = getShort(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -478,9 +486,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Integer getInteger( Map map, Object key, Integer defaultValue ) { - Integer answer = getInteger( map, key ); - if ( answer == null ) { + public static <K> Integer getInteger(Map<? super K, ?> map, K key, Integer defaultValue) { + Integer answer = getInteger(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -498,9 +506,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Long getLong( Map map, Object key, Long defaultValue ) { - Long answer = getLong( map, key ); - if ( answer == null ) { + public static <K> Long getLong(Map<? super K, ?> map, K key, Long defaultValue) { + Long answer = getLong(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -518,9 +526,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Float getFloat( Map map, Object key, Float defaultValue ) { - Float answer = getFloat( map, key ); - if ( answer == null ) { + public static <K> Float getFloat(Map<? super K, ?> map, K key, Float defaultValue) { + Float answer = getFloat(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -538,9 +546,9 @@ public class MapUtils { * original value is null, the map is null or the number conversion * fails */ - public static Double getDouble( Map map, Object key, Double defaultValue ) { - Double answer = getDouble( map, key ); - if ( answer == null ) { + public static <K> Double getDouble(Map<? super K, ?> map, K key, Double defaultValue) { + Double answer = getDouble(map, key); + if (answer == null) { answer = defaultValue; } return answer; @@ -558,14 +566,13 @@ public class MapUtils { * original value is null, the map is null or the map conversion * fails */ - public static Map getMap( Map map, Object key, Map defaultValue ) { - Map answer = getMap( map, key ); - if ( answer == null ) { + public static <K> Map<?, ?> getMap(Map<? super K, ?> map, K key, Map<?, ?> defaultValue) { + Map<?, ?> answer = getMap(map, key); + if (answer == null) { answer = defaultValue; } return answer; } - // Type safe primitive getters //------------------------------------------------------------------------- @@ -583,12 +590,8 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a Boolean, <code>false</code> if null map input */ - public static boolean getBooleanValue(final Map map, final Object key) { - Boolean booleanObject = getBoolean(map, key); - if (booleanObject == null) { - return false; - } - return booleanObject.booleanValue(); + public static <K> boolean getBooleanValue(final Map<? super K, ?> map, final K key) { + return Boolean.TRUE.equals(getBoolean(map, key)); } /** @@ -600,7 +603,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a byte, <code>0</code> if null map input */ - public static byte getByteValue(final Map map, final Object key) { + public static <K> byte getByteValue(final Map<? super K, ?> map, final K key) { Byte byteObject = getByte(map, key); if (byteObject == null) { return 0; @@ -617,7 +620,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a short, <code>0</code> if null map input */ - public static short getShortValue(final Map map, final Object key) { + public static <K> short getShortValue(final Map<? super K, ?> map, final K key) { Short shortObject = getShort(map, key); if (shortObject == null) { return 0; @@ -634,7 +637,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as an int, <code>0</code> if null map input */ - public static int getIntValue(final Map map, final Object key) { + public static <K> int getIntValue(final Map<? super K, ?> map, final K key) { Integer integerObject = getInteger(map, key); if (integerObject == null) { return 0; @@ -651,7 +654,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a long, <code>0L</code> if null map input */ - public static long getLongValue(final Map map, final Object key) { + public static <K> long getLongValue(final Map<? super K, ?> map, final K key) { Long longObject = getLong(map, key); if (longObject == null) { return 0L; @@ -668,7 +671,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a float, <code>0.0F</code> if null map input */ - public static float getFloatValue(final Map map, final Object key) { + public static <K> float getFloatValue(final Map<? super K, ?> map, final K key) { Float floatObject = getFloat(map, key); if (floatObject == null) { return 0f; @@ -685,7 +688,7 @@ public class MapUtils { * @param key the key to look up * @return the value in the Map as a double, <code>0.0</code> if null map input */ - public static double getDoubleValue(final Map map, final Object key) { + public static <K> double getDoubleValue(final Map<? super K, ?> map, final K key) { Double doubleObject = getDouble(map, key); if (doubleObject == null) { return 0d; @@ -712,7 +715,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a Boolean, <code>defaultValue</code> if null map input */ - public static boolean getBooleanValue(final Map map, final Object key, boolean defaultValue) { + public static <K> boolean getBooleanValue(final Map<? super K, ?> map, final K key, boolean defaultValue) { Boolean booleanObject = getBoolean(map, key); if (booleanObject == null) { return defaultValue; @@ -732,7 +735,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a byte, <code>defaultValue</code> if null map input */ - public static byte getByteValue(final Map map, final Object key, byte defaultValue) { + public static <K> byte getByteValue(final Map<? super K, ?> map, final K key, byte defaultValue) { Byte byteObject = getByte(map, key); if (byteObject == null) { return defaultValue; @@ -752,7 +755,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a short, <code>defaultValue</code> if null map input */ - public static short getShortValue(final Map map, final Object key, short defaultValue) { + public static <K> short getShortValue(final Map<? super K, ?> map, final K key, short defaultValue) { Short shortObject = getShort(map, key); if (shortObject == null) { return defaultValue; @@ -772,7 +775,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as an int, <code>defaultValue</code> if null map input */ - public static int getIntValue(final Map map, final Object key, int defaultValue) { + public static <K> int getIntValue(final Map<? super K, ?> map, final K key, int defaultValue) { Integer integerObject = getInteger(map, key); if (integerObject == null) { return defaultValue; @@ -792,7 +795,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a long, <code>defaultValue</code> if null map input */ - public static long getLongValue(final Map map, final Object key, long defaultValue) { + public static <K> long getLongValue(final Map<? super K, ?> map, final K key, long defaultValue) { Long longObject = getLong(map, key); if (longObject == null) { return defaultValue; @@ -812,7 +815,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a float, <code>defaultValue</code> if null map input */ - public static float getFloatValue(final Map map, final Object key, float defaultValue) { + public static <K> float getFloatValue(final Map<? super K, ?> map, final K key, float defaultValue) { Float floatObject = getFloat(map, key); if (floatObject == null) { return defaultValue; @@ -832,7 +835,7 @@ public class MapUtils { * conversion fails * @return the value in the Map as a double, <code>defaultValue</code> if null map input */ - public static double getDoubleValue(final Map map, final Object key, double defaultValue) { + public static <K> double getDoubleValue(final Map<? super K, ?> map, final K key, double defaultValue) { Double doubleObject = getDouble(map, key); if (doubleObject == null) { return defaultValue; @@ -849,11 +852,11 @@ public class MapUtils { * @param map the map to convert to a Properties object, may not be null * @return the properties object */ - public static Properties toProperties(final Map map) { + public static <K, V> Properties toProperties(final Map<K, V> map) { Properties answer = new Properties(); if (map != null) { - for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { - Map.Entry entry = (Map.Entry) iter.next(); + for (Iterator<Map.Entry<K, V>> iter = map.entrySet().iterator(); iter.hasNext();) { + Map.Entry<?, ?> entry = iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); answer.put(key, value); @@ -869,16 +872,16 @@ public class MapUtils { * @return the hashmap containing the data * @throws NullPointerException if the bundle is null */ - public static Map toMap(final ResourceBundle resourceBundle) { - Enumeration enumeration = resourceBundle.getKeys(); - Map map = new HashMap(); + public static Map<String, Object> toMap(final ResourceBundle resourceBundle) { + Enumeration<String> enumeration = resourceBundle.getKeys(); + Map<String, Object> map = new HashMap<String, Object>(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); Object value = resourceBundle.getObject(key); map.put(key, value); } - + return map; } @@ -905,9 +908,9 @@ public class MapUtils { public static void verbosePrint( final PrintStream out, final Object label, - final Map map) { + final Map<?, ?> map) { - verbosePrintInternal(out, label, map, new ArrayStack(), false); + verbosePrintInternal(out, label, map, new ArrayStack<Map<?, ?>>(), false); } /** @@ -931,9 +934,9 @@ public class MapUtils { public static void debugPrint( final PrintStream out, final Object label, - final Map map) { + final Map<?, ?> map) { - verbosePrintInternal(out, label, map, new ArrayStack(), true); + verbosePrintInternal(out, label, map, new ArrayStack<Map<?, ?>>(), true); } // Implementation methods @@ -975,8 +978,8 @@ public class MapUtils { private static void verbosePrintInternal( final PrintStream out, final Object label, - final Map map, - final ArrayStack lineage, + final Map<?, ?> map, + final ArrayStack<Map<?, ?>> lineage, final boolean debug) { printIndent(out, lineage.size()); @@ -999,15 +1002,14 @@ public class MapUtils { lineage.push(map); - for (Iterator it = map.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); + for (Map.Entry<?, ?> entry : map.entrySet()) { Object childKey = entry.getKey(); Object childValue = entry.getValue(); if (childValue instanceof Map && !lineage.contains(childValue)) { verbosePrintInternal( out, (childKey == null ? "null" : childKey), - (Map) childValue, + (Map<?, ?>) childValue, lineage, debug); } else { @@ -1026,7 +1028,7 @@ public class MapUtils { + (lineage.size() - 1 - lineageIndex - 1) + "] Map)"); } - + if (debug && childValue != null) { out.print(' '); out.println(childValue.getClass().getName()); @@ -1035,7 +1037,7 @@ public class MapUtils { } } } - + lineage.pop(); printIndent(out, lineage.size()); @@ -1052,7 +1054,7 @@ public class MapUtils { out.print(INDENT_STRING); } } - + // Misc //----------------------------------------------------------------------- /** @@ -1068,15 +1070,15 @@ public class MapUtils { * @return a new HashMap containing the inverted data * @throws NullPointerException if the map is null */ - public static Map invertMap(Map map) { - Map out = new HashMap(map.size()); - for (Iterator it = map.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); + public static <K, V> Map<V, K> invertMap(Map<K, V> map) { + Map<V, K> out = new HashMap<V, K>(map.size()); + for (Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry<K, V> entry = it.next(); out.put(entry.getValue(), entry.getKey()); } return out; } - + //----------------------------------------------------------------------- /** * Protects against adding null values to a map. @@ -1089,18 +1091,16 @@ public class MapUtils { * which should be held in the same way in the map. * <p> * Keys are not validated. + * Note that this method can be used to circumvent the map's + * value type at runtime. * * @param map the map to add to, may not be null * @param key the key * @param value the value, null converted to "" * @throws NullPointerException if the map is null */ - public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException { - if (value == null) { - map.put(key, ""); - } else { - map.put(key, value); - } + public static <K> void safeAddToMap(Map<? super K, Object> map, K key, Object value) throws NullPointerException { + map.put(key, value == null ? "" : value); } //----------------------------------------------------------------------- @@ -1151,7 +1151,8 @@ public class MapUtils { * @throws ClassCastException if the array contents is mixed * @since Commons Collections 3.2 */ - public static Map putAll(Map map, Object[] array) { + @SuppressWarnings("unchecked") + public static <K, V> Map<K, V> putAll(Map<K, V> map, Object[] array) { map.size(); // force NPE if (array == null || array.length == 0) { return map; @@ -1159,12 +1160,12 @@ public class MapUtils { Object obj = array[0]; if (obj instanceof Map.Entry) { for (int i = 0; i < array.length; i++) { - Map.Entry entry = (Map.Entry) array[i]; + Map.Entry<K, V> entry = (Map.Entry<K, V>) array[i]; map.put(entry.getKey(), entry.getValue()); } } else if (obj instanceof KeyValue) { for (int i = 0; i < array.length; i++) { - KeyValue keyval = (KeyValue) array[i]; + KeyValue<K, V> keyval = (KeyValue<K, V>) array[i]; map.put(keyval.getKey(), keyval.getValue()); } } else if (obj instanceof Object[]) { @@ -1173,11 +1174,11 @@ public class MapUtils { if (sub == null || sub.length < 2) { throw new IllegalArgumentException("Invalid array element: " + i); } - map.put(sub[0], sub[1]); + map.put((K) sub[0], (V) sub[1]); } } else { for (int i = 0; i < array.length - 1;) { - map.put(array[i++], array[i++]); + map.put((K) array[i++], (V) array[i++]); } } return map; @@ -1193,6 +1194,7 @@ public class MapUtils { * @return true if empty or null * @since Commons Collections 3.2 */ + @SuppressWarnings("unchecked") public static boolean isEmpty(Map map) { return (map == null || map.isEmpty()); } @@ -1206,6 +1208,7 @@ public class MapUtils { * @return true if non-null and non-empty * @since Commons Collections 3.2 */ + @SuppressWarnings("unchecked") public static boolean isNotEmpty(Map map) { return !MapUtils.isEmpty(map); } @@ -1235,7 +1238,7 @@ public class MapUtils { * @return a synchronized map backed by the given map * @throws IllegalArgumentException if the map is null */ - public static Map synchronizedMap(Map map) { + public static <K, V> Map<K, V> synchronizedMap(Map<K, V> map) { return Collections.synchronizedMap(map); } @@ -1248,7 +1251,7 @@ public class MapUtils { * @return an unmodifiable map backed by the given map * @throws IllegalArgumentException if the map is null */ - public static Map unmodifiableMap(Map map) { + public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) { return UnmodifiableMap.decorate(map); } @@ -1267,7 +1270,7 @@ public class MapUtils { * @return a predicated map backed by the given map * @throws IllegalArgumentException if the Map is null */ - public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred) { + public static <K, V> Map<K, V> predicatedMap(Map<K, V> map, Predicate<? super K> keyPred, Predicate<? super V> valuePred) { return PredicatedMap.decorate(map, keyPred, valuePred); } @@ -1292,7 +1295,9 @@ public class MapUtils { * @return a transformed map backed by the given map * @throws IllegalArgumentException if the Map is null */ - public static Map transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) { + public static <K, V> Map<K, V> transformedMap(Map<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { return TransformedMap.decorate(map, keyTransformer, valueTransformer); } @@ -1306,7 +1311,7 @@ public class MapUtils { * @return a fixed-size map backed by that map * @throws IllegalArgumentException if the Map is null */ - public static Map fixedSizeMap(Map map) { + public static <K, V> Map<K, V> fixedSizeMap(Map<K, V> map) { return FixedSizeMap.decorate(map); } @@ -1338,8 +1343,8 @@ public class MapUtils { * @return a lazy map backed by the given map * @throws IllegalArgumentException if the Map or Factory is null */ - public static Map lazyMap(Map map, Factory factory) { - return LazyMap.decorate(map, factory); + public static <K, V> Map<K, V> lazyMap(Map<K, V> map, Factory<? extends V> factory) { + return LazyMap.getLazyMap(map, factory); } /** @@ -1377,8 +1382,8 @@ public class MapUtils { * @return a lazy map backed by the given map * @throws IllegalArgumentException if the Map or Transformer is null */ - public static Map lazyMap(Map map, Transformer transformerFactory) { - return LazyMap.decorate(map, transformerFactory); + public static <K, V> Map<K, V> lazyMap(Map<K, V> map, Transformer<? super K, ? extends V> transformerFactory) { + return LazyMap.getLazyMap(map, transformerFactory); } /** @@ -1392,7 +1397,7 @@ public class MapUtils { * @return an ordered map backed by the given map * @throws IllegalArgumentException if the Map is null */ - public static Map orderedMap(Map map) { + public static <K, V> Map<K, V> orderedMap(Map<K, V> map) { return ListOrderedMap.decorate(map); } @@ -1405,8 +1410,8 @@ public class MapUtils { * @see MultiValueMap * @since Commons Collections 3.2 */ - public static Map multiValueMap(Map map) { - return MultiValueMap.decorate(map); + public static <K, V> MultiValueMap<K, V> multiValueMap(Map<K, ? super Collection<V>> map) { + return MultiValueMap.<K, V>decorate(map); } /** @@ -1420,7 +1425,7 @@ public class MapUtils { * @see MultiValueMap * @since Commons Collections 3.2 */ - public static Map multiValueMap(Map map, Class collectionClass) { + public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(Map<K, C> map, Class<C> collectionClass) { return MultiValueMap.decorate(map, collectionClass); } @@ -1435,7 +1440,7 @@ public class MapUtils { * @see MultiValueMap * @since Commons Collections 3.2 */ - public static Map multiValueMap(Map map, Factory collectionFactory) { + public static <K, V, C extends Collection<V>> MultiValueMap<K, V> multiValueMap(Map<K, C> map, Factory<C> collectionFactory) { return MultiValueMap.decorate(map, collectionFactory); } @@ -1464,7 +1469,7 @@ public class MapUtils { * @return a synchronized map backed by the given map * @throws IllegalArgumentException if the map is null */ - public static Map synchronizedSortedMap(SortedMap map) { + public static <K, V> SortedMap<K, V> synchronizedSortedMap(SortedMap<K, V> map) { return Collections.synchronizedSortedMap(map); } @@ -1477,7 +1482,7 @@ public class MapUtils { * @return an unmodifiable map backed by the given map * @throws IllegalArgumentException if the map is null */ - public static Map unmodifiableSortedMap(SortedMap map) { + public static <K, V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, V> map) { return UnmodifiableSortedMap.decorate(map); } @@ -1496,7 +1501,8 @@ public class MapUtils { * @return a predicated map backed by the given map * @throws IllegalArgumentException if the SortedMap is null */ - public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred) { + public static <K, V> SortedMap<K, V> predicatedSortedMap(SortedMap<K, V> map, + Predicate<? super K> keyPred, Predicate<? super V> valuePred) { return PredicatedSortedMap.decorate(map, keyPred, valuePred); } @@ -1521,7 +1527,9 @@ public class MapUtils { * @return a transformed map backed by the given map * @throws IllegalArgumentException if the SortedMap is null */ - public static SortedMap transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) { + public static <K, V> SortedMap<K, V> transformedSortedMap(SortedMap<K, V> map, + Transformer<? super K, ? extends K> keyTransformer, + Transformer<? super V, ? extends V> valueTransformer) { return TransformedSortedMap.decorate(map, keyTransformer, valueTransformer); } @@ -1535,7 +1543,7 @@ public class MapUtils { * @return a fixed-size map backed by that map * @throws IllegalArgumentException if the SortedMap is null */ - public static SortedMap fixedSizeSortedMap(SortedMap map) { + public static <K, V> SortedMap<K, V> fixedSizeSortedMap(SortedMap<K, V> map) { return FixedSizeSortedMap.decorate(map); } @@ -1568,8 +1576,9 @@ public class MapUtils { * @return a lazy map backed by the given map * @throws IllegalArgumentException if the SortedMap or Factory is null */ - public static SortedMap lazySortedMap(SortedMap map, Factory factory) { - return LazySortedMap.decorate(map, factory); + public static <K, V> SortedMap<K, V> lazySortedMap(SortedMap<K, V> map, + Factory<? extends V> factory) { + return LazySortedMap.getLazySortedMap(map, factory); } /** @@ -1607,8 +1616,9 @@ public class MapUtils { * @return a lazy map backed by the given map * @throws IllegalArgumentException if the Map or Transformer is null */ - public static SortedMap lazySortedMap(SortedMap map, Transformer transformerFactory) { - return LazySortedMap.decorate(map, transformerFactory); + public static <K, V> SortedMap<K, V> lazySortedMap(SortedMap<K, V> map, + Transformer<? super K, ? extends V> transformerFactory) { + return LazySortedMap.getLazySortedMap(map, transformerFactory); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/MultiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/MultiMap.java b/src/java/org/apache/commons/collections/MultiMap.java index fa2d727..388b56a 100644 --- a/src/java/org/apache/commons/collections/MultiMap.java +++ b/src/java/org/apache/commons/collections/MultiMap.java @@ -47,7 +47,7 @@ import java.util.Map; * @author James Strachan * @author Stephen Colebourne */ -public interface MultiMap extends Map { +public interface MultiMap<K, V> extends Map<K, Object> { /** * Removes a specific value from map. @@ -66,7 +66,7 @@ public interface MultiMap extends Map { * @throws ClassCastException if the key or value is of an invalid type * @throws NullPointerException if the key or value is null and null is invalid */ - public Object remove(Object key, Object item); + public V remove(K key, V item); //----------------------------------------------------------------------- /** @@ -98,7 +98,7 @@ public interface MultiMap extends Map { * @throws ClassCastException if the key is of an invalid type * @throws NullPointerException if the key is null and null keys are invalid */ - Object get(Object key); + Object get(K key); /** * Checks whether the map contains the value specified. @@ -129,7 +129,7 @@ public interface MultiMap extends Map { * @throws NullPointerException if the key or value is null and null is invalid * @throws IllegalArgumentException if the key or value is invalid */ - Object put(Object key, Object value); + Object put(K key, Object value); /** * Removes all values associated with the specified key. @@ -144,7 +144,7 @@ public interface MultiMap extends Map { * @throws ClassCastException if the key is of an invalid type * @throws NullPointerException if the key is null and null keys are invalid */ - Object remove(Object key); + Object remove(K key); /** * Gets a collection containing all the values in the map. @@ -155,6 +155,6 @@ public interface MultiMap extends Map { * * @return a collection view of the values contained in this map */ - Collection values(); + Collection<Object> values(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/OrderedBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/OrderedBidiMap.java b/src/java/org/apache/commons/collections/OrderedBidiMap.java index 70f6ca2..642d9df 100644 --- a/src/java/org/apache/commons/collections/OrderedBidiMap.java +++ b/src/java/org/apache/commons/collections/OrderedBidiMap.java @@ -47,20 +47,6 @@ public interface OrderedBidiMap<K, V> extends BidiMap<K, V>, OrderedMap<K, V> { * * @return an inverted bidirectional map */ - public BidiMap<V, K> inverseBidiMap(); - - /** - * Gets a view of this map where the keys and values are reversed. - * <p> - * Changes to one map will be visible in the other and vice versa. - * This enables both directions of the map to be accessed equally. - * <p> - * Implementations should seek to avoid creating a new object every time this - * method is called. See <code>AbstractMap.values()</code> etc. Calling this - * method on the inverse map should return the original. - * - * @return an inverted bidirectional map - */ - public OrderedBidiMap<V, K> inverseOrderedBidiMap(); + public OrderedBidiMap<V, K> inverseBidiMap(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/OrderedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/OrderedMap.java b/src/java/org/apache/commons/collections/OrderedMap.java index 83e34cd..7a45392 100644 --- a/src/java/org/apache/commons/collections/OrderedMap.java +++ b/src/java/org/apache/commons/collections/OrderedMap.java @@ -37,7 +37,7 @@ public interface OrderedMap<K, V> extends IterableMap<K, V> { * * @return a map iterator */ - OrderedMapIterator<K, V> orderedMapIterator(); + OrderedMapIterator<K, V> mapIterator(); /** * Gets the first key currently in this map.