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.functor.adapter;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.functor.BinaryPredicate;
022 import org.apache.commons.functor.UnaryPredicate;
023
024 /**
025 * Adapts a
026 * {@link UnaryPredicate UnaryPredicate}
027 * to the
028 * {@link BinaryPredicate BinaryPredicate} interface
029 * by ignoring the first binary argument.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying functor is. Attempts to serialize
035 * an instance whose delegate is not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @param <L> the left argument type.
039 * @param <R> the right argument type.
040 * @version $Revision: 1157611 $ $Date: 2011-08-14 21:45:14 +0200 (Sun, 14 Aug 2011) $
041 * @author Rodney Waldhoff
042 */
043 public final class IgnoreLeftPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
044 /**
045 * serialVersionUID declaration.
046 */
047 private static final long serialVersionUID = -3200352647509255939L;
048 /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */
049 private final UnaryPredicate<? super R> predicate;
050
051 /**
052 * Create a new IgnoreLeftPredicate.
053 * @param predicate the right predicate
054 */
055 public IgnoreLeftPredicate(UnaryPredicate<? super R> predicate) {
056 if (predicate == null) {
057 throw new IllegalArgumentException("UnaryPredicate argument was null");
058 }
059 this.predicate = predicate;
060 }
061
062 /**
063 * {@inheritDoc}
064 */
065 public boolean test(L left, R right) {
066 return predicate.test(right);
067 }
068
069 /**
070 * {@inheritDoc}
071 */
072 public boolean equals(Object that) {
073 return that == this || (that instanceof IgnoreLeftPredicate<?, ?> && equals((IgnoreLeftPredicate<?, ?>) that));
074 }
075
076 /**
077 * Learn whether another IgnoreLeftPredicate is equal to this.
078 * @param that the IgnoreLeftPredicate to test
079 * @return boolean
080 */
081 public boolean equals(IgnoreLeftPredicate<?, ?> that) {
082 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
083 }
084
085 /**
086 * {@inheritDoc}
087 */
088 public int hashCode() {
089 int hash = "IgnoreLeftPredicate".hashCode();
090 if (null != predicate) {
091 hash ^= predicate.hashCode();
092 }
093 return hash;
094 }
095
096 /**
097 * {@inheritDoc}
098 */
099 public String toString() {
100 return "IgnoreLeftPredicate<" + predicate + ">";
101 }
102
103 /**
104 * Adapt a UnaryPredicate to an IgnoreLeftPredicate.
105 * @param <L> left type
106 * @param <R> right type
107 * @param predicate to adapt
108 * @return IgnoreLeftPredicate<L, R>
109 */
110 public static <L, R> IgnoreLeftPredicate<L, R> adapt(UnaryPredicate<? super R> predicate) {
111 return null == predicate ? null : new IgnoreLeftPredicate<L, R>(predicate);
112 }
113
114 }