1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.functors.predicate;
17
18 import net.sf.collections15.Predicate;
19 import net.sf.collections15.Transformer;
20 import net.sf.collections15.PredicateDecorator;
21
22
23 /***
24 * <code>Predicate</code> implementation whose {@link #evaluate} method
25 * transforms the input object and evaluates the result using another
26 * <code>Predicate</code> instance.
27 *
28 * @author Alban Peignier
29 * @author Stephen Colebourne
30 * @author Chris Lambrou (port to Java 5.0)
31 * @since Collections15 1.0
32 */
33 public final class TransformedPredicate <E, D> implements Predicate<E>, PredicateDecorator<D>
34 {
35
36 static final long serialVersionUID = 8019627955353752434L;
37
38 /***
39 * The <code>Transformer</code> used to transform input objects.
40 */
41 private Transformer<? super E, ? extends D> transformer;
42
43 /***
44 * The <code>Predicate</code> used to evaluate the transformed objects.
45 */
46 private Predicate<? super D> predicate;
47
48 /***
49 * Returns an instance that transforms input objects using the specified
50 * <code>Transformer</code> an evaluates the result using the specified
51 * <code>Predicate</code>.
52 *
53 * @param transformer The <code>Transformer</code> to use.
54 * @param predicate The <code>Predicate</code> to decorate.
55 *
56 * @return An instance that transforms input objects and evaluates the
57 * result using the decorated <code>Predicate</code>.
58 *
59 * @throws IllegalArgumentException Thrown if either argument is
60 * <code>null</code>.
61 */
62 public static <E, D> TransformedPredicate<E, D> getInstance(Transformer<? super E, ? extends D> transformer, Predicate<? super D> predicate)
63 {
64 return new TransformedPredicate<E, D>(transformer, predicate);
65 }
66
67 /***
68 * Creates a new instance that transforms input objects using the specified
69 * <code>Transformer</code> an evaluates the result using the specified
70 * <code>Predicate</code>.
71 *
72 * @param transformer The <code>Transformer</code> to use.
73 * @param predicate The <code>Predicate</code> to decorate.
74 *
75 * @throws IllegalArgumentException Thrown if either argument is
76 * <code>null</code>.
77 */
78 protected TransformedPredicate(Transformer<? super E, ? extends D> transformer, Predicate<? super D> predicate)
79 {
80 if (transformer == null) {
81 throw new IllegalArgumentException("transformer cannot be null");
82 }
83 if (predicate == null) {
84 throw new IllegalArgumentException("predicate cannot be null");
85 }
86 this.transformer = transformer;
87 this.predicate = predicate;
88 }
89
90 /***
91 * Transforms the input object and evaluates the result using the decorated
92 * <code>Predicate</code>..
93 *
94 * @param object The input object to transformt and evaluate.
95 *
96 * @return <code>true</code> if the decorated <code>Predicate</code> returns
97 * <code>true</code>. <code>false</code> if the decorated
98 * <code>Predicate</code> returns <code>false</code>.
99 */
100 public boolean evaluate(E object)
101 {
102 D intermediateResult = transformer.transform(object);
103 return predicate.evaluate(intermediateResult);
104 }
105
106 /***
107 * Returns the <code>Predicate</code> decorated by this
108 * <code>Predicate</code>.
109 *
110 * @return The decorated <code>Predicate</code>.
111 */
112 public Predicate<? super D> getDecoratedPredicate()
113 {
114 return predicate;
115 }
116
117 /***
118 * Gets the <code>Transformer</code> applied to the input objects before
119 * application to the internal <code>Predicate</code>.
120 *
121 * @return The <code>Transformer</code> applied to the input objects before
122 * application to the internal <code>Predicate</code>.
123 */
124 public Transformer<? super E, ? extends D> getTransformer()
125 {
126 return transformer;
127 }
128 }