KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > functors > InstanceofPredicate


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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: 1.6 $ $Date: 2004/05/16 11:16:01 $
28  *
29  * @author Stephen Colebourne
30  */

31 public final class InstanceofPredicate implements Predicate, Serializable JavaDoc {
32
33     /** Serial version UID */
34     static final long serialVersionUID = -6682656911025165584L;
35
36     /** The type to compare to */
37     private final Class JavaDoc iType;
38     
39     /**
40      * Factory to create the identity predicate.
41      *
42      * @param type the type to check for, may not be null
43      * @return the predicate
44      * @throws IllegalArgumentException if the class is null
45      */

46     public static Predicate getInstance(Class JavaDoc type) {
47         if (type == null) {
48             throw new IllegalArgumentException JavaDoc("The type to check instanceof must not be null");
49         }
50         return new InstanceofPredicate(type);
51     }
52
53     /**
54      * Constructor that performs no validation.
55      * Use <code>getInstance</code> if you want that.
56      *
57      * @param type the type to check for
58      */

59     public InstanceofPredicate(Class JavaDoc type) {
60         super();
61         iType = type;
62     }
63
64     /**
65      * Evaluates the predicate returning true if the input object is of the correct type.
66      *
67      * @param object the input object
68      * @return true if input is of stored type
69      */

70     public boolean evaluate(Object JavaDoc object) {
71         return (iType.isInstance(object));
72     }
73
74     /**
75      * Gets the type to compare to.
76      *
77      * @return the type
78      * @since Commons Collections 3.1
79      */

80     public Class JavaDoc getType() {
81         return iType;
82     }
83
84 }
85
Popular Tags