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 Function Function}
027     * to the
028     * {@link UnaryFunction UnaryFunction} interface
029     * by ignoring the 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 functor is.  Attempts to serialize
035     * an instance whose delegate is not
036     * <code>Serializable</code> will result in an exception.
037     *
038     * @param <A> the argument type.
039     * @param <T> the returned value type.
040     * @version $Revision: 1157609 $ $Date: 2011-08-14 21:42:21 +0200 (Sun, 14 Aug 2011) $
041     * @author Rodney Waldhoff
042     */
043    public final class FunctionUnaryFunction<A, T> implements UnaryFunction<A, T>, Serializable {
044        /**
045         * serialVersionUID declaration.
046         */
047        private static final long serialVersionUID = 1993899041200996524L;
048        /** The {@link Function Function} I'm wrapping. */
049        private final Function<? extends T> function;
050    
051        /**
052         * Create a new FunctionUnaryFunction.
053         * @param function to adapt
054         */
055        public FunctionUnaryFunction(Function<? extends T> function) {
056            if (function == null) {
057                throw new IllegalArgumentException("Function argument was null");
058            }
059            this.function = function;
060        }
061    
062        /**
063         * {@inheritDoc}
064         */
065        public T evaluate(A obj) {
066            return function.evaluate();
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean equals(Object that) {
073            return that == this || (that instanceof FunctionUnaryFunction<?, ?>
074                    && equals((FunctionUnaryFunction<?, ?>) that));
075        }
076    
077        /**
078         * Learn whether another FunctionUnaryFunction is equal to this.
079         * @param that FunctionUnaryFunction to test
080         * @return boolean
081         */
082        public boolean equals(FunctionUnaryFunction<?, ?> that) {
083            return null != that && (null == function ? null == that.function : function.equals(that.function));
084        }
085    
086        /**
087         * {@inheritDoc}
088         */
089        public int hashCode() {
090            int hash = "FunctionUnaryFunction".hashCode();
091            if (null != function) {
092                hash ^= function.hashCode();
093            }
094            return hash;
095        }
096    
097        /**
098         * {@inheritDoc}
099         */
100        public String toString() {
101            return "FunctionUnaryFunction<" + function + ">";
102        }
103    
104        /**
105         * Adapt a Function to the UnaryFunction interface.
106         * @param <A> arg type
107         * @param <T> result type
108         * @param function to adapt
109         * @return FunctionUnaryFunction
110         */
111        public static <A, T> FunctionUnaryFunction<A, T> adapt(Function<? extends T> function) {
112            return null == function ? null : new FunctionUnaryFunction<A, T>(function);
113        }
114    
115    }