Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EachElement |
|
| 1.8;1.8 |
1 | /* | |
2 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
3 | * you may not use this file except in compliance with the License. | |
4 | * You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | ||
15 | package org.apache.commons.functor.generator.util; | |
16 | ||
17 | import java.util.Arrays; | |
18 | import java.util.Collection; | |
19 | import java.util.Iterator; | |
20 | import java.util.Map; | |
21 | ||
22 | import org.apache.commons.functor.generator.Generator; | |
23 | import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; | |
24 | ||
25 | /** | |
26 | * Generator factory for each element of a "collection". | |
27 | * | |
28 | * @since 1.0 | |
29 | * @version $Revision: 1160769 $ $Date: 2011-08-23 18:11:06 +0200 (Tue, 23 Aug 2011) $ | |
30 | * @author Jason Horman (jason@jhorman.org) | |
31 | */ | |
32 | public final class EachElement { | |
33 | /** | |
34 | * <p>{@code EachElement} instances should NOT be constructed in | |
35 | * standard programming. Instead, the methods of the class should be invoked | |
36 | * statically.</p> | |
37 | * | |
38 | * <p>This constructor is public to permit tools that require a JavaBean | |
39 | * instance to operate.</p> | |
40 | */ | |
41 | 0 | public EachElement() { |
42 | 0 | } |
43 | ||
44 | /** | |
45 | * Get a Generator for each element of a Collection. | |
46 | * @param <E> the type of elements held in the input collection. | |
47 | * @param collection to iterate | |
48 | * @return Generator<E> | |
49 | */ | |
50 | public static <E> Generator<E> from(Collection<? extends E> collection) { | |
51 | 20 | return collection == null ? null : EachElement.from(collection.iterator()); |
52 | } | |
53 | ||
54 | /** | |
55 | * Get a Generator for each entry of a Map. | |
56 | * @param <K> the type of keys maintained by the input map. | |
57 | * @param <V> the type of mapped values in the input map. | |
58 | * @param map to iterate | |
59 | * @return Generator | |
60 | */ | |
61 | @SuppressWarnings("unchecked") | |
62 | public static <K, V> Generator<Map.Entry<K, V>> from(Map<? extends K, ? extends V> map) { | |
63 | 4 | return map == null ? null : EachElement.from(((Map<K, V>) map).entrySet().iterator()); |
64 | } | |
65 | ||
66 | /** | |
67 | * Get a Generator for each element of an Object[]. | |
68 | * @param <E> the type of elements held in the input array. | |
69 | * @param array to iterate | |
70 | * @return Generator | |
71 | */ | |
72 | public static <E> Generator<E> from(E[] array) { | |
73 | 4 | return array == null ? null : EachElement.from(Arrays.asList(array).iterator()); |
74 | } | |
75 | ||
76 | /** | |
77 | * Get a Generator for each element of an Iterator. | |
78 | * @param <E> the type of elements held in the input iterator. | |
79 | * @param iter to iterate | |
80 | * @return Generator | |
81 | */ | |
82 | public static <E> Generator<E> from(Iterator<? extends E> iter) { | |
83 | 26 | return iter == null ? null : new IteratorToGeneratorAdapter<E>(iter); |
84 | } | |
85 | } |