Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GeneratorContains |
|
| 1.3333333333333333;1.333 | ||||
GeneratorContains$ContainsProcedure |
|
| 1.3333333333333333;1.333 |
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.core.algorithm; | |
18 | ||
19 | import java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.BinaryPredicate; | |
22 | import org.apache.commons.functor.UnaryPredicate; | |
23 | import org.apache.commons.functor.UnaryProcedure; | |
24 | import org.apache.commons.functor.generator.Generator; | |
25 | ||
26 | /** | |
27 | * Tests whether a {@link Generator} contains an element that matches a {@link UnaryPredicate}. | |
28 | * | |
29 | * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ | |
30 | */ | |
31 | 2 | public final class GeneratorContains<T> implements BinaryPredicate<Generator<? extends T>, UnaryPredicate<? super T>>, |
32 | Serializable { | |
33 | /** | |
34 | * serialVersionUID declaration. | |
35 | */ | |
36 | private static final long serialVersionUID = -1539983619621733276L; | |
37 | 2 | private static final GeneratorContains<Object> INSTANCE = new GeneratorContains<Object>(); |
38 | ||
39 | /** | |
40 | * Helper procedure. | |
41 | */ | |
42 | 2 | private static class ContainsProcedure<T> implements UnaryProcedure<T> { |
43 | private final UnaryPredicate<? super T> pred; | |
44 | private boolean found; | |
45 | ||
46 | /** | |
47 | * Create a new ContainsProcedure. | |
48 | * @pred test | |
49 | */ | |
50 | 12 | public ContainsProcedure(UnaryPredicate<? super T> pred) { |
51 | 12 | this.pred = pred; |
52 | 12 | } |
53 | ||
54 | /** | |
55 | * {@inheritDoc} | |
56 | */ | |
57 | public void run(T obj) { | |
58 | 60 | found |= pred.test(obj); |
59 | 60 | } |
60 | } | |
61 | ||
62 | /** | |
63 | * {@inheritDoc} | |
64 | * @param left Generator | |
65 | * @param right UnaryPredicate | |
66 | */ | |
67 | public boolean test(Generator<? extends T> left, UnaryPredicate<? super T> right) { | |
68 | 12 | ContainsProcedure<T> findProcedure = new ContainsProcedure<T>(right); |
69 | 12 | left.run(findProcedure); |
70 | 12 | return findProcedure.found; |
71 | } | |
72 | ||
73 | /** | |
74 | * {@inheritDoc} | |
75 | */ | |
76 | public boolean equals(Object obj) { | |
77 | 0 | return obj == this || obj != null && obj.getClass().equals(getClass()); |
78 | } | |
79 | ||
80 | /** | |
81 | * {@inheritDoc} | |
82 | */ | |
83 | public int hashCode() { | |
84 | 0 | return System.identityHashCode(INSTANCE); |
85 | } | |
86 | ||
87 | /** | |
88 | * Get a static {@link GeneratorContains} instance. | |
89 | * @return {@link GeneratorContains} | |
90 | */ | |
91 | public static GeneratorContains<Object> instance() { | |
92 | 12 | return INSTANCE; |
93 | } | |
94 | } |