Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionTransformer |
|
| 1.25;1.25 | ||||
CollectionTransformer$1 |
|
| 1.25;1.25 |
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.ArrayList; | |
18 | import java.util.Collection; | |
19 | ||
20 | import org.apache.commons.functor.UnaryFunction; | |
21 | import org.apache.commons.functor.UnaryProcedure; | |
22 | import org.apache.commons.functor.generator.Generator; | |
23 | ||
24 | /** | |
25 | * Transforms a generator into a collection. If a collection is not passed into | |
26 | * the constructor an ArrayList will be returned from the transform method. | |
27 | * | |
28 | * @param <E> the type of elements held in the adapted collection. | |
29 | * @since 1.0 | |
30 | * @version $Revision: 1160793 $ $Date: 2011-08-23 18:48:32 +0200 (Tue, 23 Aug 2011) $ | |
31 | * @author Jason Horman (jason@jhorman.org) | |
32 | */ | |
33 | 1600702 | public class CollectionTransformer<E> implements UnaryFunction<Generator<? extends E>, Collection<? super E>> { |
34 | /* | |
35 | * TODO revisit this class... it could stand a more-descriptive name. Also, it's a little | |
36 | * hard to say whether, for an instance constructed without a specific target collection, | |
37 | * #evaluate() should return a new ArrayList for each call, or continue adding to | |
38 | * a single ArrayList instance (the current behavior). | |
39 | * Perhaps this is more a documentation issue than anything. | |
40 | */ | |
41 | ||
42 | // instance methods | |
43 | //--------------------------------------------------- | |
44 | /** | |
45 | * The adapted collection has to be filled. | |
46 | */ | |
47 | private final Collection<? super E> toFill; | |
48 | ||
49 | // constructors | |
50 | //--------------------------------------------------- | |
51 | /** | |
52 | * Create a new CollectionTransformer. | |
53 | */ | |
54 | public CollectionTransformer() { | |
55 | 100 | this(null); |
56 | 100 | } |
57 | ||
58 | /** | |
59 | * Create a new CollectionTransformer. | |
60 | * @param toFill Collection to fill | |
61 | */ | |
62 | 120 | public CollectionTransformer(Collection<? super E> toFill) { |
63 | Collection<? super E> coll; | |
64 | 120 | if (toFill == null) { |
65 | 100 | coll = new ArrayList<E>(); |
66 | } else { | |
67 | 20 | coll = toFill; |
68 | } | |
69 | 120 | this.toFill = coll; |
70 | 120 | } |
71 | ||
72 | // instance methods | |
73 | //--------------------------------------------------- | |
74 | /** | |
75 | * {@inheritDoc} | |
76 | */ | |
77 | @SuppressWarnings("unchecked") | |
78 | public Collection<E> evaluate(Generator<? extends E> generator) { | |
79 | 120 | generator.run(new UnaryProcedure<E>() { |
80 | public void run(E obj) { | |
81 | 1600582 | toFill.add(obj); |
82 | 1600582 | } |
83 | }); | |
84 | 120 | return (Collection<E>) toFill; |
85 | } | |
86 | } |