001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.functors;
018
019 import java.io.Serializable;
020 import java.util.HashSet;
021 import java.util.Set;
022
023 import org.apache.commons.collections.Predicate;
024
025 /**
026 * Predicate implementation that returns true the first time an object is
027 * passed into the predicate.
028 *
029 * @since Commons Collections 3.0
030 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
031 *
032 * @author Stephen Colebourne
033 */
034 public final class UniquePredicate implements Predicate, Serializable {
035
036 /** Serial version UID */
037 private static final long serialVersionUID = -3319417438027438040L;
038
039 /** The set of previously seen objects */
040 private final Set iSet = new HashSet();
041
042 /**
043 * Factory to create the predicate.
044 *
045 * @return the predicate
046 * @throws IllegalArgumentException if the predicate is null
047 */
048 public static Predicate getInstance() {
049 return new UniquePredicate();
050 }
051
052 /**
053 * Constructor that performs no validation.
054 * Use <code>getInstance</code> if you want that.
055 */
056 public UniquePredicate() {
057 super();
058 }
059
060 /**
061 * Evaluates the predicate returning true if the input object hasn't been
062 * received yet.
063 *
064 * @param object the input object
065 * @return true if this is the first time the object is seen
066 */
067 public boolean evaluate(Object object) {
068 return iSet.add(object);
069 }
070
071 }