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.Predicate; 022 import org.apache.commons.functor.UnaryPredicate; 023 024 /** 025 * Adapts a 026 * {@link UnaryPredicate UnaryPredicate} 027 * to the 028 * {@link Predicate Predicate} interface 029 * using a constant unary 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 * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ 039 * @author Rodney Waldhoff 040 */ 041 public final class BoundPredicate implements Predicate, Serializable { 042 /** 043 * serialVersionUID declaration. 044 */ 045 private static final long serialVersionUID = -5721164265625291834L; 046 /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */ 047 private final UnaryPredicate<Object> predicate; 048 /** The parameter to pass to {@code predicate}. */ 049 private final Object param; 050 051 /** 052 * Create a new BoundPredicate instance. 053 * @param <A> input type 054 * @param predicate the predicate to adapt 055 * @param arg the constant argument to use 056 */ 057 @SuppressWarnings("unchecked") 058 public <A> BoundPredicate(UnaryPredicate<? super A> predicate, A arg) { 059 if (predicate == null) { 060 throw new IllegalArgumentException("UnaryPredicate argument was null"); 061 } 062 this.predicate = (UnaryPredicate<Object>) predicate; 063 this.param = arg; 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 public boolean test() { 070 return predicate.test(param); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 public boolean equals(Object that) { 077 return that == this || (that instanceof BoundPredicate && equals((BoundPredicate) that)); 078 } 079 080 /** 081 * Learn whether another BoundPredicate is equal to this. 082 * @param that BoundPredicate to test 083 * @return boolean 084 */ 085 public boolean equals(BoundPredicate that) { 086 return null != that 087 && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)) 088 && (null == param ? null == that.param : param.equals(that.param)); 089 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 public int hashCode() { 096 int hash = "BoundPredicate".hashCode(); 097 if (null != predicate) { 098 hash <<= 2; 099 hash ^= predicate.hashCode(); 100 } 101 if (null != param) { 102 hash <<= 2; 103 hash ^= param.hashCode(); 104 } 105 return hash; 106 } 107 108 /** 109 * {@inheritDoc} 110 */ 111 public String toString() { 112 return "BoundPredicate<" + predicate + "(" + param + ")>"; 113 } 114 115 /** 116 * Adapt the given, possibly-<code>null</code>, 117 * {@link UnaryPredicate UnaryPredicate} to the 118 * {@link Predicate Predicate} interface by binding 119 * the specified <code>Object</code> as a constant 120 * argument. 121 * When the given <code>UnaryPredicate</code> is <code>null</code>, 122 * returns <code>null</code>. 123 * 124 * @param <A> input type 125 * @param predicate the possibly-<code>null</code> 126 * {@link UnaryPredicate UnaryPredicate} to adapt 127 * @param arg the object to bind as a constant argument 128 * @return a <code>BoundPredicate</code> wrapping the given 129 * {@link UnaryPredicate UnaryPredicate}, or <code>null</code> 130 * if the given <code>UnaryPredicate</code> is <code>null</code> 131 */ 132 public static <A> BoundPredicate bind(UnaryPredicate<? super A> predicate, A arg) { 133 return null == predicate ? null : new BoundPredicate(predicate, arg); 134 } 135 136 }