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 BinaryPredicate BinaryPredicate} 027 * to the 028 * {@link UnaryPredicate UnaryPredicate} interface 029 * using a constant left-side 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 objects are. Attempts to serialize 035 * an instance whose delegates are not 036 * <code>Serializable</code> will result in an exception. 037 * 038 * @param <A> the argument type. 039 * @version $Revision: 1157623 $ $Date: 2011-08-14 21:59:51 +0200 (Sun, 14 Aug 2011) $ 040 * @author Rodney Waldhoff 041 */ 042 public final class LeftBoundPredicate<A> implements UnaryPredicate<A>, Serializable { 043 044 /** 045 * serialVersionUID declaration. 046 */ 047 private static final long serialVersionUID = 3851481216909573294L; 048 /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */ 049 private final BinaryPredicate<Object, ? super A> predicate; 050 /** The parameter to pass to {@code predicate}. */ 051 private final Object param; 052 053 /** 054 * Create a new LeftBoundPredicate. 055 * @param <L> the left argument type. 056 * @param predicate the predicate to adapt 057 * @param arg the constant argument to use 058 */ 059 @SuppressWarnings("unchecked") 060 public <L> LeftBoundPredicate(BinaryPredicate<? super L, ? super A> predicate, L arg) { 061 if (predicate == null) { 062 throw new IllegalArgumentException("BinaryPredicate argument was null"); 063 } 064 this.predicate = (BinaryPredicate<Object, ? super A>) predicate; 065 this.param = arg; 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 public boolean test(A obj) { 072 return predicate.test(param, obj); 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 public boolean equals(Object that) { 079 return that == this || (that instanceof LeftBoundPredicate<?> && equals((LeftBoundPredicate<?>) that)); 080 } 081 082 /** 083 * Learn whether another LeftBoundPredicate is equal to this. 084 * @param that LeftBoundPredicate to test 085 * @return boolean 086 */ 087 public boolean equals(LeftBoundPredicate<?> that) { 088 return null != that 089 && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)) 090 && (null == param ? null == that.param : param.equals(that.param)); 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 public int hashCode() { 097 int hash = "LeftBoundPredicate".hashCode(); 098 if (null != predicate) { 099 hash <<= 2; 100 hash ^= predicate.hashCode(); 101 } 102 if (null != param) { 103 hash <<= 2; 104 hash ^= param.hashCode(); 105 } 106 return hash; 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 public String toString() { 113 return "LeftBoundPredicate<" + predicate + "(" + param + ",?)>"; 114 } 115 116 /** 117 * Adapt a BinaryPredicate to the UnaryPredicate interface. 118 * @param <L> the left argument type. 119 * @param <R> the right argument type. 120 * @param predicate to adapt 121 * @param arg Object argument to always send as the left operand to the wrapped function 122 * @return LeftBoundPredicate 123 */ 124 public static <L, R> LeftBoundPredicate<R> bind(BinaryPredicate<? super L, ? super R> predicate, L arg) { 125 return null == predicate ? null : new LeftBoundPredicate<R>(predicate, arg); 126 } 127 }