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: import java.util.HashSet;
20: import java.util.Set;
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: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
30: *
31: * @author Stephen Colebourne
32: */
33: public final class UniquePredicate implements Predicate, Serializable {
34:
35: /** Serial version UID */
36: private static final long serialVersionUID = -3319417438027438040L;
37:
38: /** The set of previously seen objects */
39: private final Set iSet = new HashSet();
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 object) {
67: return iSet.add(object);
68: }
69:
70: }
|