KickJava   Java API By Example, From Geeks To Geeks.

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


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 the same object
24  * as the one stored in this predicate.
25  *
26  * @since Commons Collections 3.0
27  * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $
28  *
29  * @author Stephen Colebourne
30  */

31 public final class IdentityPredicate implements Predicate, Serializable JavaDoc {
32
33     /** Serial version UID */
34     static final long serialVersionUID = -89901658494523293L;
35
36     
37     /** The value to compare to */
38     private final Object JavaDoc iValue;
39     
40     /**
41      * Factory to create the identity predicate.
42      *
43      * @param object the object to compare to
44      * @return the predicate
45      * @throws IllegalArgumentException if the predicate is null
46      */

47     public static Predicate getInstance(Object JavaDoc object) {
48         if (object == null) {
49             return NullPredicate.INSTANCE;
50         }
51         return new IdentityPredicate(object);
52     }
53
54     /**
55      * Constructor that performs no validation.
56      * Use <code>getInstance</code> if you want that.
57      *
58      * @param object the object to compare to
59      */

60     public IdentityPredicate(Object JavaDoc object) {
61         super();
62         iValue = object;
63     }
64
65     /**
66      * Evaluates the predicate returning true if the input object is identical to
67      * the stored object.
68      *
69      * @param object the input object
70      * @return true if input is the same object as the stored value
71      */

72     public boolean evaluate(Object JavaDoc object) {
73         return (iValue == object);
74     }
75
76     /**
77      * Gets the value.
78      *
79      * @return the value
80      * @since Commons Collections 3.1
81      */

82     public Object JavaDoc getValue() {
83         return iValue;
84     }
85
86 }
87
Popular Tags