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.functor.adapter; 018 019import org.apache.commons.functor.BinaryPredicate; 020import org.apache.commons.functor.Predicate; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a 025 * {@link Predicate Predicate} 026 * to the 027 * {@link BinaryPredicate BinaryPredicate} interface 028 * by ignoring the first binary argument. 029 * 030 * @param <L> the left argument type. 031 * @param <R> the right argument type. 032 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $ 033 */ 034public final class IgnoreLeftPredicate<L, R> implements BinaryPredicate<L, R> { 035 /** The {@link Predicate Predicate} I'm wrapping. */ 036 private final Predicate<? super R> predicate; 037 038 /** 039 * Create a new IgnoreLeftPredicate. 040 * @param predicate the right predicate 041 */ 042 public IgnoreLeftPredicate(Predicate<? super R> predicate) { 043 this.predicate = Validate.notNull(predicate, "Predicate argument was null"); 044 } 045 046 /** 047 * {@inheritDoc} 048 */ 049 public boolean test(L left, R right) { 050 return predicate.test(right); 051 } 052 053 /** 054 * {@inheritDoc} 055 */ 056 @Override 057 public boolean equals(Object obj) { 058 if (obj == this) { 059 return true; 060 } 061 if (!(obj instanceof IgnoreLeftPredicate<?, ?>)) { 062 return false; 063 } 064 IgnoreLeftPredicate<?, ?> that = (IgnoreLeftPredicate<?, ?>) obj; 065 return this.predicate.equals(that.predicate); 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public int hashCode() { 073 int hash = "IgnoreLeftPredicate".hashCode(); 074 hash ^= predicate.hashCode(); 075 return hash; 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public String toString() { 083 return "IgnoreLeftPredicate<" + predicate + ">"; 084 } 085 086 /** 087 * Adapt a Predicate to an IgnoreLeftPredicate. 088 * @param <L> left type 089 * @param <R> right type 090 * @param predicate to adapt 091 * @return IgnoreLeftPredicate<L, R> 092 */ 093 public static <L, R> IgnoreLeftPredicate<L, R> adapt(Predicate<? super R> predicate) { 094 return null == predicate ? null : new IgnoreLeftPredicate<L, R>(predicate); 095 } 096 097}