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