Coverage Report - org.apache.commons.functor.adapter.FullyBoundPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
FullyBoundPredicate
95%
22/23
77%
28/36
3
 
 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.BinaryPredicate;
 22  
 import org.apache.commons.functor.Predicate;
 23  
 
 24  
 /**
 25  
  * Adapts a
 26  
  * {@link BinaryPredicate BinaryPredicate}
 27  
  * to the
 28  
  * {@link org.apache.commons.functor.UnaryPredicate UnaryPredicate} interface
 29  
  * using a constant left-side argument.
 30  
  * <p/>
 31  
  * Note that although this class implements
 32  
  * {@link Serializable}, a given instance will
 33  
  * only be truly <code>Serializable</code> if the
 34  
  * underlying objects are.  Attempts to serialize
 35  
  * an instance whose delegates are not
 36  
  * <code>Serializable</code> will result in an exception.
 37  
  *
 38  
  * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
 39  
  * @author Matt Benson
 40  
  */
 41  
 public final class FullyBoundPredicate implements Predicate, Serializable {
 42  
 
 43  
     /**
 44  
      * serialVersionUID declaration.
 45  
      */
 46  
     private static final long serialVersionUID = 7676235030688391839L;
 47  
     /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
 48  
     private final BinaryPredicate<Object, Object> predicate;
 49  
     /** The left parameter to pass to {@code predicate}. */
 50  
     private final Object left;
 51  
     /** The right parameter to pass to {@code predicate}. */
 52  
     private final Object right;
 53  
 
 54  
     /**
 55  
      * Create a new FullyBoundPredicate instance.
 56  
      * @param <L> left type
 57  
      * @param <R> right type
 58  
      * @param predicate the predicate to adapt
 59  
      * @param left the left argument to use
 60  
      * @param right the right argument to use
 61  
      */
 62  
     @SuppressWarnings("unchecked")
 63  24
     public <L, R> FullyBoundPredicate(BinaryPredicate<? super L, ? super R> predicate, L left, R right) {
 64  24
         if (predicate == null) {
 65  0
             throw new IllegalArgumentException("BinaryPredicate argument was null");
 66  
         }
 67  24
         this.predicate = (BinaryPredicate<Object, Object>) predicate;
 68  24
         this.left = left;
 69  24
         this.right = right;
 70  24
     }
 71  
 
 72  
     /**
 73  
      * {@inheritDoc}
 74  
      */
 75  
     public boolean test() {
 76  2
         return predicate.test(left, right);
 77  
     }
 78  
 
 79  
     /**
 80  
      * {@inheritDoc}
 81  
      */
 82  
     public boolean equals(Object that) {
 83  30
         return that == this || (that instanceof FullyBoundPredicate && equals((FullyBoundPredicate) that));
 84  
     }
 85  
 
 86  
     /**
 87  
      * Learn whether another FullyBoundPredicate is equal to this.
 88  
      * @param that FullyBoundPredicate to test
 89  
      * @return boolean
 90  
      */
 91  
     public boolean equals(FullyBoundPredicate that) {
 92  22
         return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate))
 93  
                 && (null == left ? null == that.left : left.equals(that.left))
 94  
                 && (null == right ? null == that.right : right.equals(that.right));
 95  
     }
 96  
 
 97  
     /**
 98  
      * {@inheritDoc}
 99  
      */
 100  
     public int hashCode() {
 101  32
         int hash = "FullyBoundPredicate".hashCode();
 102  32
         if (null != predicate) {
 103  32
             hash <<= 2;
 104  32
             hash ^= predicate.hashCode();
 105  
         }
 106  32
         hash <<= 2;
 107  32
         if (null != left) {
 108  16
             hash ^= left.hashCode();
 109  
         }
 110  32
         hash <<= 2;
 111  32
         if (null != right) {
 112  16
             hash ^= right.hashCode();
 113  
         }
 114  32
         return hash;
 115  
     }
 116  
 
 117  
     /**
 118  
      * {@inheritDoc}
 119  
      */
 120  
     public String toString() {
 121  24
         return "FullyBoundPredicate<" + predicate + "(" + left + ", " + right + ")>";
 122  
     }
 123  
 
 124  
     /**
 125  
      * Adapt a BinaryPredicate to the Predicate interface.
 126  
      * @param predicate to adapt
 127  
      * @param <L> left type
 128  
      * @param <R> right type
 129  
      * @param left L argument to always send as the left operand to the wrapped function
 130  
      * @param right R argument to always send as the right operand to the wrapped function
 131  
      * @return FullyBoundPredicate
 132  
      */
 133  
     public static <L, R> FullyBoundPredicate bind(
 134  
             BinaryPredicate<? super L, ? super R> predicate, L left, R right) {
 135  6
         return null == predicate ? null : new FullyBoundPredicate(predicate, left, right);
 136  
     }
 137  
 }