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 import org.apache.commons.functor.UnaryFunction; 023 024 /** 025 * A BinaryFunction whose result is then run through a UnaryFunction. 026 * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $ 027 * @author Matt Benson 028 */ 029 public class TransformedBinaryFunction<L, R, T> implements BinaryFunction<L, R, T>, Serializable { 030 /** 031 * serialVersionUID declaration. 032 */ 033 private static final long serialVersionUID = 3312781645741807814L; 034 035 /** 036 * Type-remembering helper 037 * @param <X> 038 */ 039 private static final class Helper<X, L, R, T> implements BinaryFunction<L, R, T>, Serializable { 040 /** 041 * serialVersionUID declaration. 042 */ 043 private static final long serialVersionUID = 8141488776884860650L; 044 private BinaryFunction<? super L, ? super R, ? extends X> preceding; 045 private UnaryFunction<? super X, ? extends T> following; 046 047 /** 048 * Create a new Helper. 049 * @param preceding BinaryFunction 050 * @param following UnaryFunction 051 */ 052 private Helper(BinaryFunction<? super L, ? super R, ? extends X> preceding, 053 UnaryFunction<? super X, ? extends T> following) { 054 this.preceding = preceding; 055 this.following = following; 056 } 057 058 /** 059 * {@inheritDoc} 060 */ 061 public T evaluate(L left, R right) { 062 return following.evaluate(preceding.evaluate(left, right)); 063 } 064 } 065 066 private final Helper<?, L, R, T> helper; 067 068 /** 069 * Create a new TransformedBinaryFunction. 070 * @param <X> 071 * @param preceding BinaryFunction 072 * @param following UnaryFunction 073 */ 074 public <X> TransformedBinaryFunction(BinaryFunction<? super L, ? super R, ? extends X> preceding, 075 UnaryFunction<? super X, ? extends T> following) { 076 this.helper = new Helper<X, L, R, T>(preceding, following); 077 } 078 079 /** 080 * {@inheritDoc} 081 */ 082 public final T evaluate(L left, R right) { 083 return helper.evaluate(left, right); 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public final boolean equals(Object obj) { 091 return obj == this || obj instanceof TransformedBinaryFunction<?, ?, ?> 092 && equals((TransformedBinaryFunction<?, ?, ?>) obj); 093 } 094 095 /** 096 * Learn whether another TransformedBinaryFunction is equal to <code>this</code>. 097 * @param that instance to test 098 * @return whether equal 099 */ 100 public final boolean equals(TransformedBinaryFunction<?, ?, ?> that) { 101 return that != null && that.helper.preceding.equals(this.helper.preceding) 102 && that.helper.following.equals(this.helper.following); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public int hashCode() { 110 int result = "TransformedBinaryFunction".hashCode(); 111 result <<= 2; 112 result |= helper.following.hashCode(); 113 result <<= 2; 114 result |= helper.preceding.hashCode(); 115 return result; 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 public String toString() { 123 return "TransformedBinaryFunction<" + helper.preceding + "; " + helper.following + ">"; 124 } 125 }