1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.functor.core.composite;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.functor.Function;
22 import org.apache.commons.functor.UnaryFunction;
23
24 /**
25 * A Function whose result is then run through a UnaryFunction.
26 * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
27 * @author Matt Benson
28 */
29 public class TransformedFunction<T> implements Function<T>, Serializable {
30 /**
31 * serialVersionUID declaration.
32 */
33 private static final long serialVersionUID = 1201423110871342081L;
34
35 /**
36 * Type-remembering helper
37 * @param <X>
38 */
39 private static final class Helper<X, T> implements Function<T>, Serializable {
40 /**
41 * serialVersionUID declaration.
42 */
43 private static final long serialVersionUID = -7177784125292465809L;
44 private Function<? extends X> preceding;
45 private UnaryFunction<? super X, ? extends T> following;
46
47 /**
48 * Create a new Helper.
49 * @param preceding Function
50 * @param following UnaryFunction
51 */
52 private Helper(Function<? extends X> preceding, UnaryFunction<? super X, ? extends T> following) {
53 this.preceding = preceding;
54 this.following = following;
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 public T evaluate() {
61 return following.evaluate(preceding.evaluate());
62 }
63 }
64
65 private final Helper<?, T> helper;
66
67 /**
68 * Create a new TransformedFunction.
69 * @param <X>
70 * @param preceding Function
71 * @param following UnaryFunction
72 */
73 public <X> TransformedFunction(Function<? extends X> preceding,
74 UnaryFunction<? super X, ? extends T> following) {
75 this.helper = new Helper<X, T>(preceding, following);
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 public final T evaluate() {
82 return helper.evaluate();
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public final boolean equals(Object obj) {
90 return obj == this || obj instanceof TransformedFunction<?> && equals((TransformedFunction<?>) obj);
91 }
92
93 /**
94 * Learn whether another TransformedFunction is equal to <code>this</code>.
95 * @param that instance to test
96 * @return whether equal
97 */
98 public final boolean equals(TransformedFunction<?> that) {
99 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 }