| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Collections |
|
| 0.0;0 | ||||
| Collections$1 |
|
| 0.0;0 | ||||
| Collections$2 |
|
| 0.0;0 | ||||
| Collections$3 |
|
| 0.0;0 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package jaggregate; | |
| 7 | ||
| 8 | import java.lang.reflect.Array; | |
| 9 | ||
| 10 | import static jaggregate.Dictionary.*; | |
| 11 | import static jaggregate.IdentityDictionary.*; | |
| 12 | import static jaggregate.internal.ArgumentChecks.*; | |
| 13 | import static jaggregate.internal.Casting.*; | |
| 14 | ||
| 15 | /** | |
| 16 | * Methods that should be on {@link Collection}, but aren't for reasons of type safety | |
| 17 | * or generics restrictions. | |
| 18 | * | |
| 19 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 20 | * @version $Id: Collections.java,v 1.9 2008/10/03 19:01:23 pholser Exp $ | |
| 21 | */ | |
| 22 | public class Collections { | |
| 23 | /** | |
| 24 | * Discourages instantiation. | |
| 25 | * | |
| 26 | * @throws UnsupportedOperationException always | |
| 27 | */ | |
| 28 | 1 | protected Collections() { |
| 29 | 1 | throw new UnsupportedOperationException(); |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * Answers an array with the same elements as the given collection, with the same | |
| 34 | * {@linkplain Collection#size() size} as the collection, and a component type of | |
| 35 | * the given class. | |
| 36 | * <p/> | |
| 37 | * If {@code collection} maintains an ordering for its elements, the order of | |
| 38 | * those elements will be preserved in the result. | |
| 39 | * | |
| 40 | * @param <T> the component type of the array to be created | |
| 41 | * @param <E> the type of the elements in the collection to convert | |
| 42 | * @param collection the collection to make an array from | |
| 43 | * @param componentType the desired type of the result array's components | |
| 44 | * @return an array of {@code collection}'s elements | |
| 45 | * @throws NullPointerException if either {@code collection} or {@code componentType} | |
| 46 | * is {@code null} | |
| 47 | * @throws IllegalArgumentException if {@code componentType} is {@link Void#TYPE} | |
| 48 | * @see Collection#toArray() | |
| 49 | * @see <a href="http://cleveralias.blogs.com/thought_spearmints/2005/02/creating_an_arr.html">Creating | |
| 50 | * an array from a generic Java collection</a> | |
| 51 | */ | |
| 52 | public static <T, E extends T> T[] toArray( Collection<E> collection, | |
| 53 | Class<T> componentType ) { | |
| 54 | ||
| 55 | 70 | if ( componentType.isPrimitive() ) |
| 56 | 2 | throw new IllegalArgumentException( "primitive component type" ); |
| 57 | ||
| 58 | 67 | final T[] collectionAsArray = |
| 59 | cast( Array.newInstance( componentType, collection.size() ) ); | |
| 60 | ||
| 61 | 66 | collection.inject( 0, new BinaryFunctor<Integer, E, Integer>() { |
| 62 | 160 | public Integer evaluate( Integer first, E second ) { |
| 63 | 94 | collectionAsArray[first] = second; |
| 64 | 94 | return first + 1; |
| 65 | } | |
| 66 | } ); | |
| 67 | ||
| 68 | 66 | return collectionAsArray; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Answers a dictionary whose keys are the elements of the given collection and whose | |
| 73 | * values are the results of applying the given mapper to the associated element | |
| 74 | * from the collection. | |
| 75 | * <p/> | |
| 76 | * {@code elements} are traversed in the order specified by {@link | |
| 77 | * Collection#forEachDo(UnaryFunctor)}. | |
| 78 | * <p/> | |
| 79 | * This method would be on {@link Collection} if it weren't | |
| 80 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404691">for</a> | |
| 81 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6218229">these</a> | |
| 82 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6292765">bugs</a>. | |
| 83 | * | |
| 84 | * @param <E> the type of elements in the given collection | |
| 85 | * @param <V> the types of the values in the resultant dictionary | |
| 86 | * @param elements a collection | |
| 87 | * @param mapper a function to apply to each element of {@code elements} | |
| 88 | * @return a mapping from {@code elements} to mapped values | |
| 89 | * @throws NullPointerException if either {@code elements} or {@code mapper} is | |
| 90 | * {@code null} | |
| 91 | */ | |
| 92 | public static <E, V> Dictionary<E, V> toDictionary( Collection<E> elements, | |
| 93 | final UnaryFunctor<? super E, ? extends V> mapper ) { | |
| 94 | ||
| 95 | 4 | ensureNotNull( elements, COLLECTION ); |
| 96 | 3 | ensureNotNull( mapper, MAPPER ); |
| 97 | ||
| 98 | 2 | final Dictionary<E, V> mappings = emptyDictionary(); |
| 99 | ||
| 100 | 2 | elements.forEachDo( new UnaryFunctor<E, Void>() { |
| 101 | 4 | public Void evaluate( E argument ) { |
| 102 | 2 | mappings.putAt( argument, mapper.evaluate( argument ) ); |
| 103 | 2 | return null; |
| 104 | } | |
| 105 | } ); | |
| 106 | ||
| 107 | 2 | return mappings; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * Answers a dictionary whose keys are the elements of the given collection and whose | |
| 112 | * values are the results of applying the given mapper to the associated element | |
| 113 | * from the collection. | |
| 114 | * <p/> | |
| 115 | * {@code elements} are traversed in the order specified by {@link | |
| 116 | * Collection#forEachDo(UnaryFunctor)}. | |
| 117 | * <p/> | |
| 118 | * This method would be on {@link Collection} if it weren't | |
| 119 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404691">for</a> | |
| 120 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6218229">these</a> | |
| 121 | * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6292765">bugs</a>. | |
| 122 | * | |
| 123 | * @param <E> the type of elements in the given collection | |
| 124 | * @param <V> the types of the values in the resultant dictionary | |
| 125 | * @param elements a collection | |
| 126 | * @param mapper a function to apply to each element of {@code elements} | |
| 127 | * @return a mapping from {@code elements} to mapped values | |
| 128 | * @throws NullPointerException if either {@code elements} or {@code mapper} is | |
| 129 | * {@code null} | |
| 130 | */ | |
| 131 | public static <E, V> IdentityDictionary<E, V> toIdentityDictionary( | |
| 132 | Collection<E> elements, final UnaryFunctor<? super E, ? extends V> mapper ) { | |
| 133 | ||
| 134 | 4 | ensureNotNull( elements, COLLECTION ); |
| 135 | 3 | ensureNotNull( mapper, MAPPER ); |
| 136 | ||
| 137 | 2 | final IdentityDictionary<E, V> mappings = emptyIdentityDictionary(); |
| 138 | ||
| 139 | 2 | elements.forEachDo( new UnaryFunctor<E, Void>() { |
| 140 | 4 | public Void evaluate( E argument ) { |
| 141 | 2 | mappings.putAt( argument, mapper.evaluate( argument ) ); |
| 142 | 2 | return null; |
| 143 | } | |
| 144 | } ); | |
| 145 | ||
| 146 | 2 | return mappings; |
| 147 | } | |
| 148 | } |