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 */ 017package org.apache.commons.collections4.functors; 018 019import java.io.Serializable; 020 021import org.apache.commons.collections4.Equator; 022import org.apache.commons.collections4.Predicate; 023 024/** 025 * Predicate implementation that returns true if the input is the same object 026 * as the one stored in this predicate by equals. 027 * 028 * @since 3.0 029 */ 030public final class EqualPredicate<T> implements Predicate<T>, Serializable { 031 032 /** Serial version UID */ 033 private static final long serialVersionUID = 5633766978029907089L; 034 035 /** 036 * Factory to create the predicate. 037 * 038 * @param <T> the type that the predicate queries 039 * @param object the object to compare to 040 * @return the predicate 041 */ 042 public static <T> Predicate<T> equalPredicate(final T object) { 043 if (object == null) { 044 return NullPredicate.nullPredicate(); 045 } 046 return new EqualPredicate<>(object); 047 } 048 049 /** 050 * Factory to create the identity predicate. 051 * 052 * @param <T> the type that the predicate queries 053 * @param object the object to compare to 054 * @param equator the equator to use for comparison 055 * @return the predicate 056 * @since 4.0 057 */ 058 public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) { 059 if (object == null) { 060 return NullPredicate.nullPredicate(); 061 } 062 return new EqualPredicate<>(object, equator); 063 } 064 065 /** The value to compare to */ 066 private final T iValue; 067 068 /** The equator to use for comparison */ 069 private final Equator<T> equator; 070 071 /** 072 * Constructor that performs no validation. 073 * Use {@code equalPredicate} if you want that. 074 * 075 * @param object the object to compare to 076 */ 077 public EqualPredicate(final T object) { 078 // do not use the DefaultEquator to keep backwards compatibility 079 // the DefaultEquator returns also true if the two object references are equal 080 this(object, null); 081 } 082 083 /** 084 * Constructor that performs no validation. 085 * Use {@code equalPredicate} if you want that. 086 * 087 * @param object the object to compare to 088 * @param equator the equator to use for comparison 089 * @since 4.0 090 */ 091 public EqualPredicate(final T object, final Equator<T> equator) { 092 iValue = object; 093 this.equator = equator; 094 } 095 096 /** 097 * Evaluates the predicate returning true if the input equals the stored value. 098 * 099 * @param object the input object 100 * @return true if input object equals stored value 101 */ 102 @Override 103 public boolean evaluate(final T object) { 104 if (equator != null) { 105 return equator.equate(iValue, object); 106 } 107 return iValue.equals(object); 108 } 109 110 /** 111 * Gets the value. 112 * 113 * @return the value 114 * @since 3.1 115 */ 116 public Object getValue() { 117 return iValue; 118 } 119 120}