1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31 public final class GeneratorContains<T> implements BinaryPredicate<Generator<? extends T>, UnaryPredicate<? super T>>,
32 Serializable {
33
34
35
36 private static final long serialVersionUID = -1539983619621733276L;
37 private static final GeneratorContains<Object> INSTANCE = new GeneratorContains<Object>();
38
39
40
41
42 private static class ContainsProcedure<T> implements UnaryProcedure<T> {
43 private final UnaryPredicate<? super T> pred;
44 private boolean found;
45
46
47
48
49
50 public ContainsProcedure(UnaryPredicate<? super T> pred) {
51 this.pred = pred;
52 }
53
54
55
56
57 public void run(T obj) {
58 found |= pred.test(obj);
59 }
60 }
61
62
63
64
65
66
67 public boolean test(Generator<? extends T> left, UnaryPredicate<? super T> right) {
68 ContainsProcedure<T> findProcedure = new ContainsProcedure<T>(right);
69 left.run(findProcedure);
70 return findProcedure.found;
71 }
72
73
74
75
76 public boolean equals(Object obj) {
77 return obj == this || obj != null && obj.getClass().equals(getClass());
78 }
79
80
81
82
83 public int hashCode() {
84 return System.identityHashCode(INSTANCE);
85 }
86
87
88
89
90
91 public static GeneratorContains<Object> instance() {
92 return INSTANCE;
93 }
94 }