01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.functors;
17:
18: import java.io.Serializable;
19:
20: import org.apache.commons.collections.FunctorException;
21: import org.apache.commons.collections.Predicate;
22: import org.apache.commons.collections.Transformer;
23:
24: /**
25: * Predicate implementation that returns the result of a transformer.
26: *
27: * @since Commons Collections 3.0
28: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
29: *
30: * @author Stephen Colebourne
31: */
32: public final class TransformerPredicate implements Predicate,
33: Serializable {
34:
35: /** Serial version UID */
36: private static final long serialVersionUID = -2407966402920578741L;
37:
38: /** The transformer to call */
39: private final Transformer iTransformer;
40:
41: /**
42: * Factory to create the predicate.
43: *
44: * @param transformer the transformer to decorate
45: * @return the predicate
46: * @throws IllegalArgumentException if the transformer is null
47: */
48: public static Predicate getInstance(Transformer transformer) {
49: if (transformer == null) {
50: throw new IllegalArgumentException(
51: "The transformer to call must not be null");
52: }
53: return new TransformerPredicate(transformer);
54: }
55:
56: /**
57: * Constructor that performs no validation.
58: * Use <code>getInstance</code> if you want that.
59: *
60: * @param transformer the transformer to decorate
61: */
62: public TransformerPredicate(Transformer transformer) {
63: super ();
64: iTransformer = transformer;
65: }
66:
67: /**
68: * Evaluates the predicate returning the result of the decorated transformer.
69: *
70: * @param object the input object
71: * @return true if decorated transformer returns Boolean.TRUE
72: * @throws FunctorException if the transformer returns an invalid type
73: */
74: public boolean evaluate(Object object) {
75: Object result = iTransformer.transform(object);
76: if (result instanceof Boolean == false) {
77: throw new FunctorException(
78: "Transformer must return an instanceof Boolean, it was a "
79: + (result == null ? "null object" : result
80: .getClass().getName()));
81: }
82: return ((Boolean) result).booleanValue();
83: }
84:
85: /**
86: * Gets the transformer.
87: *
88: * @return the transformer
89: * @since Commons Collections 3.1
90: */
91: public Transformer getTransformer() {
92: return iTransformer;
93: }
94:
95: }
|