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;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.UnaryFunction;
022    import org.apache.commons.functor.UnaryPredicate;
023    
024    /**
025     * {@link #evaluate Evaluates} to its input argument.
026     *
027     * {@link #test Tests} to the <code>boolean</code>
028     * value of the <code>Boolean</code>-valued parameter.
029     * The {@link #test} method throws an exception if
030     * the parameter isn't a non-<code>null</code>
031     * <code>Boolean</code>.
032     *
033     * @param <T> the returned value type.
034     * @version $Revision: 1160385 $ $Date: 2011-08-22 21:28:37 +0200 (Mon, 22 Aug 2011) $
035     * @author Rodney Waldhoff
036     */
037    public final class Identity<T> implements UnaryFunction<T, T>, UnaryPredicate<T>, Serializable {
038        // static attributes
039        // ------------------------------------------------------------------------
040        /**
041         * A generic {@code Identity<Object>} instance.
042         */
043        public static final Identity<Object> INSTANCE = new Identity<Object>();
044    
045        /**
046         * serialVersionUID declaration.
047         */
048        private static final long serialVersionUID = 4145504259013789494L;
049    
050        // constructor
051        // ------------------------------------------------------------------------
052    
053        /**
054         * Create a new Identity.
055         */
056        public Identity() {
057        }
058    
059        // function interface
060        // ------------------------------------------------------------------------
061    
062        /**
063         * {@inheritDoc}
064         */
065        public T evaluate(T obj) {
066            return obj;
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean test(Object obj) {
073            return test((Boolean) obj);
074        }
075    
076        /**
077         * Test a Boolean object by returning its <code>booleanValue</code>.
078         * @param bool Boolean
079         * @return boolean
080         */
081        public boolean test(Boolean bool) {
082            return bool.booleanValue();
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public boolean equals(Object that) {
089            return (that instanceof Identity<?>);
090        }
091    
092        /**
093         * {@inheritDoc}
094         */
095        public int hashCode() {
096            return "Identity".hashCode();
097        }
098    
099        /**
100         * {@inheritDoc}
101         */
102        public String toString() {
103            return "Identity";
104        }
105    
106        // static methods
107        // ------------------------------------------------------------------------
108    
109        /**
110         * Get an Identity instance.
111         * @param <T> the identity returned value type.
112         * @return Identity
113         */
114        public static <T> Identity<T> instance() {
115            return new Identity<T>();
116        }
117    
118    }