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 */ 017 package org.apache.commons.functor.core.composite; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.BinaryPredicate; 022 023 /** 024 * Transposes (swaps) the arguments to some other 025 * {@link BinaryPredicate predicate}. 026 * For example, given a predicate <i>p</i> 027 * and the ordered pair of arguments <i>a</i>, 028 * <i>b</i>. 029 * {@link #test tests} 030 * <code>p.test(b,a)</code>. 031 * <p> 032 * Note that although this class implements 033 * {@link Serializable}, a given instance will 034 * only be truly <code>Serializable</code> if the 035 * underlying functor is. Attempts to serialize 036 * an instance whose delegate is not 037 * <code>Serializable</code> will result in an exception. 038 * </p> 039 * @version $Revision: 1170828 $ $Date: 2011-09-14 22:00:19 +0200 (Wed, 14 Sep 2011) $ 040 * @author Rodney Waldhoff 041 */ 042 public class TransposedPredicate<L, R> implements BinaryPredicate<L, R>, Serializable { 043 /** 044 * serialVersionUID declaration. 045 */ 046 private static final long serialVersionUID = 3441209087576289240L; 047 // attributes 048 // ------------------------------------------------------------------------ 049 private final BinaryPredicate<? super R, ? super L> predicate; 050 051 // constructor 052 // ------------------------------------------------------------------------ 053 /** 054 * Create a new TransposedPredicate. 055 * @param predicate the BinaryPredicate to transpose 056 */ 057 public TransposedPredicate(BinaryPredicate<? super R, ? super L> predicate) { 058 if (predicate == null) { 059 throw new IllegalArgumentException("BinaryPredicate argument must not be null"); 060 } 061 this.predicate = predicate; 062 } 063 064 // functor interface 065 // ------------------------------------------------------------------------ 066 /** 067 * {@inheritDoc} 068 */ 069 public final boolean test(L left, R right) { 070 return predicate.test(right, left); 071 } 072 073 /** 074 * {@inheritDoc} 075 */ 076 public boolean equals(Object that) { 077 return that == this || (that instanceof TransposedPredicate<?, ?> && equals((TransposedPredicate<?, ?>) that)); 078 } 079 080 /** 081 * Learn whether another TransposedPredicate is equal to this. 082 * @param that the TransposedPredicate to test 083 * @return boolean 084 */ 085 public boolean equals(TransposedPredicate<?, ?> that) { 086 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); 087 } 088 089 /** 090 * {@inheritDoc} 091 */ 092 public int hashCode() { 093 int hash = "TransposedPredicate".hashCode(); 094 if (null != predicate) { 095 hash ^= predicate.hashCode(); 096 } 097 return hash; 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 public String toString() { 104 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 return null == p ? null : new TransposedPredicate<R, L>(p); 116 } 117 118 }