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.Function;
022    import org.apache.commons.functor.UnaryFunction;
023    
024    /**
025     * Adapts a
026     * {@link UnaryFunction UnaryFunction}
027     * to the
028     * {@link Function Function} interface
029     * using a constant unary argument.
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: 1157604 $ $Date: 2011-08-14 21:29:42 +0200 (Sun, 14 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class BoundFunction<T> implements Function<T>, Serializable {
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = 8873081237760986490L;
047        /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
048        private final UnaryFunction<Object, ? extends T> function;
049        /** The argument to pass to {@code function}. */
050        private final Object arg;
051    
052        /**
053         * Create a new BoundFunction instance.
054         * @param <A> the argument value type
055         * @param function the function to adapt
056         * @param arg the constant argument to use
057         */
058        @SuppressWarnings("unchecked")
059        public <A> BoundFunction(UnaryFunction<? super A, ? extends T> function, A arg) {
060            if (function == null) {
061                throw new IllegalArgumentException("UnaryFunction argument was null");
062            }
063            this.function = (UnaryFunction<Object, ? extends T>) function;
064            this.arg = arg;
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public T evaluate() {
071            return function.evaluate(arg);
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        public boolean equals(Object that) {
078            return that == this || (that instanceof BoundFunction<?> && equals((BoundFunction<?>) that));
079        }
080    
081        /**
082         * Learn whether another BoundFunction is equal to this.
083         * @param that BoundFunction to test
084         * @return boolean
085         */
086        public boolean equals(BoundFunction<?> that) {
087            if (that == null) {
088                return false;
089            }
090            if (!(that.function.equals(this.function))) {
091                return false;
092            }
093            return that.arg == this.arg || that.arg != null && that.arg.equals(this.arg);
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public int hashCode() {
100            int result = "BoundFunction".hashCode();
101            result <<= 2;
102            result |= function.hashCode();
103            result <<= 2;
104            return arg == null ? result : result | arg.hashCode();
105        }
106    
107        /**
108         * {@inheritDoc}
109         */
110        public String toString() {
111            return "BoundFunction<" + function.toString() + "(" + arg + ")>";
112        }
113    
114        /**
115         * Adapt the given, possibly-<code>null</code>,
116         * {@link UnaryFunction UnaryFunction} to the
117         * {@link Function Function} interface by binding
118         * the specified <code>Object</code> as a constant
119         * argument.
120         * When the given <code>UnaryFunction</code> is <code>null</code>,
121         * returns <code>null</code>.
122         * @param <A> input type
123         * @param <T> result type
124         * @param function the possibly-<code>null</code>
125         *        {@link UnaryFunction UnaryFunction} to adapt
126         * @param arg the object to bind as a constant argument
127         * @return a <code>BoundFunction</code> wrapping the given
128         *         {@link UnaryFunction UnaryFunction}, or <code>null</code>
129         *         if the given <code>UnaryFunction</code> is <code>null</code>
130         */
131        public static <A, T> BoundFunction<T> bind(UnaryFunction<? super A, ? extends T> function, A arg) {
132            return null == function ? null : new BoundFunction<T>(function, arg);
133        }
134    
135    }