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