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.NullaryFunction; 020import org.apache.commons.functor.NullaryPredicate; 021import org.apache.commons.lang3.Validate; 022 023/** 024 * Adapts a <code>Boolean</code>-valued 025 * {@link NullaryFunction NullaryFunction} to the 026 * {@link NullaryPredicate NullaryPredicate} interface. 027 * 028 * @version $Revision: 1365377 $ $Date: 2012-07-24 21:59:23 -0300 (Tue, 24 Jul 2012) $ 029 */ 030public final class NullaryFunctionNullaryPredicate implements NullaryPredicate { 031 032 /** The {@link NullaryFunction NullaryFunction} I'm wrapping. */ 033 private final NullaryFunction<Boolean> function; 034 035 /** 036 * Create a new NullaryFunctionNullaryPredicate. 037 * @param function to adapt 038 */ 039 public NullaryFunctionNullaryPredicate(NullaryFunction<Boolean> function) { 040 this.function = Validate.notNull(function, "NullaryFunction argument was null"); 041 } 042 043 /** 044 * Returns the <code>boolean</code> value of the non-<code>null</code> 045 * <code>Boolean</code> returned by the {@link NullaryFunction#evaluate evaluate} 046 * method of my underlying function. 047 * {@inheritDoc} 048 */ 049 public boolean test() { 050 return function.evaluate().booleanValue(); 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 NullaryFunctionNullaryPredicate)) { 062 return false; 063 } 064 NullaryFunctionNullaryPredicate that = (NullaryFunctionNullaryPredicate) obj; 065 return this.function.equals(that.function); 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public int hashCode() { 073 int hash = "NullaryFunctionNullaryPredicate".hashCode(); 074 hash ^= function.hashCode(); 075 return hash; 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 @Override 082 public String toString() { 083 return "NullaryFunctionNullaryPredicate<" + function + ">"; 084 } 085 086 /** 087 * Adapt a NullaryFunction as a NullaryPredicate. 088 * @param function to adapt 089 * @return NullaryFunctionNullaryPredicate 090 */ 091 public static NullaryFunctionNullaryPredicate adapt(NullaryFunction<Boolean> function) { 092 return null == function ? null : new NullaryFunctionNullaryPredicate(function); 093 } 094}