org.jboss.remoting.util
Class CollectionUtil

java.lang.Object
  extended by org.jboss.remoting.util.CollectionUtil

public final class CollectionUtil
extends Object

A set of helpful utility functions for collections.


Method Summary
static
<T> List<T>
arrayList()
          Create an array-backed list.
static
<T> List<T>
arrayList(List<T> orig)
          Create an array-backed list whose contents are a copy of the given list.
static boolean arrayStartsWith(Object[] theArray, Object[] prefix)
          Determine if one array begins with another.
static
<T> BlockingQueue<T>
blockingQueue(int size)
          Create a fixed-capacity blocking queue.
static
<T> Iterable<T>
combine(Iterable<? extends T> first, Iterable<? extends T> second)
          Combine two Iterables into one.
static
<T> Iterable<T>
combine(Iterable<? extends T> first, Iterable<? extends T> second, Iterable<? extends T> third)
          Combine three Iterables into one.
static
<T> Iterable<T>
combine(Iterable<? extends T> first, Iterable<? extends T> second, Iterable<? extends T> third, Iterable<? extends T> fourth)
          Combine four Iterables into one.
static
<T> Iterator<T>
combine(Iterator<? extends T> first, Iterator<? extends T> second)
          Combine two Iterators into one.
static
<T> Iterator<T>
combine(Iterator<? extends T> first, Iterator<? extends T> second, Iterator<? extends T> third)
          Combine three Iterators into one.
static
<T> Iterator<T>
combine(Iterator<? extends T> first, Iterator<? extends T> second, Iterator<? extends T> third, Iterator<? extends T> fourth)
          Combine four Iterators into one.
static
<K,V> ConcurrentMap<K,V>
concurrentMap()
          Create a concurrent map instance.
static
<K,V> ConcurrentMap<K,V>
concurrentWeakMap()
          Create a concurrent map with weak keys.
static
<K,V> ConcurrentMap<K,V>
concurrentWeakValueMap()
          Create a concurrent map with weak values.
static
<T> Iterable<T>
emptyIterable()
          Get the empty iterable.
static
<T> Iterator<T>
emptyIterator()
          Get the empty iterator.
static
<K,V> Map.Entry<K,V>
entry(K key, V value)
          Create an immutable map entry.
static
<K extends Enum<K>,V>
EnumMap<K,V>
enumMap(Class<K> keyType)
          Create an enum map for the given key type.
static
<K,V> Map<K,V>
hashMap()
          Create a hash map.
static
<K,V> Map<K,V>
hashMap(Map.Entry<K,V>... entries)
          Create a prepopulated hash map.
static
<T> Set<T>
hashSet()
          Create a hash set.
static
<T> Iterator<T>
iterator(Enumeration<T> enumeration)
          Create an Iterator view of an Enumeration.
static String join(CharSequence delimiter, Iterable<? extends CharSequence> strings)
          Join a series of character sequences using a delimiter.
static
<T> LinkedList<T>
linkedList()
          Create a linked list.
static
<T> Iterable<T>
loop(Enumeration<T> enumeration)
          Create an Iterable view of an Enumeration.
static
<T> Iterable<T>
loop(Iterator<T> iterator)
          Create an Iterable view of an Iterator.
static
<T> Iterable<T>
protectedIterable(Iterable<T> original)
          Create an Iterable view of another Iterable that exposes no other methods.
static
<T> Iterable<T>
reverse(List<T> list)
          Get an iterable reversed view of a list.
static
<T> ListIterator<T>
reverse(ListIterator<T> original)
          Get a reversed view of a list iterator.
static Iterable<String> split(String delimiter, String subject)
          Create an iterable view of a string split by a given delimiter.
static
<T> List<T>
synchronizedArrayList()
          Create a synchronized array-backed list.
static
<K,V> ConcurrentMap<K,V>
synchronizedHashMap()
          Create a synchronized hash map that obeys the contract for ConcurrentMap.
static
<T> Set<T>
synchronizedHashSet()
          Create a synchronized hash set.
static
<K,V> ConcurrentMap<K,V>
synchronizedMap(Map<K,V> original)
          Create a synchronized map that obeys the contract for ConcurrentMap.
static
<T> BlockingQueue<T>
synchronizedQueue(Queue<T> nested)
          Create a synchronized version of the nested queue that obeys the contract for BlockingQueue.
static
<T> Set<T>
synchronizedSet(Set<T> nested)
          Create a synchronized wrapper for the given set.
static
<K,V> ConcurrentMap<K,V>
synchronizedWeakHashMap()
          Create a synchronized hash map with weak keys, which obeys the ConcurrentMap contract.
static
<T> Set<T>
synchronizedWeakHashSet()
          Create a synchronized weak hash set.
static
<T> T[]
toArray(Iterator<? extends T> iterator, Class<T> type)
          Convert an iterator to an array.
static
<T> T[]
toArrayReversed(Iterator<? extends T> iterator, Class<T> type)
          Convert an iterator to an array in reverse order.
static
<K,V> Map<K,V>
unmodifiableHashMap(Map.Entry<K,V>... entries)
          Create an unmodifiable prepopulated hash map.
static
<T> List<T>
unmodifiableList(T... entries)
          Create an unmodifiable list view of an array.
static
<K,V> Map<K,V>
weakHashMap()
          Create a hash map with weak keys.
static
<T> Set<T>
weakHashSet()
          Create a weak hash set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

enumMap

public static <K extends Enum<K>,V> EnumMap<K,V> enumMap(Class<K> keyType)
Create an enum map for the given key type.

Parameters:
keyType - the key type
Returns:
the new map

concurrentMap

public static <K,V> ConcurrentMap<K,V> concurrentMap()
Create a concurrent map instance.

Returns:
a new concurrent map

concurrentWeakMap

public static <K,V> ConcurrentMap<K,V> concurrentWeakMap()
Create a concurrent map with weak keys.

Returns:
a new concurrent map with weak keys

concurrentWeakValueMap

public static <K,V> ConcurrentMap<K,V> concurrentWeakValueMap()
Create a concurrent map with weak values.

Returns:
a new concurrent map with weak values

synchronizedMap

public static <K,V> ConcurrentMap<K,V> synchronizedMap(Map<K,V> original)
Create a synchronized map that obeys the contract for ConcurrentMap.

Parameters:
original - the map to be wrapped
Returns:
a synchronized map

synchronizedHashMap

public static <K,V> ConcurrentMap<K,V> synchronizedHashMap()
Create a synchronized hash map that obeys the contract for ConcurrentMap.

Returns:
a synchronized hash map

arrayList

public static <T> List<T> arrayList()
Create an array-backed list.

Returns:
an array-backed list

linkedList

public static <T> LinkedList<T> linkedList()
Create a linked list.

Returns:
a linked list

synchronizedArrayList

public static <T> List<T> synchronizedArrayList()
Create a synchronized array-backed list.

Returns:
a synchronized array-backed list

arrayList

public static <T> List<T> arrayList(List<T> orig)
Create an array-backed list whose contents are a copy of the given list.

Parameters:
orig - the original list
Returns:
an array backed list

synchronizedSet

public static <T> Set<T> synchronizedSet(Set<T> nested)
Create a synchronized wrapper for the given set.

Parameters:
nested - the nested set
Returns:
a synchronized version of the nested set

synchronizedHashSet

public static <T> Set<T> synchronizedHashSet()
Create a synchronized hash set.

Returns:
a synchronized hash set

synchronizedWeakHashSet

public static <T> Set<T> synchronizedWeakHashSet()
Create a synchronized weak hash set.

Returns:
a synchronized weak hash set

synchronizedQueue

public static <T> BlockingQueue<T> synchronizedQueue(Queue<T> nested)
Create a synchronized version of the nested queue that obeys the contract for BlockingQueue.

Parameters:
nested - the nested queue
Returns:
the blocking queue

weakHashSet

public static <T> Set<T> weakHashSet()
Create a weak hash set.

Returns:
a weak hash set

blockingQueue

public static <T> BlockingQueue<T> blockingQueue(int size)
Create a fixed-capacity blocking queue.

Parameters:
size - the fixed size
Returns:
a fixed-capacity blocking queue

hashSet

public static <T> Set<T> hashSet()
Create a hash set.

Returns:
a hash set

weakHashMap

public static <K,V> Map<K,V> weakHashMap()
Create a hash map with weak keys. See WeakHashMap.

Returns:
a hash map with weak keys

synchronizedWeakHashMap

public static <K,V> ConcurrentMap<K,V> synchronizedWeakHashMap()
Create a synchronized hash map with weak keys, which obeys the ConcurrentMap contract.

Returns:
a synchronized weak hash map

unmodifiableList

public static <T> List<T> unmodifiableList(T... entries)
Create an unmodifiable list view of an array.

Parameters:
entries - the array
Returns:
an unmodifiable list

hashMap

public static <K,V> Map<K,V> hashMap()
Create a hash map.

Returns:
a hash map

entry

public static <K,V> Map.Entry<K,V> entry(K key,
                                         V value)
Create an immutable map entry.

Parameters:
key - the key
value - the value
Returns:
the entry

hashMap

public static <K,V> Map<K,V> hashMap(Map.Entry<K,V>... entries)
Create a prepopulated hash map. The map will be sized for the number of elements given.

Parameters:
entries - the map entries
Returns:
the prepopulated hash map

unmodifiableHashMap

public static <K,V> Map<K,V> unmodifiableHashMap(Map.Entry<K,V>... entries)
Create an unmodifiable prepopulated hash map.

Parameters:
entries - the map entries
Returns:
the unmodifiable prepopulated hash map

protectedIterable

public static <T> Iterable<T> protectedIterable(Iterable<T> original)
Create an Iterable view of another Iterable that exposes no other methods.

Parameters:
original - the wrapped instance
Returns:
a new Iterable

loop

public static <T> Iterable<T> loop(Enumeration<T> enumeration)
Create an Iterable view of an Enumeration. The view can be used only once.

Parameters:
enumeration - the enumeration
Returns:
the Iterable view

loop

public static <T> Iterable<T> loop(Iterator<T> iterator)
Create an Iterable view of an Iterator. The view can be used only once.

Parameters:
iterator - the iterator
Returns:
the Iterable view

iterator

public static <T> Iterator<T> iterator(Enumeration<T> enumeration)
Create an Iterator view of an Enumeration.

Parameters:
enumeration - the enumeration
Returns:
the Iterator view

split

public static Iterable<String> split(String delimiter,
                                     String subject)
Create an iterable view of a string split by a given delimiter.

Parameters:
delimiter - the delimiter
subject - the original string
Returns:
the iterable split view

join

public static String join(CharSequence delimiter,
                          Iterable<? extends CharSequence> strings)
Join a series of character sequences using a delimiter.

Parameters:
delimiter - the delimiter
strings - the strings
Returns:
a joined string

toArray

public static <T> T[] toArray(Iterator<? extends T> iterator,
                              Class<T> type)
Convert an iterator to an array. The iterator should be relatively short to avoid blowing out the stack.

Parameters:
iterator - the iterator
type - the array element type
Returns:
the array

toArrayReversed

public static <T> T[] toArrayReversed(Iterator<? extends T> iterator,
                                      Class<T> type)
Convert an iterator to an array in reverse order. The iterator should be relatively short to avoid blowing out the stack.

Parameters:
iterator - the iterator
type - the array element type
Returns:
the array

arrayStartsWith

public static boolean arrayStartsWith(Object[] theArray,
                                      Object[] prefix)
Determine if one array begins with another. If prefix is empty (has a length of zero), then this method always returns true. If prefix is longer than theArray, this method returns false. Otherwise true is returned if the first N elements of theArray are equal to the corresponding element in prefix, where N is equal to the length of prefix. "Equal" in this context means that either both elements being compared are null, or they are equal by way of Object.equals().

Parameters:
theArray - the array to test
prefix - the prefix to test against
Returns:
true if theArray starts with prefix

emptyIterable

public static <T> Iterable<T> emptyIterable()
Get the empty iterable.

Returns:
the empty iterable

emptyIterator

public static <T> Iterator<T> emptyIterator()
Get the empty iterator.

Returns:
the empty iterator

reverse

public static <T> ListIterator<T> reverse(ListIterator<T> original)
Get a reversed view of a list iterator.

Parameters:
original - the original iterator
Returns:
the reversed view

reverse

public static <T> Iterable<T> reverse(List<T> list)
Get an iterable reversed view of a list.

Parameters:
list - the list
Returns:
the reversed view

combine

public static <T> Iterator<T> combine(Iterator<? extends T> first,
                                      Iterator<? extends T> second)
Combine two Iterators into one.

Parameters:
first - the first Iterator
second - the second Iterator
Returns:
a combined Iterator

combine

public static <T> Iterator<T> combine(Iterator<? extends T> first,
                                      Iterator<? extends T> second,
                                      Iterator<? extends T> third)
Combine three Iterators into one.

Parameters:
first - the first Iterator
second - the second Iterator
third - the third Iterator
Returns:
a new combined Iterator

combine

public static <T> Iterator<T> combine(Iterator<? extends T> first,
                                      Iterator<? extends T> second,
                                      Iterator<? extends T> third,
                                      Iterator<? extends T> fourth)
Combine four Iterators into one.

Parameters:
first - the first Iterator
second - the second Iterator
third - the third Iterator
fourth - the fourth Iterator
Returns:
a new combined Iterator

combine

public static <T> Iterable<T> combine(Iterable<? extends T> first,
                                      Iterable<? extends T> second)
Combine two Iterables into one.

Parameters:
first - the first Iterable
second - the second Iterable
Returns:
a new combined Iterable

combine

public static <T> Iterable<T> combine(Iterable<? extends T> first,
                                      Iterable<? extends T> second,
                                      Iterable<? extends T> third)
Combine three Iterables into one.

Parameters:
first - the first Iterable
second - the second Iterable
third - the third Iterable
Returns:
a new combined Iterable

combine

public static <T> Iterable<T> combine(Iterable<? extends T> first,
                                      Iterable<? extends T> second,
                                      Iterable<? extends T> third,
                                      Iterable<? extends T> fourth)
Combine four Iterables into one.

Parameters:
first - the first Iterable
second - the second Iterable
third - the third Iterable
fourth - the fourth Iterable
Returns:
a new combined Iterable


Copyright © 2008 JBoss, a division of Red Hat, Inc.