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.comparator;
018    
019    import java.io.Serializable;
020    import java.util.Comparator;
021    
022    import org.apache.commons.functor.BinaryFunction;
023    
024    /**
025     * Adapts a {@link Comparator Comparator} to the
026     * {@link BinaryFunction} interface.
027     *
028     * @version $Revision: 1156349 $ $Date: 2011-08-10 22:21:14 +0200 (Wed, 10 Aug 2011) $
029     * @author Rodney Waldhoff
030     */
031    public final class ComparatorFunction<T> implements BinaryFunction<T, T, Integer>, Serializable {
032    
033        /**
034         * Basic ComparatorFunction instance.
035         */
036        public static final ComparatorFunction<Comparable<?>> INSTANCE = ComparatorFunction.<Comparable<?>>instance();
037    
038        /**
039         * serialVersionUID declaration.
040         */
041        private static final long serialVersionUID = 1642024869929206095L;
042    
043        private final Comparator<? super T> comparator;
044    
045        /**
046         * Create a new ComparatorFunction.
047         * @param comparator to wrap
048         */
049        public ComparatorFunction(Comparator<? super T> comparator) {
050            if (comparator == null) {
051                throw new IllegalArgumentException("Comparator must not be null");
052            }
053            this.comparator = comparator;
054        }
055    
056        /**
057         * {@inheritDoc}
058         */
059        public Integer evaluate(T left, T right) {
060            return Integer.valueOf(comparator.compare(left, right));
061        }
062    
063        /**
064         * {@inheritDoc}
065         */
066        public boolean equals(Object that) {
067            return that == this || (that instanceof ComparatorFunction<?> && equals((ComparatorFunction<?>) that));
068        }
069    
070        /**
071         * Learn whether a specified ComparatorFunction is equal to this.
072         * @param that the ComparatorFunction to test
073         * @return boolean
074         */
075        public boolean equals(ComparatorFunction<?> that) {
076            return null != that && comparator.equals(that.comparator);
077        }
078    
079        /**
080         * {@inheritDoc}
081         */
082        public int hashCode() {
083            return "ComparatorFunction".hashCode() ^ comparator.hashCode();
084        }
085    
086        /**
087         * {@inheritDoc}
088         */
089        public String toString() {
090            return "ComparatorFunction<" + comparator + ">";
091        }
092    
093        /**
094         * Get a basic ComparatorFunction instance.
095         * @return ComparatorFunction
096         */
097        @SuppressWarnings("unchecked")
098        public static <T> ComparatorFunction<T> instance() {
099            return new ComparatorFunction<T>(ComparableComparator.INSTANCE);
100        }
101    }