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.BinaryFunction;
022
023 /**
024 * Transposes (swaps) the arguments to some other
025 * {@link BinaryFunction function}.
026 * For example, given a function <i>f</i>
027 * and the ordered pair of arguments <i>a</i>,
028 * <i>b</i>.
029 * {@link #evaluate evaluates} to
030 * <code>f.evaluate(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: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
040 * @author Rodney Waldhoff
041 */
042 public class TransposedFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable {
043 /**
044 * serialVersionUID declaration.
045 */
046 private static final long serialVersionUID = -5824252875453493940L;
047 // attributes
048 // ------------------------------------------------------------------------
049 private final BinaryFunction<? super R, ? super L, ? extends T> function;
050
051 // constructor
052 // ------------------------------------------------------------------------
053 /**
054 * Create a new TransposedFunction.
055 * @param function BinaryFunction to transpose.
056 */
057 public TransposedFunction(BinaryFunction<? super R, ? super L, ? extends T> function) {
058 if (function == null) {
059 throw new IllegalArgumentException("BinaryFunction argument was null");
060 }
061 this.function = function;
062 }
063
064 // functor interface
065 // ------------------------------------------------------------------------
066 /**
067 * {@inheritDoc}
068 */
069 public final T evaluate(L left, R right) {
070 return function.evaluate(right, left);
071 }
072
073 /**
074 * {@inheritDoc}
075 */
076 public final boolean equals(Object that) {
077 return that == this || (that instanceof TransposedFunction<?, ?, ?>
078 && equals((TransposedFunction<?, ?, ?>) that));
079 }
080
081 /**
082 * Learn whether another TransposedFunction is equal to this.
083 * @param that TransposedFunction to test
084 * @return boolean
085 */
086 public final boolean equals(TransposedFunction<?, ?, ?> that) {
087 return null != that && (null == function ? null == that.function : function.equals(that.function));
088 }
089
090 /**
091 * {@inheritDoc}
092 */
093 public int hashCode() {
094 int hash = "TransposedFunction".hashCode();
095 if (null != function) {
096 hash ^= function.hashCode();
097 }
098 return hash;
099 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public String toString() {
105 return "TransposedFunction<" + function + ">";
106 }
107
108 // static
109 // ------------------------------------------------------------------------
110 /**
111 * Transpose a BinaryFunction.
112 * @param f BinaryFunction to transpose
113 * @return TransposedFunction
114 */
115 public static <L, R, T> TransposedFunction<R, L, T> transpose(BinaryFunction<? super L, ? super R, ? extends T> f) {
116 return null == f ? null : new TransposedFunction<R, L, T>(f);
117 }
118
119 }