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.Predicate;
21:
22: /**
23: * Predicate implementation that returns true if the input is an instanceof
24: * the type stored in this predicate.
25: *
26: * @since Commons Collections 3.0
27: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
28: *
29: * @author Stephen Colebourne
30: */
31: public final class InstanceofPredicate implements Predicate,
32: Serializable {
33:
34: /** Serial version UID */
35: private static final long serialVersionUID = -6682656911025165584L;
36:
37: /** The type to compare to */
38: private final Class iType;
39:
40: /**
41: * Factory to create the identity predicate.
42: *
43: * @param type the type to check for, may not be null
44: * @return the predicate
45: * @throws IllegalArgumentException if the class is null
46: */
47: public static Predicate getInstance(Class type) {
48: if (type == null) {
49: throw new IllegalArgumentException(
50: "The type to check instanceof must not be null");
51: }
52: return new InstanceofPredicate(type);
53: }
54:
55: /**
56: * Constructor that performs no validation.
57: * Use <code>getInstance</code> if you want that.
58: *
59: * @param type the type to check for
60: */
61: public InstanceofPredicate(Class type) {
62: super ();
63: iType = type;
64: }
65:
66: /**
67: * Evaluates the predicate returning true if the input object is of the correct type.
68: *
69: * @param object the input object
70: * @return true if input is of stored type
71: */
72: public boolean evaluate(Object object) {
73: return (iType.isInstance(object));
74: }
75:
76: /**
77: * Gets the type to compare to.
78: *
79: * @return the type
80: * @since Commons Collections 3.1
81: */
82: public Class getType() {
83: return iType;
84: }
85:
86: }
|