Coverage Report - org.apache.commons.functor.core.composite.TransposedPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
TransposedPredicate
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.core.composite;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import org.apache.commons.functor.BinaryPredicate;
 22  
 
 23  
 /**
 24  
  * Transposes (swaps) the arguments to some other
 25  
  * {@link BinaryPredicate predicate}.
 26  
  * For example, given a predicate <i>p</i>
 27  
  * and the ordered pair of arguments <i>a</i>,
 28  
  * <i>b</i>.
 29  
  * {@link #test tests}
 30  
  * <code>p.test(b,a)</code>.
 31  
  * <p>
 32  
  * Note that although this class implements
 33  
  * {@link Serializable}, a given instance will
 34  
  * only be truly <code>Serializable</code> if the
 35  
  * underlying functor is.  Attempts to serialize
 36  
  * an instance whose delegate is not
 37  
  * <code>Serializable</code> will result in an exception.
 38  
  * </p>
 39  
  * @version $Revision: 1170828 $ $Date: 2011-09-14 22:00:19 +0200 (Wed, 14 Sep 2011) $
 40  
  * @author Rodney Waldhoff
 41  
  */
 42  
 public class TransposedPredicate<L, R> implements BinaryPredicate<L, R>, Serializable {
 43  
     /**
 44  
      * serialVersionUID declaration.
 45  
      */
 46  
     private static final long serialVersionUID = 3441209087576289240L;
 47  
     // attributes
 48  
     // ------------------------------------------------------------------------
 49  
     private final BinaryPredicate<? super R, ? super L> predicate;
 50  
 
 51  
     // constructor
 52  
     // ------------------------------------------------------------------------
 53  
     /**
 54  
      * Create a new TransposedPredicate.
 55  
      * @param predicate the BinaryPredicate to transpose
 56  
      */
 57  20
     public TransposedPredicate(BinaryPredicate<? super R, ? super L> predicate) {
 58  20
         if (predicate == null) {
 59  0
             throw new IllegalArgumentException("BinaryPredicate argument must not be null");
 60  
         }
 61  20
         this.predicate = predicate;
 62  20
     }
 63  
 
 64  
     // functor interface
 65  
     // ------------------------------------------------------------------------
 66  
     /**
 67  
      * {@inheritDoc}
 68  
      */
 69  
     public final boolean test(L left, R right) {
 70  4
         return predicate.test(right, left);
 71  
     }
 72  
 
 73  
     /**
 74  
      * {@inheritDoc}
 75  
      */
 76  
     public boolean equals(Object that) {
 77  26
         return that == this || (that instanceof TransposedPredicate<?, ?> && equals((TransposedPredicate<?, ?>) that));
 78  
     }
 79  
 
 80  
     /**
 81  
      * Learn whether another TransposedPredicate is equal to this.
 82  
      * @param that the TransposedPredicate to test
 83  
      * @return boolean
 84  
      */
 85  
     public boolean equals(TransposedPredicate<?, ?> that) {
 86  18
         return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
 87  
     }
 88  
 
 89  
     /**
 90  
      * {@inheritDoc}
 91  
      */
 92  
     public int hashCode() {
 93  28
         int hash = "TransposedPredicate".hashCode();
 94  28
         if (null != predicate) {
 95  28
             hash ^= predicate.hashCode();
 96  
         }
 97  28
         return hash;
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     public String toString() {
 104  20
         return "TransposedPredicate<" + predicate + ">";
 105  
     }
 106  
 
 107  
     // static
 108  
     // ------------------------------------------------------------------------
 109  
     /**
 110  
      * Return the transposition of <code>p</code>.
 111  
      * @param p BinaryPredicate to transpose
 112  
      * @return TransposedPredicate
 113  
      */
 114  
     public static <L, R> TransposedPredicate<R, L> transpose(BinaryPredicate<? super L, ? super R> p) {
 115  6
         return null == p ? null : new TransposedPredicate<R, L>(p);
 116  
     }
 117  
 
 118  
 }