org.jopendocument.util
Class CollectionUtils

java.lang.Object
  extended by org.jopendocument.util.CollectionUtils

public class CollectionUtils
extends Object

Une classe regroupant des méthodes utilitaires pour les collections.

Author:
ILM Informatique 30 sept. 2004

Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<K,V> Map<K,V>
addIfNotPresent(Map<K,V> map, Map<? extends K,? extends V> toAdd)
          Add entries from toAdd into map only if the key is not already present.
static
<T,C extends Collection<? super T>>
C
addTo(Iterator<T> iter, C c)
           
static List<int[]> aggregate(Collection<? extends Number> ints)
          Aggregate a list of ints into a list of intervals.
static
<U,T extends U>
List<T>
castToList(U[] array, Class<T> clazz)
          Convert an array to a list of a different type.
static
<T> Set<T>
contains(Set<T> col1, Set<T> col2)
          Test whether col2 is contained in col1.
static
<C extends Collection<?>>
boolean
containsAny(C coll1, C coll2)
           
static
<T> IdentitySet<T>
createIdentitySet(T... items)
           
static
<K,V> Map<K,V>
createMap(Collection<? extends K> keys)
          Creates a map with null values.
static
<K,V> Map<K,V>
createMap(K key, V val, K key2, V val2)
           
static
<K,V> Map<K,V>
createMap(K key, V val, K key2, V val2, K key3, V val3)
           
static
<T> Set<T>
createSet(T... items)
           
static void delete(List<?> l, int from)
          Deletes the tail of a list.
static void delete(List<?> l, int from, int to)
          Deletes a slice of a list.
static
<T> IdentitySet<T>
emptyIdentitySet()
           
static
<T> Iterator<T>
emptyIterator()
           
static
<A,B> int
equals(List<A> a, List<B> b, boolean fromEnd, ITransformer<A,B> transf)
          The number of equals item between a and b, starting from the choosen end.
static
<T> int
equalsFromEnd(List<T> a, List<T> b)
          The number of equals item between a and b, starting from the end.
static
<T> int
equalsFromStart(List<T> a, List<T> b)
           
static
<T> boolean
exists(Collection<T> collection, IPredicate<? super T> predicate)
           
static
<K,V,M extends Map<K,V>>
M
fillMap(M m, Collection<? extends K> keys)
          Fills a map with null values.
static
<T> void
filter(Collection<T> collection, IPredicate<? super T> predicate)
           
static
<T,U,C extends Collection<? super U>>
C
filterAndTransform(Collection<T> c, IPredicate<? super T> filterBefore, ITransformer<? super T,U> transf, C res)
           
static
<T,U,C extends Collection<? super U>>
C
filterTransformAndFilter(Collection<T> c, IPredicate<? super T> filterBefore, ITransformer<? super T,U> transf, IPredicate<? super U> filterAfter, C res)
           
static List flatten(Map hierarchy)
          Permet d'aplatir une hiérarchie.
static List flatten(Map hierarchy, org.apache.commons.collections.Transformer itemTransf)
          Permet d'aplatir une hiérarchie.
static
<T> T
getFirst(Collection<T> l)
           
static
<T> T
getFirst(List<T> l)
          Return the first item of l if it isn't empty, otherwise null.
static
<E> List<Integer>
getIndexesChanged(List<E> oldList, List<E> newList)
          Compute the index that have changed (added or removed) between 2 lists.
static
<T> T
getLast(List<T> l)
          Return the last item of l if it isn't empty, otherwise null.
static
<T> ListIterator<T>
getListIterator(List<T> l, boolean reversed)
           
static
<T> T
getNoExn(List<T> l, int index)
          Return the item no index of l if it exists, otherwise null.
static
<T> T
getSole(Collection<T> l)
           
static
<T> T
getSole(List<T> l)
          Return the first item of l if it's the only one, otherwise null.
static int getValidIndex(List<?> l, int i)
          Return an index between 0 and l.size() inclusive.
static int getValidIndex(List<?> l, int i, boolean strict)
           
static
<T> Set<T>
inter(List<Set<T>> sets)
           
static
<T> Set<T>
inter(Set<T>... sets)
           
static
<T> Set<T>
inter(Set<T> a, Set<T> b)
          Compute the intersection of a and b.
static
<T> Collection<T>
intersection(Collection<T> a, Collection<T> b)
           
static
<T> Set<? extends T>
interSubtype(Set<? extends T> a, Set<? extends T> b)
           
static
<T> void
iterate(Collection<T> c, IClosure<T> cl)
           
static
<T> void
iterate(Collection<T> c, IPredicate<? super T> filterBefore, IClosure<T> cl)
           
static
<E> String
join(Collection<E> c, String sep, ITransformer<? super E,?> tf)
          Concatene une collection.
static
<T> String
join(Collection<T> c, String sep)
          Concatene une collection en appelant simplement toString() sur chaque élément.
static
<K,V> Tuple2<List<K>,List<V>>
mapToLists(Map<K,V> map)
          Convertit une map en 2 listes, une pour les clefs, une pour les valeurs.
static SortedMap organize(Collection col, List<? extends org.apache.commons.collections.Transformer> propExtractors, List<? extends Comparator> propComp, Comparator itemOrdering)
          Permet d'organiser une collection en une hiérarchie à l'aide de Map.
static
<T> ListIterator<T>
reverseListIterator(ListIterator<T> listIter)
           
static
<T> Collection<T>
select(Collection<T> a, IPredicate<? super T> pred)
           
static
<T,C extends Collection<? super T>>
C
select(Collection<T> a, IPredicate<? super T> pred, C b)
           
static List<String> split(String s)
           
static List<String> split(String s, Pattern pattern)
          Split a string into a list based on a pattern.
static List<String> split(String s, String sep)
           
static
<T> Collection<T>
substract(Collection<T> a, Collection<? extends T> b)
           
static
<T> Collection<T>
subtract(Collection<T> a, Collection<? extends T> b)
           
static
<T> ArrayList<T>
toArrayList(Iterator<T> iter, int estimatedSize)
           
static
<T> Set<T>
toIdentitySet(Collection<T> items)
          Return an IdentitySet consisting of items.
static
<T> LinkedList<T>
toLinkedList(Iterator<T> iter)
           
static
<T,U,C extends Collection<? super U>>
C
transform(Collection<T> c, ITransformer<? super T,U> transf, C res)
           
static
<T,U,C extends Collection<? super U>>
C
transformAndFilter(Collection<T> c, ITransformer<? super T,U> transf, IPredicate<? super U> filterAfter, C res)
           
static
<T> Set<T>
union(Set<? extends T> a, Set<? extends T> b)
          Returns a Set containing the union of the given Sets.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

join

public static final <E> String join(Collection<E> c,
                                    String sep,
                                    ITransformer<? super E,?> tf)
Concatene une collection. Cette méthode va appliquer un transformation sur chaque élément avant d'appeler toString(). join([-1, 3, 0], " ,", doubleTransformer) == "-2, 6, 0"

Type Parameters:
E - type of items
Parameters:
c - la collection a concaténer.
sep - le séparateur entre chaque élément.
tf - la transformation à appliquer à chaque élément.
Returns:
la chaine composée de chacun des éléments séparés par sep.

join

public static <T> String join(Collection<T> c,
                              String sep)
Concatene une collection en appelant simplement toString() sur chaque élément.

Type Parameters:
T - type of collection
Parameters:
c - la collection a concaténer.
sep - le séparateur entre chaque élément.
Returns:
la chaine composée de chacun des éléments séparés par sep.
See Also:
join(Collection, String, ITransformer)

transform

public static <T,U,C extends Collection<? super U>> C transform(Collection<T> c,
                                                                ITransformer<? super T,U> transf,
                                                                C res)

transformAndFilter

public static <T,U,C extends Collection<? super U>> C transformAndFilter(Collection<T> c,
                                                                         ITransformer<? super T,U> transf,
                                                                         IPredicate<? super U> filterAfter,
                                                                         C res)

filterAndTransform

public static <T,U,C extends Collection<? super U>> C filterAndTransform(Collection<T> c,
                                                                         IPredicate<? super T> filterBefore,
                                                                         ITransformer<? super T,U> transf,
                                                                         C res)

filterTransformAndFilter

public static <T,U,C extends Collection<? super U>> C filterTransformAndFilter(Collection<T> c,
                                                                               IPredicate<? super T> filterBefore,
                                                                               ITransformer<? super T,U> transf,
                                                                               IPredicate<? super U> filterAfter,
                                                                               C res)

iterate

public static <T> void iterate(Collection<T> c,
                               IClosure<T> cl)

iterate

public static <T> void iterate(Collection<T> c,
                               IPredicate<? super T> filterBefore,
                               IClosure<T> cl)

split

public static List<String> split(String s)

split

public static List<String> split(String s,
                                 String sep)

split

public static List<String> split(String s,
                                 Pattern pattern)
Split a string into a list based on a pattern.

Parameters:
s - the string to split.
pattern - the pattern where to cut the string.
Returns:
the splitted string, empty list if s is "".

getValidIndex

public static int getValidIndex(List<?> l,
                                int i)
Return an index between 0 and l.size() inclusive. If i is negative, it is added to l.size() (but bounded to 0), ie for a list of 3 items, -1 is the index of the last item ; -3 and -4 are both the first. If i is greater than l.size() then l.size() is returned.
    a  b  c  a  b  c
   -3 -2 -1  0  1  2  3
 

Parameters:
l - the list, eg a list of 3 items.
i - the virtual index, eg -1.
Returns:
the real index, eg 2.

getValidIndex

public static int getValidIndex(List<?> l,
                                int i,
                                boolean strict)

delete

public static void delete(List<?> l,
                          int from,
                          int to)
Deletes a slice of a list. Pass indexes to getValidIndex(List, int) to allow delete(l, 0, -1) to clear l or delete(l, -2, -2) to remove the penultimate item.

Parameters:
l - the list to delete from.
from - the first index to be removed (inclusive).
to - the last index to be removed (inclusive).

delete

public static void delete(List<?> l,
                          int from)
Deletes the tail of a list. The resulting list will have a size of from.

Parameters:
l - the list to delete from.
from - the first index to be removed (inclusive).

filter

public static <T> void filter(Collection<T> collection,
                              IPredicate<? super T> predicate)

exists

public static <T> boolean exists(Collection<T> collection,
                                 IPredicate<? super T> predicate)

organize

public static final SortedMap organize(Collection col,
                                       List<? extends org.apache.commons.collections.Transformer> propExtractors,
                                       List<? extends Comparator> propComp,
                                       Comparator itemOrdering)
Permet d'organiser une collection en une hiérarchie à l'aide de Map. Avec Col = [ Obs1(bat=BAT A, local=A1, num=1), Obs2(bat=BAT B, local=B1, num=2), Obs3(bat=BAT B, local=B2, num=3), Obs4(bat=BAT B, local=B2, num=4) ] ainsi que deux extracteurs pour trouver le batiment et le local, et enfin itemOrdering suivant le numero, on a { BAT A => {A1 => {Obs1}}, {BAT B => {B1 => {Obs2}, B2 => {Obs3, Obs4}}}}.

Parameters:
col - la collection à organiser.
propExtractors - les extracteurs de propriétes.
propComp - les Comparator pour les propriétés renvoyées par les extracteurs, peut être null si les propriétés sont des Comparable.
itemOrdering - comment ordonner les éléments dans la dernière tranche, peut être null si les éléments sont des Comparable.
Returns:
une hiérarchie de SortedMap et en dernier un SortedSet.

flatten

public static final List flatten(Map hierarchy,
                                 org.apache.commons.collections.Transformer itemTransf)
Permet d'aplatir une hiérarchie. Exemple :
   A-
      A1
      A2
   B-
      B1
         B11
         B12
 
devient [A, A1, A2, B, B1, B11, B12].

Parameters:
hierarchy - la hiérarchie à aplatir.
itemTransf - la transformation à faire sur les feuilles.
Returns:
la liste correspondante.

flatten

public static final List flatten(Map hierarchy)
Permet d'aplatir une hiérarchie.

Parameters:
hierarchy - la hiérarchie à aplatir.
Returns:
la liste correspondante.

mapToLists

public static <K,V> Tuple2<List<K>,List<V>> mapToLists(Map<K,V> map)
Convertit une map en 2 listes, une pour les clefs, une pour les valeurs.

Type Parameters:
K - type of key
V - type of value
Parameters:
map - la Map à convertir.
Returns:
un tuple de 2 List, en 0 les clefs, en 1 les valeurs.

addIfNotPresent

public static <K,V> Map<K,V> addIfNotPresent(Map<K,V> map,
                                             Map<? extends K,? extends V> toAdd)
Add entries from toAdd into map only if the key is not already present.

Type Parameters:
K - type of keys.
V - type of values.
Parameters:
map - the map to fill.
toAdd - the entries to add.
Returns:
map.

getIndexesChanged

public static <E> List<Integer> getIndexesChanged(List<E> oldList,
                                                  List<E> newList)
Compute the index that have changed (added or removed) between 2 lists. One of the lists MUST be a sublist of the other, ie the to go from one to the other we just add or remove items but we don't do both.

Type Parameters:
E - type of item
Parameters:
oldList - the first list.
newList - the second list.
Returns:
a list of Integer.
Throws:
IllegalStateException - if one list is not a sublist of the other.

aggregate

public static List<int[]> aggregate(Collection<? extends Number> ints)
Aggregate a list of ints into a list of intervals. Eg aggregate([-1,0,1,2,5]) returns [[-1,2], [5,5]].

Parameters:
ints - a list of Integer strictly increasing.
Returns:
a list of int[2].

contains

public static <T> Set<T> contains(Set<T> col1,
                                  Set<T> col2)
Test whether col2 is contained in col1.

Type Parameters:
T - type of collection
Parameters:
col1 - the first collection
col2 - the second collection
Returns:
null if col1 contains all of col2, else return the extra items that col2 have.

containsAny

public static <C extends Collection<?>> boolean containsAny(C coll1,
                                                            C coll2)

castToList

public static <U,T extends U> List<T> castToList(U[] array,
                                                 Class<T> clazz)
                                    throws ClassCastException
Convert an array to a list of a different type.

Type Parameters:
U - type of array
T - type of list
Parameters:
array - the array to convert, eg new Object[]{"a", "b"}.
clazz - the class of the list items, eg String.class.
Returns:
all items of array into a list, eg ["a", "b"].
Throws:
ClassCastException - if some item of array is not a T.

equalsFromEnd

public static <T> int equalsFromEnd(List<T> a,
                                    List<T> b)
The number of equals item between a and b, starting from the end.

Type Parameters:
T - type of items.
Parameters:
a - the first list, eg [a, b, c].
b - the second list, eg [a, null, z, c].
Returns:
the number of common items, eg 1.

equalsFromStart

public static <T> int equalsFromStart(List<T> a,
                                      List<T> b)

equals

public static final <A,B> int equals(List<A> a,
                                     List<B> b,
                                     boolean fromEnd,
                                     ITransformer<A,B> transf)
The number of equals item between a and b, starting from the choosen end.

Type Parameters:
A - type of the first list.
B - type of the second list.
Parameters:
a - the first list, eg [a, b, c].
b - the second list, eg [a, null, z, c].
fromEnd - whether search from the start or the end, true.
transf - how items of a should be transformed before being compared, can be null.
Returns:
the number of common items, eg 1.

select

public static <T> Collection<T> select(Collection<T> a,
                                       IPredicate<? super T> pred)

select

public static <T,C extends Collection<? super T>> C select(Collection<T> a,
                                                           IPredicate<? super T> pred,
                                                           C b)

intersection

public static <T> Collection<T> intersection(Collection<T> a,
                                             Collection<T> b)

inter

public static <T> Set<T> inter(Set<T> a,
                               Set<T> b)
Compute the intersection of a and b. nulls are ignored : x ∩ null = x.

Type Parameters:
T - type of collection.
Parameters:
a - the first set, can be null.
b - the second set, can be null.
Returns:
the intersection.

interSubtype

public static <T> Set<? extends T> interSubtype(Set<? extends T> a,
                                                Set<? extends T> b)

inter

public static <T> Set<T> inter(Set<T>... sets)

inter

public static <T> Set<T> inter(List<Set<T>> sets)

union

public static <T> Set<T> union(Set<? extends T> a,
                               Set<? extends T> b)
Returns a Set containing the union of the given Sets.

Type Parameters:
T - type of items.
Parameters:
a - the first set, must not be null
b - the second set, must not be null
Returns:
the union of the two.

subtract

public static <T> Collection<T> subtract(Collection<T> a,
                                         Collection<? extends T> b)

substract

public static <T> Collection<T> substract(Collection<T> a,
                                          Collection<? extends T> b)

getSole

public static <T> T getSole(List<T> l)
Return the first item of l if it's the only one, otherwise null.

Type Parameters:
T - type of list.
Parameters:
l - the list.
Returns:
the first item of l or null.

getSole

public static <T> T getSole(Collection<T> l)

getFirst

public static <T> T getFirst(Collection<T> l)

getFirst

public static <T> T getFirst(List<T> l)
Return the first item of l if it isn't empty, otherwise null.

Type Parameters:
T - type of list.
Parameters:
l - the list.
Returns:
the first item of l or null.

getLast

public static <T> T getLast(List<T> l)
Return the last item of l if it isn't empty, otherwise null.

Type Parameters:
T - type of list.
Parameters:
l - the list.
Returns:
the last item of l or null.

getNoExn

public static <T> T getNoExn(List<T> l,
                             int index)
Return the item no index of l if it exists, otherwise null.

Type Parameters:
T - type of list.
Parameters:
l - the list.
index - the wanted index.
Returns:
the corresponding item of l or null.

emptyIterator

public static <T> Iterator<T> emptyIterator()

toLinkedList

public static <T> LinkedList<T> toLinkedList(Iterator<T> iter)

toArrayList

public static <T> ArrayList<T> toArrayList(Iterator<T> iter,
                                           int estimatedSize)

addTo

public static <T,C extends Collection<? super T>> C addTo(Iterator<T> iter,
                                                          C c)

getListIterator

public static <T> ListIterator<T> getListIterator(List<T> l,
                                                  boolean reversed)

reverseListIterator

public static <T> ListIterator<T> reverseListIterator(ListIterator<T> listIter)

createSet

public static <T> Set<T> createSet(T... items)

createIdentitySet

public static <T> IdentitySet<T> createIdentitySet(T... items)

toIdentitySet

public static <T> Set<T> toIdentitySet(Collection<T> items)
Return an IdentitySet consisting of items.

Parameters:
items - the collection whose elements are to be in the result.
Returns:
a set, possibly items if it's already an identity set.

emptyIdentitySet

public static <T> IdentitySet<T> emptyIdentitySet()

createMap

public static <K,V> Map<K,V> createMap(K key,
                                       V val,
                                       K key2,
                                       V val2)

createMap

public static <K,V> Map<K,V> createMap(K key,
                                       V val,
                                       K key2,
                                       V val2,
                                       K key3,
                                       V val3)

createMap

public static <K,V> Map<K,V> createMap(Collection<? extends K> keys)
Creates a map with null values.

Type Parameters:
K - type of key.
V - type of value.
Parameters:
keys - the keys of the map.
Returns:
a new map, if keys is a List it will be ordered.

fillMap

public static <K,V,M extends Map<K,V>> M fillMap(M m,
                                                 Collection<? extends K> keys)
Fills a map with null values.

Type Parameters:
K - type of key.
V - type of value.
M - type of map.
Parameters:
m - the map to fill.
keys - the keys to add.
Returns:
the passed map.


Copyright © 2010 jOpenDocument All Rights Reserved.