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