jaggregate
Class Collections

java.lang.Object
  extended by jaggregate.Collections

public class Collections
extends Object

Methods that should be on Collection, but aren't for reasons of type safety or generics restrictions.

Version:
$Id: Collections.java,v 1.9 2008/10/03 19:01:23 pholser Exp $
Author:
Paul Holser

Method Summary
static
<T,E extends T>
T[]
toArray(Collection<E> collection, Class<T> componentType)
          Answers an array with the same elements as the given collection, with the same size as the collection, and a component type of the given class.
static
<E,V> Dictionary<E,V>
toDictionary(Collection<E> elements, UnaryFunctor<? super E,? extends V> mapper)
          Answers a dictionary whose keys are the elements of the given collection and whose values are the results of applying the given mapper to the associated element from the collection.
static
<E,V> IdentityDictionary<E,V>
toIdentityDictionary(Collection<E> elements, UnaryFunctor<? super E,? extends V> mapper)
          Answers a dictionary whose keys are the elements of the given collection and whose values are the results of applying the given mapper to the associated element from the collection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toArray

public static <T,E extends T> T[] toArray(Collection<E> collection,
                                          Class<T> componentType)
Answers an array with the same elements as the given collection, with the same size as the collection, and a component type of the given class.

If collection maintains an ordering for its elements, the order of those elements will be preserved in the result.

Type Parameters:
T - the component type of the array to be created
E - the type of the elements in the collection to convert
Parameters:
collection - the collection to make an array from
componentType - the desired type of the result array's components
Returns:
an array of collection's elements
Throws:
NullPointerException - if either collection or componentType is null
IllegalArgumentException - if componentType is Void.TYPE
See Also:
Collection.toArray(), Creating an array from a generic Java collection

toDictionary

public static <E,V> Dictionary<E,V> toDictionary(Collection<E> elements,
                                                 UnaryFunctor<? super E,? extends V> mapper)
Answers a dictionary whose keys are the elements of the given collection and whose values are the results of applying the given mapper to the associated element from the collection.

elements are traversed in the order specified by Collection.forEachDo(UnaryFunctor).

This method would be on Collection if it weren't for these bugs.

Type Parameters:
E - the type of elements in the given collection
V - the types of the values in the resultant dictionary
Parameters:
elements - a collection
mapper - a function to apply to each element of elements
Returns:
a mapping from elements to mapped values
Throws:
NullPointerException - if either elements or mapper is null

toIdentityDictionary

public static <E,V> IdentityDictionary<E,V> toIdentityDictionary(Collection<E> elements,
                                                                 UnaryFunctor<? super E,? extends V> mapper)
Answers a dictionary whose keys are the elements of the given collection and whose values are the results of applying the given mapper to the associated element from the collection.

elements are traversed in the order specified by Collection.forEachDo(UnaryFunctor).

This method would be on Collection if it weren't for these bugs.

Type Parameters:
E - the type of elements in the given collection
V - the types of the values in the resultant dictionary
Parameters:
elements - a collection
mapper - a function to apply to each element of elements
Returns:
a mapping from elements to mapped values
Throws:
NullPointerException - if either elements or mapper is null


© Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. Licensed under the Academic Free License version 3.0. pholser@alumni.rice.edu