Coverage Report - org.apache.commons.functor.adapter.BinaryFunctionBinaryPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
BinaryFunctionBinaryPredicate
92%
13/14
70%
14/20
2.143
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.functor.adapter;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import org.apache.commons.functor.BinaryFunction;
 22  
 import org.apache.commons.functor.BinaryPredicate;
 23  
 
 24  
 /**
 25  
  * Adapts a <code>Boolean</code>-valued {@link BinaryFunction BinaryFunction} to
 26  
  * the {@link BinaryPredicate BinaryPredicate} interface. <p> Note that although
 27  
  * this class implements {@link Serializable}, a given instance will only be
 28  
  * truly <code>Serializable</code> if the underlying functor is. Attempts to
 29  
  * serialize an instance whose delegate is not <code>Serializable</code> will
 30  
  * result in an exception. </p>
 31  
  * @param <L> the left argument type.
 32  
  * @param <R> the right argument type.
 33  
  * @version $Revision: 1156745 $ $Date: 2011-08-11 21:08:49 +0200 (Thu, 11 Aug 2011) $
 34  
  * @author Rodney Waldhoff
 35  
  */
 36  
 public final class BinaryFunctionBinaryPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
 37  
     /**
 38  
      * serialVersionUID declaration.
 39  
      */
 40  
     private static final long serialVersionUID = -5150315320718936186L;
 41  
     /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
 42  
     private final BinaryFunction<? super L, ? super R, Boolean> function;
 43  
 
 44  
     /**
 45  
      * Create an {@link BinaryPredicate BinaryPredicate} wrapping the given
 46  
      * {@link BinaryFunction BinaryFunction}.
 47  
      * @param function the {@link BinaryFunction BinaryFunction} to wrap
 48  
      */
 49  32
     public BinaryFunctionBinaryPredicate(final BinaryFunction<? super L, ? super R, Boolean> function) {
 50  32
         if (function == null) {
 51  0
             throw new IllegalArgumentException("BinaryFunction argument must not be null");
 52  
         }
 53  32
         this.function = function;
 54  32
     }
 55  
 
 56  
     /**
 57  
      * {@inheritDoc} Returns the <code>boolean</code> value of the
 58  
      * non-<code>null</code> <code>Boolean</code> returned by the
 59  
      * {@link BinaryFunction#evaluate evaluate} method of my underlying
 60  
      * function.
 61  
      *
 62  
      * The mehod throws NullPointerException if the underlying function returns
 63  
      * <code>null</code>, and
 64  
      * ClassCastException if the underlying function returns a
 65  
      * non-<code>Boolean</code>
 66  
      */
 67  
     public boolean test(final L left, final R right) {
 68  44
         return function.evaluate(left, right);
 69  
     }
 70  
 
 71  
     /**
 72  
      * {@inheritDoc}
 73  
      */
 74  
     public boolean equals(final Object that) {
 75  56
         return that == this
 76  
                 || (that instanceof BinaryFunctionBinaryPredicate<?, ?>
 77  
                 && equals((BinaryFunctionBinaryPredicate<?, ?>) that));
 78  
     }
 79  
 
 80  
     /**
 81  
      * Learn whether another BinaryFunctionBinaryPredicate is equal to this.
 82  
      * @param that BinaryFunctionBinaryPredicate to test
 83  
      * @return boolean
 84  
      */
 85  
     public boolean equals(final BinaryFunctionBinaryPredicate<?, ?> that) {
 86  20
         return null != that && (null == function ? null == that.function : function.equals(that.function));
 87  
     }
 88  
 
 89  
     /**
 90  
      * {@inheritDoc}
 91  
      */
 92  
     public int hashCode() {
 93  66
         int hash = "BinaryFunctionBinaryPredicate".hashCode();
 94  66
         if (null != function) {
 95  66
             hash ^= function.hashCode();
 96  
         }
 97  66
         return hash;
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     public String toString() {
 104  50
         return "BinaryFunctionBinaryPredicate<" + function + ">";
 105  
     }
 106  
 
 107  
     /**
 108  
      * Adapt the given, possibly-<code>null</code>, {@link BinaryFunction
 109  
      * BinaryFunction} to the {@link BinaryPredicate BinaryPredicate} interface.
 110  
      * When the given <code>BinaryFunction</code> is <code>null</code>, returns
 111  
      * <code>null</code>.
 112  
      *
 113  
      * @param <L> left type
 114  
      * @param <R> right type
 115  
      * @param <T> result type
 116  
      * @param function the possibly-<code>null</code> {@link BinaryFunction
 117  
      * BinaryFunction} to adapt
 118  
      * @return a <code>BinaryFunctionBinaryPredicate</code> wrapping the given
 119  
      * {@link BinaryFunction BinaryFunction}, or <code>null</code> if the given
 120  
      * <code>BinaryFunction</code> is <code>null</code>
 121  
      */
 122  
     public static <L, R, T> BinaryFunctionBinaryPredicate<L, R> adapt(
 123  
             final BinaryFunction<? super L, ? super R, Boolean> function) {
 124  12
         return null == function ? null : new BinaryFunctionBinaryPredicate<L, R>(function);
 125  
     }
 126  
 
 127  
 }