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
21 import java.io.Serializable;
22
23
24 /***
25 * <code>Predicate</code> implementation that wraps a <code>Transformer</code>
26 * whose output type is <code>Boolean</code>. The <code>Predicate</code>
27 * evaluates <code>true</code> if the <code>Transformer</code> returns
28 * <code>Boolean.TRUE</code>, and evaluates to <code>false</code> if the
29 * <code>Transformer</code> returns <code>Boolean.False</code> or
30 * <code>null</code>.
31 *
32 * @author Stephen Colebourne
33 * @author Chris Lambrou (port to Java 5.0)
34 * @since Collections15 1.0
35 */
36 public final class TransformerPredicate <E> implements Predicate<E>, Serializable
37 {
38
39 static final long serialVersionUID = 4651373347317863840L;
40
41 /***
42 * The wrapped <code>Transformer</code>.
43 */
44 private final Transformer<E, Boolean> transformer;
45
46 /***
47 * Returns an instance that evaluates input objects using the specified
48 * <code>Transformer</code>.
49 *
50 * @param transformer The <code>Transformer</code> to use.
51 *
52 * @return A <code>Predicate</code> that evaluates input objects using the
53 * specified <code>Transformer</code>.
54 *
55 * @throws IllegalArgumentException Thrown if the <code>transformer</code>
56 * argument is <code>null</code>.
57 */
58 public static <T> Predicate<T> getInstance(Transformer<T, Boolean> transformer)
59 {
60 if (transformer == null) {
61 throw new IllegalArgumentException("The transformer to call must not be null");
62 }
63 return new TransformerPredicate<T>(transformer);
64 }
65
66 /***
67 * Creates a new instance that evaluates input objects using the specified
68 * <code>Transformer</code>.
69 *
70 * @param transformer The <code>Transformer</code> to use.
71 *
72 * @throws IllegalArgumentException Thrown if the <code>transformer</code>
73 * argument is <code>null</code>.
74 */
75 protected TransformerPredicate(Transformer<E, Boolean> transformer)
76 {
77 if (transformer == null) {
78 throw new IllegalArgumentException("null transformer specified");
79 }
80 this.transformer = transformer;
81 }
82
83 /***
84 * Evaluates the <code>Predicate</code> returning the result of the wrapped
85 * <code>Transformer</code>.
86 *
87 * @param object The input object yo evaluate.
88 *
89 * @return <code>true</code> if the underlying <code>Transformer</code>
90 * returns <code>Boolean.TRUE</code>. <code>false</code> if the
91 * underlying <code>Transformer</code> returns <code>Boolean.FALSE</code>
92 * or <code>null</code>.
93 */
94 public boolean evaluate(E object)
95 {
96 Boolean val = transformer.transform(object);
97 return (val == null) ? false : val.booleanValue();
98 }
99
100 /***
101 * Returns the underlying <code>Transformer</code>.
102 *
103 * @return The underlying <code>Transformer</code>.
104 *
105 * @since Collections15 1.0
106 */
107 public Transformer<E, Boolean> getTransformer()
108 {
109 return transformer;
110 }
111
112 }