KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashSet JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.commons.collections.Predicate;
23
24 /**
25  * Predicate implementation that returns true the first time an object is
26  * passed into the predicate.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.4 $ $Date: 2004/05/16 11:16:01 $
30  *
31  * @author Stephen Colebourne
32  */

33 public final class UniquePredicate implements Predicate, Serializable JavaDoc {
34
35     /** Serial version UID */
36     static final long serialVersionUID = -3319417438027438040L;
37     
38     /** The set of previously seen objects */
39     private final Set JavaDoc iSet = new HashSet JavaDoc();
40     
41     /**
42      * Factory to create the predicate.
43      *
44      * @return the predicate
45      * @throws IllegalArgumentException if the predicate is null
46      */

47     public static Predicate getInstance() {
48         return new UniquePredicate();
49     }
50
51     /**
52      * Constructor that performs no validation.
53      * Use <code>getInstance</code> if you want that.
54      */

55     public UniquePredicate() {
56         super();
57     }
58
59     /**
60      * Evaluates the predicate returning true if the input object hasn't been
61      * received yet.
62      *
63      * @param object the input object
64      * @return true if this is the first time the object is seen
65      */

66     public boolean evaluate(Object JavaDoc object) {
67         return iSet.add(object);
68     }
69
70 }
71
Popular Tags