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 */ 017package org.apache.commons.functor.core.composite; 018 019import java.io.Serializable; 020 021import org.apache.commons.functor.BinaryProcedure; 022import org.apache.commons.lang3.Validate; 023 024/** 025 * Transposes (swaps) the arguments to some other 026 * {@link BinaryProcedure procedure}. 027 * For example, given a procedure <i>p</i> 028 * and the ordered pair of arguments <i>a</i>, 029 * <i>b</i>. 030 * {@link #run runs} 031 * <code>p.run(b,a)</code>. 032 * <p> 033 * Note that although this class implements 034 * {@link Serializable}, a given instance will 035 * only be truly <code>Serializable</code> if the 036 * underlying functor is. Attempts to serialize 037 * an instance whose delegate is not 038 * <code>Serializable</code> will result in an exception. 039 * </p> 040 * @param <L> the left argument type. 041 * @param <R> the right argument type. 042 * @version $Revision: 1365329 $ $Date: 2012-07-24 18:34:23 -0400 (Tue, 24 Jul 2012) $ 043 */ 044public class TransposedProcedure<L, R> implements BinaryProcedure<L, R>, Serializable { 045 /** 046 * serialVersionUID declaration. 047 */ 048 private static final long serialVersionUID = 4472683678460290015L; 049 // attributes 050 // ------------------------------------------------------------------------ 051 /** 052 * The adapted procedure. 053 */ 054 private final BinaryProcedure<? super R, ? super L> procedure; 055 056 // constructor 057 // ------------------------------------------------------------------------ 058 /** 059 * Create a new TransposedProcedure. 060 * @param procedure BinaryProcedure to transpose 061 */ 062 public TransposedProcedure(BinaryProcedure<? super R, ? super L> procedure) { 063 this.procedure = Validate.notNull(procedure, "BinaryProcedure argument was null"); 064 } 065 066 // functor interface 067 // ------------------------------------------------------------------------ 068 /** 069 * {@inheritDoc} 070 */ 071 public void run(L left, R right) { 072 procedure.run(right, left); 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override 079 public boolean equals(Object that) { 080 return that == this || (that instanceof TransposedProcedure<?, ?> && equals((TransposedProcedure<?, ?>) that)); 081 } 082 083 /** 084 * Learn whether another TransposedProcedure is equal to this. 085 * @param that TransposedPredicate to test 086 * @return boolean 087 */ 088 public boolean equals(TransposedProcedure<?, ?> that) { 089 return null != that && procedure.equals(that.procedure); 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public int hashCode() { 097 int hash = "TransposedProcedure".hashCode(); 098 hash ^= procedure.hashCode(); 099 return hash; 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 @Override 106 public String toString() { 107 return "TransposedProcedure<" + procedure + ">"; 108 } 109 110 // static 111 // ------------------------------------------------------------------------ 112 /** 113 * Transpose a BinaryProcedure. 114 * 115 * @param <L> the left argument type. 116 * @param <R> the right argument type. 117 * @param p to transpose 118 * @return TransposedProcedure 119 */ 120 public static <L, R> TransposedProcedure<R, L> transpose(BinaryProcedure<? super L, ? super R> p) { 121 return null == p ? null : new TransposedProcedure<R, L>(p); 122 } 123 124}