JAPI 0.9.0
Yet another Java API
API Documentation

net.sf.japi.util
Class Collections2

java.lang.Object
  extended by net.sf.japi.util.Collections2

public class Collections2
extends Object

This class provides some additional utility methods you might miss in Collections. It is named Collections2 so you have no problems using both, this class and java.util.Collections (no name conflict).

Author:
Christian Hujer
See Also:
Collections

Method Summary
static
<T,C extends Collection<T>>
C
filter(C c, Filter<? super T> filter)
          Returns a collection only containing those elements accepted by the given filter.
static
<T extends Comparable<? super T>>
boolean
isSorted(List<T> list)
          Checks whether a list is sorted.
static
<T> boolean
isSorted(List<T> list, Comparator<? super T> c)
          Checks whether a list is sorted.
static
<T,C extends Collection<T>>
int
removeFilter(C collection, Filter<? super T> filter)
          Removes all elements from a collection not accepted by the given filter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

filter

public static final <T,C extends Collection<T>> C filter(C c,
                                                         Filter<? super T> filter)
Returns a collection only containing those elements accepted by the given filter. The original collection remains unmodified. This method tries its best to create a new, empty variant of the Collection passed in c:
  1. it's tried to create a new instance from the class of c using Reflection
  2. it's tried to Object.clone() c using Reflection and Collection.clear() the clone
  3. it's tried if c is a RandomAccess List, in which case an ArrayList is returned as an alternative

Parameters:
c - Collection to filter
filter - Filter to use for c
Returns:
collection containing only those elements accepted by the filter or null if the Collection could not be created.

removeFilter

public static final <T,C extends Collection<T>> int removeFilter(C collection,
                                                                 Filter<? super T> filter)
Removes all elements from a collection not accepted by the given filter. The original collection is modified. It is required that the collection is modifiable.

Parameters:
collection - Collection to filter
filter - Filter to use for c
Returns:
number of elements removed

isSorted

public static final <T extends Comparable<? super T>> boolean isSorted(List<T> list)
Checks whether a list is sorted.

Parameters:
list - List to check
Returns:
true if list is sorted, otherwise false
See Also:
Comparable, Collections.sort(List)

isSorted

public static final <T> boolean isSorted(List<T> list,
                                         Comparator<? super T> c)
Checks whether a list is sorted.

Parameters:
list - List to check
c - Comparator to use for comparing list elements
Returns:
true if list is sorted, otherwise false
See Also:
Comparator, Collections.sort(List,Comparator)

JAPI
Yet another Java API
API Documentation

© 2005-2006 Christian Hujer. All rights reserved. See copyright