| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Collection |
|
| 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.util.Comparator; | |
| 9 | ||
| 10 | /** | |
| 11 | * Provides protocol for manipulating and operating on a collection of objects, called | |
| 12 | * <dfn>elements</dfn>, either individually or as a whole. A collection can be fixed or | |
| 13 | * variable-sized, ordered or unordered, and its elements may or may not be accessible | |
| 14 | * by external keys. | |
| 15 | * <p/> | |
| 16 | * Some implementations of collections may choose to use the {@linkplain Object#hashCode | |
| 17 | * hash} values of either the elements of the collection or the keys by which those | |
| 18 | * elements are accessed (if there are any). If the hash values of such objects are | |
| 19 | * modified, the behavior of any message sent to such a collection is undefined until | |
| 20 | * the collection is {@linkplain #rehash() rehashed} in order to restore its | |
| 21 | * consistency. | |
| 22 | * | |
| 23 | * @param <E> a restriction on the types of elements that may be included in the | |
| 24 | * collection | |
| 25 | * | |
| 26 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 27 | * @version $Id: Collection.java,v 1.7 2008/05/12 23:49:07 pholser Exp $ | |
| 28 | */ | |
| 29 | public interface Collection<E> { | |
| 30 | /** | |
| 31 | * Answers {@code true} if the given discriminator answers {@code true} for every | |
| 32 | * element of this collection, or if this collection is {@linkplain #isEmpty() | |
| 33 | * empty}; otherwise answers {@code false}. | |
| 34 | * <p/> | |
| 35 | * It is unspecified whether {@code discriminator} will be evaluated with every | |
| 36 | * element of this collection. | |
| 37 | * | |
| 38 | * @param discriminator the discriminator to evaluate | |
| 39 | * @return whether {@code discriminator} is {@code true} for every element | |
| 40 | * @throws NullPointerException if {@code discriminator} is {@code null} | |
| 41 | */ | |
| 42 | boolean allSatisfy( UnaryCondition<? super E> discriminator ); | |
| 43 | ||
| 44 | /** | |
| 45 | * Answers {@code true} if the given discriminator answers {@code true} for any | |
| 46 | * element of this collection; answers {@code false} otherwise, or if this | |
| 47 | * collection is {@linkplain #isEmpty() empty}. | |
| 48 | * <p/> | |
| 49 | * It is unspecified whether {@code discriminator} will be evaluated with every | |
| 50 | * element of this collection. | |
| 51 | * | |
| 52 | * @param discriminator the discriminator to evaluate | |
| 53 | * @return whether {@code discriminator} is {@code true} for any element | |
| 54 | * @throws NullPointerException if the {@code discriminator} is {@code null} | |
| 55 | */ | |
| 56 | boolean anySatisfy( UnaryCondition<? super E> discriminator ); | |
| 57 | ||
| 58 | /** | |
| 59 | * Answers an array with the same elements as this collection, with the same | |
| 60 | * {@linkplain #size() size} as this collection, and a component class of | |
| 61 | * {@link Object}. This means that objects of any class may be inserted into | |
| 62 | * the result. | |
| 63 | * <p/> | |
| 64 | * If this collection maintains an ordering for its elements, the order of those | |
| 65 | * elements will be preserved in the result. | |
| 66 | * | |
| 67 | * @return an array of this collection's elements | |
| 68 | * @see #toArray(Class) | |
| 69 | * @see Collections#toArray(Collection,Class) | |
| 70 | */ | |
| 71 | Object[] toArray(); | |
| 72 | ||
| 73 | /** | |
| 74 | * Answers an array with the same elements as this collection, with the same | |
| 75 | * {@linkplain #size() size} as this collection, and a component type of the given | |
| 76 | * class. This means that only elements of the given class or any of its | |
| 77 | * derivatives may be inserted into the result. | |
| 78 | * <p/> | |
| 79 | * Note that even though the given class will be the runtime component class of the | |
| 80 | * result array, the return type of this method is {@code Object[]}. With Java | |
| 81 | * generics, it is not possible to express the return type as an array of the | |
| 82 | * component class without sacrificing the flexibility of providing a | |
| 83 | * {@code componentClass} that is a supertype of the generic parameter's actual | |
| 84 | * argument. Therefore, the caller should downcast to the desired type, or risk | |
| 85 | * raising {@link ArrayStoreException} when inserting via a reference of type | |
| 86 | * {@code Object[]}. | |
| 87 | * <p/> | |
| 88 | * If this collection maintains an ordering for its elements, the order of those | |
| 89 | * elements will be preserved in the result. | |
| 90 | * | |
| 91 | * @param componentClass the desired type of the result array's components | |
| 92 | * @return an array of this collection's elements | |
| 93 | * @throws NullPointerException if {@code componentClass} is {@code null} | |
| 94 | * @see Collections#toArray(Collection,Class) | |
| 95 | */ | |
| 96 | Object[] toArray( Class<? super E> componentClass ); | |
| 97 | ||
| 98 | /** | |
| 99 | * Answers a bag with the same elements as this collection. | |
| 100 | * | |
| 101 | * @return a bag of this collection's elements | |
| 102 | */ | |
| 103 | Bag<E> toBag(); | |
| 104 | ||
| 105 | /** | |
| 106 | * Answers an identity bag with the same elements as this collection. | |
| 107 | * | |
| 108 | * @return a bag of this collection's elements | |
| 109 | */ | |
| 110 | IdentityBag<E> toIdentityBag(); | |
| 111 | ||
| 112 | /** | |
| 113 | * Answers an identity set with the same elements as this collection. Since sets | |
| 114 | * do not store duplicate elements, the result may have fewer elements than this | |
| 115 | * collection. | |
| 116 | * | |
| 117 | * @return a set of this collection's elements | |
| 118 | */ | |
| 119 | IdentitySet<E> toIdentitySet(); | |
| 120 | ||
| 121 | /** | |
| 122 | * Answers an ordered collection with the same elements as this collection. | |
| 123 | * The result has the same {@linkplain #size() size} as this collection. | |
| 124 | * <p/> | |
| 125 | * If this collection maintains an ordering for its elements, the order of those | |
| 126 | * elements will be preserved in the result. | |
| 127 | * | |
| 128 | * @return an ordered collection of this collection's elements | |
| 129 | */ | |
| 130 | OrderedCollection<E> toOrderedCollection(); | |
| 131 | ||
| 132 | /** | |
| 133 | * Answers a set with the same elements as this collection. Since sets do not store | |
| 134 | * duplicate elements, the result may have fewer elements than this collection. | |
| 135 | * | |
| 136 | * @return a set of this collection's elements | |
| 137 | */ | |
| 138 | Set<E> toSet(); | |
| 139 | ||
| 140 | /** | |
| 141 | * Answers a sorted collection with the same elements as this collection. If this | |
| 142 | * collection has established an ordering on its elements, then that ordering is used | |
| 143 | * in the answer. Otherwise, order of the elements is arbitrary--in such a case, | |
| 144 | * one should probably use the {@linkplain #toSortedCollection(Comparator) overload | |
| 145 | * of this method that accepts a comparator}. | |
| 146 | * | |
| 147 | * @return a sorted collection of this collection's elements | |
| 148 | */ | |
| 149 | SortedCollection<E> toSortedCollection(); | |
| 150 | ||
| 151 | /** | |
| 152 | * Answers a sorted collection with the same elements as this collection, using the | |
| 153 | * given comparator to order the elements. | |
| 154 | * | |
| 155 | * @param comparator a comparator | |
| 156 | * @return a sorted collection of this collection's elements | |
| 157 | */ | |
| 158 | SortedCollection<E> toSortedCollection( Comparator<? super E> comparator ); | |
| 159 | ||
| 160 | /** | |
| 161 | * Evaluates the given transformer for each element of this collection, with the | |
| 162 | * element as the parameter, and answers a new collection containing the results | |
| 163 | * of these evaluations. | |
| 164 | * <p/> | |
| 165 | * The elements are traversed in the order specified by {@link | |
| 166 | * #forEachDo(UnaryFunctor) forEachDo} for this collection. | |
| 167 | * <p/> | |
| 168 | * Unless specifically refined, this message is defined to answer an object | |
| 169 | * conforming to the same protocol as this collection. | |
| 170 | * | |
| 171 | * @param <R> return type of the transformer | |
| 172 | * @param transformer the transformer to evaluate | |
| 173 | * @return a collection of the transformations | |
| 174 | * @throws NullPointerException if {@code transformer} is {@code null} | |
| 175 | */ | |
| 176 | <R> Collection<R> collect( UnaryFunctor<? super E, ? extends R> transformer ); | |
| 177 | ||
| 178 | /** | |
| 179 | * Answers the first element of this collection for which the given discriminator | |
| 180 | * answers {@code true} when given that element as an argument. | |
| 181 | * <p/> | |
| 182 | * {@code discriminator} will only be evaluated until such an object is found or | |
| 183 | * until all of the elements of the collection have been used as arguments. | |
| 184 | * That is, there may be elements of this collection that are never used as | |
| 185 | * arguments to {@code discriminator}. | |
| 186 | * <p/> | |
| 187 | * The elements are traversed in the order specified by {@link | |
| 188 | * #forEachDo(UnaryFunctor) forEachDo} for this collection. | |
| 189 | * | |
| 190 | * @param discriminator the discriminator to evaluate | |
| 191 | * @return the first element of this collection that satisfies {@code discriminator} | |
| 192 | * @throws java.util.NoSuchElementException if no element of this collection | |
| 193 | * satisfies {@code discriminator} | |
| 194 | * @throws NullPointerException if {@code discriminator} is {@code null} | |
| 195 | */ | |
| 196 | E detect( UnaryCondition<? super E> discriminator ); | |
| 197 | ||
| 198 | /** | |
| 199 | * For each element of this collection, evaluates the given operation with the | |
| 200 | * element as the parameter. | |
| 201 | * <p/> | |
| 202 | * Unless specifically refined, the elements are not traversed in a particular order. | |
| 203 | * Each element is visited exactly once. Conformant protocols may refine this | |
| 204 | * message to specify a particular ordering. | |
| 205 | * | |
| 206 | * @param operation the operation to perform | |
| 207 | * @throws NullPointerException if {@code operation} is {@code null} | |
| 208 | */ | |
| 209 | void forEachDo( UnaryFunctor<? super E, ?> operation ); | |
| 210 | ||
| 211 | /** | |
| 212 | * Answers {@code true} if an element of this collection is equivalent to the | |
| 213 | * given one; answers {@code false} otherwise. | |
| 214 | * | |
| 215 | * @param target the object to compare against | |
| 216 | * @return whether an equivalent object is in the collection | |
| 217 | */ | |
| 218 | boolean includes( E target ); | |
| 219 | ||
| 220 | /** | |
| 221 | * Answers the final result of evaluating the given operation using each element of | |
| 222 | * this collection and the previous evaluation result as the parameters. | |
| 223 | * <p/> | |
| 224 | * The first evaluation of {@code operation} is performed with the given initial | |
| 225 | * value as the first parameter, and the first element of this collection as the | |
| 226 | * second parameter. Subsequent evaluations are done with the result of the | |
| 227 | * previous evaluation as the first parameter, and the next element as the second | |
| 228 | * parameter. The result of the last evaluation is answered. | |
| 229 | * <p/> | |
| 230 | * If this collection is {@linkplain #isEmpty() empty}, answers the initial value. | |
| 231 | * <p/> | |
| 232 | * The elements are traversed in the order specified by {@link | |
| 233 | * #forEachDo(UnaryFunctor) forEachDo} for this collection. | |
| 234 | * | |
| 235 | * @param <R> return type and type of first argument of the operation | |
| 236 | * @param initialValue first parameter for the first evaluation | |
| 237 | * @param operation the operation to evaluate | |
| 238 | * @return the result of the last evaluation | |
| 239 | * @throws NullPointerException if {@code operation} is {@code null} | |
| 240 | */ | |
| 241 | <R> R inject( R initialValue, | |
| 242 | BinaryFunctor<? super R, ? super E, ? extends R> operation ); | |
| 243 | ||
| 244 | /** | |
| 245 | * Answers {@code true} iff this collection's {@linkplain #size() size} is zero; | |
| 246 | * otherwise answers {@code false}. | |
| 247 | * | |
| 248 | * @return whether this collection contains any elements | |
| 249 | */ | |
| 250 | boolean isEmpty(); | |
| 251 | ||
| 252 | /** | |
| 253 | * Answers the number of elements of this collection which are equivalent to | |
| 254 | * the given target. | |
| 255 | * | |
| 256 | * @param target the object to compare against | |
| 257 | * @return the number of occurrences of equivalent objects in this collection | |
| 258 | */ | |
| 259 | int occurrencesOf( E target ); | |
| 260 | ||
| 261 | /** | |
| 262 | * Re-establishes any hash invariants of this collection. | |
| 263 | */ | |
| 264 | void rehash(); | |
| 265 | ||
| 266 | /** | |
| 267 | * Answers a new collection which contains only the elements in this collection | |
| 268 | * which cause the given discriminator to answer {@code false}. | |
| 269 | * <p/> | |
| 270 | * For each element of this collection, {@code discriminator} is evaluated with the | |
| 271 | * element as the parameter. Each element which causes {@code discriminator} to | |
| 272 | * answer {@code false} is included in the new collection. | |
| 273 | * <p/> | |
| 274 | * The elements are traversed in the order specified by {@link | |
| 275 | * #forEachDo(UnaryFunctor) forEachDo} for this collection. | |
| 276 | * <p/> | |
| 277 | * Unless specifically refined, this message is defined to answer an object | |
| 278 | * conforming to the same protocol as this collection. If both this collection | |
| 279 | * and the result maintain an ordering of their elements, the elements of the | |
| 280 | * result will be in the same relative order as the elements of this collection. | |
| 281 | * | |
| 282 | * @param discriminator the discriminator to evaluate | |
| 283 | * @return a collection of the rejected elements of this collection | |
| 284 | * @throws NullPointerException if {@code discriminator} is {@code null} | |
| 285 | */ | |
| 286 | Collection<E> reject( UnaryCondition<? super E> discriminator ); | |
| 287 | ||
| 288 | /** | |
| 289 | * Answers a new collection which contains only the elements in this collection | |
| 290 | * which cause the given discriminator to answer {@code true}. | |
| 291 | * <p/> | |
| 292 | * For each element of this collection, {@code discriminator} is evaluated with the | |
| 293 | * element as the parameter. Each element which causes {@code discriminator} to | |
| 294 | * answer {@code true} is included in the new collection. | |
| 295 | * <p/> | |
| 296 | * The elements are traversed in order specified by {@link | |
| 297 | * #forEachDo(UnaryFunctor) forEachDo} for this collection. | |
| 298 | * <p/> | |
| 299 | * Unless specifically refined, this message is defined to answer an object | |
| 300 | * conforming to the same protocol as this collection. If both this collection | |
| 301 | * and the result maintain an ordering of their elements, the elements of the | |
| 302 | * result will be in the same relative order as the elements of this collection. | |
| 303 | * | |
| 304 | * @param discriminator the discriminator to evaluate | |
| 305 | * @return a collection of the selected elements of this collection | |
| 306 | * @throws NullPointerException if {@code discriminator} is {@code null} | |
| 307 | */ | |
| 308 | Collection<E> select( UnaryCondition<? super E> discriminator ); | |
| 309 | ||
| 310 | /** | |
| 311 | * Answers the number of elements in this collection. | |
| 312 | * | |
| 313 | * @return the number of elements in this collection | |
| 314 | */ | |
| 315 | int size(); | |
| 316 | } |