View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.comparator;
18  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  
22  import org.apache.commons.functor.BinaryFunction;
23  import org.apache.commons.functor.UnaryFunction;
24  import org.apache.commons.functor.adapter.RightBoundFunction;
25  
26  /**
27   * Adapts a {@link Comparator Comparator} to the
28   * {@link BinaryFunction} interface.
29   *
30   * @version $Revision: 1166355 $ $Date: 2011-09-07 21:54:29 +0200 (Wed, 07 Sep 2011) $
31   * @author Rodney Waldhoff
32   */
33  public final class Min<T> implements BinaryFunction<T, T, T>, Serializable {
34  
35      /**
36       * Basic Min instance.
37       */
38      public static final Min<Comparable<?>> INSTANCE = Min.<Comparable<?>>instance();
39  
40      /**
41       * serialVersionUID declaration.
42       */
43      private static final long serialVersionUID = 9190170976707323848L;
44  
45      private final Comparator<T> comparator;
46  
47      /**
48       * Create a new Min.
49       */
50      @SuppressWarnings("unchecked")
51      public Min() {
52          this(ComparableComparator.instance());
53      }
54  
55      /**
56       * Create a new Min.
57       * @param comparator to use
58       */
59      public Min(Comparator<T> comparator) {
60          if (comparator == null) {
61              throw new IllegalArgumentException("Comparator argument must not be null");
62          }
63          this.comparator = comparator;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public T evaluate(T left, T right) {
70          return (comparator.compare(left, right) <= 0) ? left : right;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public boolean equals(Object that) {
77          return that == this || (that instanceof Min<?> && equals((Min<?>) that));
78      }
79  
80      /**
81       * Learn whether another Min is equal to this.
82       * @param that Min to test
83       * @return boolean
84       */
85      public boolean equals(Min<?> that) {
86          return null != that && comparator.equals(that.comparator);
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public int hashCode() {
93          return "Min".hashCode() ^ comparator.hashCode();
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public String toString() {
100         return "Min<" + comparator + ">";
101     }
102 
103     /**
104      * Get a basic Min instance.
105      * @return Min
106      */
107     public static <T extends Comparable<?>> Min<T> instance() {
108         return new Min<T>();
109     }
110 
111     /**
112      * Get a Min UnaryFunction.
113      * @param right the right side argument of the Min function
114      * @return UnaryFunction<T, T>
115      */
116     public static <T extends Comparable<?>> UnaryFunction<T, T> instance(T right) {
117         return RightBoundFunction.bind(new Min<T>(), right);
118     }
119 
120 }