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.adapter;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.functor.BinaryFunction;
022 import org.apache.commons.functor.Function;
023
024 /**
025 * Adapts a
026 * {@link BinaryFunction BinaryFunction}
027 * to the
028 * {@link Function Function} interface
029 * using constant arguments.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying objects are. Attempts to serialize
035 * an instance whose delegates are not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @param <T> the returned value type.
039 * @version $Revision: 1157607 $ $Date: 2011-08-14 21:32:47 +0200 (Sun, 14 Aug 2011) $
040 * @author Matt Benson
041 */
042 public final class FullyBoundFunction<T> implements Function<T>, Serializable {
043 /**
044 * serialVersionUID declaration.
045 */
046 private static final long serialVersionUID = -8588331452137525985L;
047 /** The {@link BinaryFunction BinaryFunction} I'm wrapping. */
048 private final BinaryFunction<Object, Object, ? extends T> function;
049 /** The left parameter to pass to {@code function}. */
050 private final Object left;
051 /** The right parameter to pass to {@code function}. */
052 private final Object right;
053
054 /**
055 * Create a new FullyBoundFunction.
056 * @param <L> the left argument type.
057 * @param <R> the right argument type.
058 * @param function the function to adapt
059 * @param left the left side argument to use
060 * @param right the right side argument to use
061 */
062 @SuppressWarnings("unchecked")
063 public <L, R> FullyBoundFunction(BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
064 if (function == null) {
065 throw new IllegalArgumentException("BinaryFunction argument was null");
066 }
067 this.function = (BinaryFunction<Object, Object, T>) function;
068 this.left = left;
069 this.right = right;
070 }
071
072 /**
073 * {@inheritDoc}
074 */
075 public T evaluate() {
076 return function.evaluate(left, right);
077 }
078
079 /**
080 * {@inheritDoc}
081 */
082 public boolean equals(Object that) {
083 return that == this || (that instanceof FullyBoundFunction<?> && equals((FullyBoundFunction<?>) that));
084 }
085
086 /**
087 * Learn whether another FullyBoundFunction is equal to this.
088 * @param that FullyBoundFunction to test
089 * @return boolean
090 */
091 public boolean equals(FullyBoundFunction<?> that) {
092 return null != that && (null == function ? null == that.function : function.equals(that.function))
093 && (null == left ? null == that.left : left.equals(that.left))
094 && (null == right ? null == that.right : right.equals(that.right));
095 }
096
097 /**
098 * {@inheritDoc}
099 */
100 public int hashCode() {
101 int hash = "FullyBoundFunction".hashCode();
102 if (null != function) {
103 hash <<= 2;
104 hash ^= function.hashCode();
105 }
106 hash <<= 2;
107 if (null != left) {
108 hash ^= left.hashCode();
109 }
110 hash <<= 2;
111 if (null != right) {
112 hash ^= right.hashCode();
113 }
114 return hash;
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 public String toString() {
121 return "FullyBoundFunction<" + function + "(" + left + ", " + right + ")>";
122 }
123
124 /**
125 * Adapt a BinaryFunction as a Function.
126 * @param <L> left type
127 * @param <R> right type
128 * @param <T> result type
129 * @param function to adapt
130 * @param left left side argument
131 * @param right right side argument
132 * @return FullyBoundFunction
133 */
134 public static <L, R, T> FullyBoundFunction<T> bind(
135 BinaryFunction<? super L, ? super R, ? extends T> function, L left, R right) {
136 return null == function ? null : new FullyBoundFunction<T>(function, left, right);
137 }
138
139 }