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 */
017package org.apache.commons.functor.core.comparator;
018
019import java.util.Comparator;
020
021import org.apache.commons.functor.BinaryPredicate;
022import org.apache.commons.functor.Predicate;
023import org.apache.commons.functor.adapter.RightBoundPredicate;
024import org.apache.commons.lang3.Validate;
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 * @param <T> the binary predicate input types
034 * @version $Revision: 1537901 $ $Date: 2013-11-01 12:30:19 +0100 (Fr, 01 Nov 2013) $
035 */
036public final class IsGreaterThan<T> implements BinaryPredicate<T, T> {
037
038    /**
039     * Basic IsGreaterThan instance.
040     */
041    public static final IsGreaterThan<Comparable<?>> INSTANCE = IsGreaterThan.<Comparable<?>>instance();
042
043    /**
044     * The wrapped comparator.
045     */
046    private final Comparator<? super T> comparator;
047
048    /**
049     * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
050     * for {@link Comparable Comparable}s.
051     */
052    @SuppressWarnings("unchecked")
053    public IsGreaterThan() {
054        this((Comparator<? super T>) ComparableComparator.INSTANCE);
055    }
056
057    /**
058     * Construct a <code>IsGreaterThan</code> {@link BinaryPredicate predicate}
059     * for the given {@link Comparator Comparator}.
060     *
061     * @param comparator the {@link Comparator Comparator}, when <code>null</code>,
062     *        a <code>Comparator</code> for {@link Comparable Comparable}s will
063     *        be used.
064     */
065    public IsGreaterThan(Comparator<? super T> comparator) {
066        this.comparator = Validate.notNull(comparator, "Comparator argument must not be null");
067    }
068
069    /**
070     * Return <code>true</code> iff the <i>left</i> parameter is
071     * greater than the <i>right</i> parameter under my current
072     * {@link Comparator Comparator}.
073     * {@inheritDoc}
074     */
075    public boolean test(T left, T right) {
076        return comparator.compare(left, right) > 0;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public boolean equals(Object obj) {
084        if (obj == this) {
085            return true;
086        }
087        if (!(obj instanceof IsGreaterThan<?>)) {
088            return false;
089        }
090        IsGreaterThan<?> that = (IsGreaterThan<?>) obj;
091        return this.comparator.equals(that.comparator);
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public int hashCode() {
099        int hash = "IsGreaterThan".hashCode();
100        // by construction, comparator is never null
101        hash ^= comparator.hashCode();
102        return hash;
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public String toString() {
110        return "IsGreaterThan<" + comparator + ">";
111    }
112
113    /**
114     * Get a typed IsGreaterThan instance.
115     *
116     * @param <T> the binary predicate input types
117     * @return IsGreaterThan<T>
118     */
119    public static <T extends Comparable<?>> IsGreaterThan<T> instance() {
120        return new IsGreaterThan<T>();
121    }
122
123    /**
124     * Get an IsGreaterThan Predicate.
125     *
126     * @param <T> the binary predicate input types
127     * @param right the right side object of the IsGreaterThan comparison
128     * @return Predicate<T>
129     */
130    public static <T extends Comparable<?>> Predicate<T> instance(T right) {
131        return RightBoundPredicate.bind(new IsGreaterThan<T>(), right);
132    }
133
134}