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.NullaryPredicate; 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 NullaryPredicate NullaryPredicate} interface 028 * using a constant unary argument. 029 * 030 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 031 */ 032public final class BoundNullaryPredicate implements NullaryPredicate { 033 /** The {@link Predicate Predicate} I'm wrapping. */ 034 private final Predicate<Object> predicate; 035 /** The parameter to pass to {@code predicate}. */ 036 private final Object param; 037 038 /** 039 * Create a new BoundNullaryPredicate instance. 040 * @param <A> input type 041 * @param predicate the predicate to adapt 042 * @param arg the constant argument to use 043 */ 044 @SuppressWarnings("unchecked") 045 public <A> BoundNullaryPredicate(Predicate<? super A> predicate, A arg) { 046 this.predicate = 047 (Predicate<Object>) Validate.notNull(predicate, 048 "Predicate argument was null"); 049 this.param = arg; 050 } 051 052 /** 053 * {@inheritDoc} 054 */ 055 public boolean test() { 056 return predicate.test(param); 057 } 058 059 /** 060 * {@inheritDoc} 061 */ 062 @Override 063 public boolean equals(Object obj) { 064 if (obj == this) { 065 return true; 066 } 067 if (!(obj instanceof BoundNullaryPredicate)) { 068 return false; 069 } 070 BoundNullaryPredicate that = (BoundNullaryPredicate) obj; 071 return predicate.equals(that.predicate) 072 && (null == param ? null == that.param : param.equals(that.param)); 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override 079 public int hashCode() { 080 int hash = "BoundNullaryPredicate".hashCode(); 081 hash <<= 2; 082 hash ^= predicate.hashCode(); 083 if (null != param) { 084 hash <<= 2; 085 hash ^= param.hashCode(); 086 } 087 return hash; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public String toString() { 095 return "BoundNullaryPredicate<" + predicate + "(" + param + ")>"; 096 } 097 098 /** 099 * Adapt the given, possibly-<code>null</code>, 100 * {@link Predicate Predicate} to the 101 * {@link NullaryPredicate NullaryPredicate} interface by binding 102 * the specified <code>Object</code> as a constant 103 * argument. 104 * When the given <code>Predicate</code> is <code>null</code>, 105 * returns <code>null</code>. 106 * 107 * @param <A> input type 108 * @param predicate the possibly-<code>null</code> 109 * {@link Predicate Predicate} to adapt 110 * @param arg the object to bind as a constant argument 111 * @return a <code>BoundNullaryPredicate</code> wrapping the given 112 * {@link Predicate Predicate}, or <code>null</code> 113 * if the given <code>Predicate</code> is <code>null</code> 114 */ 115 public static <A> BoundNullaryPredicate bind(Predicate<? super A> predicate, A arg) { 116 return null == predicate ? null : new BoundNullaryPredicate(predicate, arg); 117 } 118 119}