Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TransformedGenerator |
|
| 2.5;2.5 | ||||
TransformedGenerator$1 |
|
| 2.5;2.5 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.functor.generator; | |
18 | ||
19 | import org.apache.commons.functor.UnaryFunction; | |
20 | import org.apache.commons.functor.UnaryProcedure; | |
21 | ||
22 | /** | |
23 | * Generator that transforms the elements of another Generator. | |
24 | * | |
25 | * @version $Revision: 1156799 $ $Date: 2011-08-11 22:11:54 +0200 (Thu, 11 Aug 2011) $ | |
26 | */ | |
27 | 256 | public class TransformedGenerator<I, E> extends BaseGenerator<E> { |
28 | private final UnaryFunction<? super I, ? extends E> func; | |
29 | ||
30 | /** | |
31 | * Create a new TransformedGenerator. | |
32 | * @param wrapped Generator to transform | |
33 | * @param func UnaryFunction to apply to each element | |
34 | */ | |
35 | public TransformedGenerator(Generator<? extends I> wrapped, UnaryFunction<? super I, ? extends E> func) { | |
36 | 20 | super(wrapped); |
37 | 20 | if (wrapped == null) { |
38 | 0 | throw new IllegalArgumentException("Generator argument was null"); |
39 | } | |
40 | 20 | if (func == null) { |
41 | 0 | throw new IllegalArgumentException("UnaryFunction argument was null"); |
42 | } | |
43 | 20 | this.func = func; |
44 | 20 | } |
45 | ||
46 | /** | |
47 | * {@inheritDoc} | |
48 | */ | |
49 | public void run(final UnaryProcedure<? super E> proc) { | |
50 | 20 | getWrappedGenerator().run(new UnaryProcedure<I>() { |
51 | public void run(I obj) { | |
52 | 256 | proc.run(func.evaluate(obj)); |
53 | 256 | } |
54 | }); | |
55 | 20 | } |
56 | ||
57 | /** | |
58 | * {@inheritDoc} | |
59 | */ | |
60 | @SuppressWarnings("unchecked") | |
61 | @Override | |
62 | protected Generator<? extends I> getWrappedGenerator() { | |
63 | 20 | return (Generator<? extends I>) super.getWrappedGenerator(); |
64 | } | |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | public boolean equals(Object obj) { | |
70 | 0 | if (obj == this) { |
71 | 0 | return true; |
72 | } | |
73 | 0 | if (!(obj instanceof TransformedGenerator<?, ?>)) { |
74 | 0 | return false; |
75 | } | |
76 | 0 | TransformedGenerator<?, ?> other = (TransformedGenerator<?, ?>) obj; |
77 | 0 | return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.func == func; |
78 | } | |
79 | ||
80 | /** | |
81 | * {@inheritDoc} | |
82 | */ | |
83 | public int hashCode() { | |
84 | 0 | int result = "TransformedGenerator".hashCode(); |
85 | 0 | result <<= 2; |
86 | 0 | result ^= getWrappedGenerator().hashCode(); |
87 | 0 | result <<= 2; |
88 | 0 | result ^= func.hashCode(); |
89 | 0 | return result; |
90 | } | |
91 | } |