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.BinaryPredicate;
023    import org.apache.commons.functor.UnaryPredicate;
024    import org.apache.commons.functor.adapter.RightBoundPredicate;
025    
026    /**
027     * A {@link BinaryPredicate BinaryPredicate} that {@link #test tests}
028     * <code>true</code> iff the left argument is greater than the
029     * right argument under the specified {@link Comparator}.
030     * When no (or a <code>null</code> <code>Comparator</code> is specified,
031     * a {@link Comparable Comparable} <code>Comparator</code> is used.
032     *
033     * @version $Revision: 1166341 $ $Date: 2011-09-07 21:45:19 +0200 (Wed, 07 Sep 2011) $
034     * @author Rodney Waldhoff
035     */
036    public final class IsGreaterThan<T> implements BinaryPredicate<T, T>, Serializable {
037    
038        /**
039         * Basic IsGreaterThan instance.
040         */
041        public static final IsGreaterThan<Comparable<?>> INSTANCE = IsGreaterThan.<Comparable<?>>instance();
042    
043        /**
044         * serialVersionUID declaration.
045         */
046        private static final long serialVersionUID = -8592932496891910273L;
047    
048        private final Comparator<? super T> comparator;
049    
050        /**
051         * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
052         * for {@link Comparable Comparable}s.
053         */
054        @SuppressWarnings("unchecked")
055        public IsGreaterThan() {
056            this(ComparableComparator.INSTANCE);
057        }
058    
059        /**
060         * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
061         * for the given {@link Comparator Comparator}.
062         *
063         * @param comparator the {@link Comparator Comparator}, when <code>null</code>,
064         *        a <code>Comparator</code> for {@link Comparable Comparable}s will
065         *        be used.
066         */
067        public IsGreaterThan(Comparator<? super T> comparator) {
068            if (comparator == null) {
069                throw new IllegalArgumentException("Comparator argument must not be null");
070            }
071            this.comparator = comparator;
072        }
073    
074        /**
075         * Return <code>true</code> iff the <i>left</i> parameter is
076         * greater than the <i>right</i> parameter under my current
077         * {@link Comparator Comparator}.
078         * {@inheritDoc}
079         */
080        public boolean test(T left, T right) {
081            return comparator.compare(left, right) > 0;
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public boolean equals(Object that) {
088            return that == this || (that instanceof IsGreaterThan<?> && equals((IsGreaterThan<?>) that));
089        }
090    
091        /**
092         * Learn whether a given IsGreaterThan is equal to this.
093         * @param that the IsGreaterThan to test
094         * @return boolean
095         */
096        public boolean equals(IsGreaterThan<?> that) {
097            if (null != that) {
098                if (null == comparator) {
099                    return null == that.comparator;
100                }
101                return comparator.equals(that.comparator);
102            }
103            return false;
104        }
105    
106        /**
107         * {@inheritDoc}
108         */
109        public int hashCode() {
110            int hash = "IsGreaterThan".hashCode();
111            // by construction, comparator is never null
112            hash ^= comparator.hashCode();
113            return hash;
114        }
115    
116        /**
117         * {@inheritDoc}
118         */
119        public String toString() {
120            return "IsGreaterThan<" + comparator + ">";
121        }
122    
123        /**
124         * Get a typed IsGreaterThan instance.
125         * @param <T>
126         * @return IsGreaterThan<T>
127         */
128        public static <T extends Comparable<?>> IsGreaterThan<T> instance() {
129            return new IsGreaterThan<T>();
130        }
131    
132        /**
133         * Get an IsGreaterThan UnaryPredicate.
134         * @param right the right side object of the IsGreaterThan comparison
135         * @return UnaryPredicate<T>
136         */
137        public static <T extends Comparable<?>> UnaryPredicate<T> instance(T right) {
138            return RightBoundPredicate.bind(new IsGreaterThan<T>(), right);
139        }
140    
141    }