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.io.Serializable;
020import java.util.Comparator;
021
022import org.apache.commons.functor.BinaryFunction;
023import org.apache.commons.functor.UnaryFunction;
024import org.apache.commons.functor.adapter.RightBoundFunction;
025import org.apache.commons.lang3.Validate;
026
027/**
028 * Adapts a {@link Comparator Comparator} to the
029 * {@link BinaryFunction} interface.
030 *
031 * @param <T> the binary function arguments and return types.
032 * @version $Revision: 1364676 $ $Date: 2012-07-23 11:21:25 -0400 (Mon, 23 Jul 2012) $
033 */
034public final class Min<T> implements BinaryFunction<T, T, T>, Serializable {
035
036    /**
037     * Basic Min instance.
038     */
039    public static final Min<Comparable<?>> INSTANCE = Min.<Comparable<?>>instance();
040
041    /**
042     * serialVersionUID declaration.
043     */
044    private static final long serialVersionUID = -3538911698805767997L;
045
046    /**
047     * The wrapped comparator.
048     */
049    private final Comparator<T> comparator;
050
051    /**
052     * Create a new Min.
053     */
054    @SuppressWarnings("unchecked")
055    public Min() {
056        this((Comparator<T>) ComparableComparator.INSTANCE);
057    }
058
059    /**
060     * Create a new Min.
061     * @param comparator to use
062     */
063    public Min(Comparator<T> comparator) {
064        this.comparator = Validate.notNull(comparator, "Comparator argument must not be null");
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    public T evaluate(T left, T right) {
071        return (comparator.compare(left, right) <= 0) ? left : right;
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public boolean equals(Object that) {
079        return that == this || (that instanceof Min<?> && equals((Min<?>) that));
080    }
081
082    /**
083     * Learn whether another Min is equal to this.
084     * @param that Min to test
085     * @return boolean
086     */
087    public boolean equals(Min<?> that) {
088        return null != that && comparator.equals(that.comparator);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public int hashCode() {
096        return "Min".hashCode() ^ comparator.hashCode();
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public String toString() {
104        return "Min<" + comparator + ">";
105    }
106
107    /**
108     * Get a basic Min instance.
109     *
110     * @param <T> the binary function arguments and return types.
111     * @return Min
112     */
113    public static <T extends Comparable<?>> Min<T> instance() {
114        return new Min<T>();
115    }
116
117    /**
118     * Get a Min UnaryFunction.
119     *
120     * @param <T> the binary function arguments and return types.
121     * @param right the right side argument of the Min function
122     * @return UnaryFunction<T, T>
123     */
124    public static <T extends Comparable<?>> UnaryFunction<T, T> instance(T right) {
125        return RightBoundFunction.bind(new Min<T>(), right);
126    }
127
128}