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 org.apache.commons.functor.UnaryPredicate;
21  
22  /**
23   * A {@link UnaryPredicate} that tests whether a {@link Comparable} object is
24   * within a range. The range is defined in the constructor.
25   *
26   * @since 1.0
27   * @version $Revision: 1171154 $ $Date: 2011-09-15 17:58:38 +0200 (Thu, 15 Sep 2011) $
28   * @author  Jason Horman (jason@jhorman.org)
29   */
30  
31  public class IsWithinRange<A extends Comparable<A>> implements UnaryPredicate<A>, Serializable {
32      /**
33       * serialVersionUID declaration.
34       */
35      private static final long serialVersionUID = -7584005207181667878L;
36  
37      /** Hashcode of the name of this Predicate. */
38      private static final int NAME_HASH_CODE = "IsWithinRange".hashCode();
39  
40      /** Base Hashcode. */
41      private static final int BASE_HASH_CODE = 29;
42  
43      /***************************************************
44       *  Instance variables
45       ***************************************************/
46  
47      /** The minimum value of the range. */
48      private final A min;
49      /** The maximum value of the range. */
50      private final A max;
51  
52      /***************************************************
53       *  Constructors
54       ***************************************************/
55  
56      /**
57       * Create a new IsWithinRange by passing in the range that will
58       * be used in the {@link #test}.
59       * @param min Comparable
60       * @param max Comparable
61       */
62      public IsWithinRange(A min, A max) {
63          if (min == null || max == null) {
64              throw new IllegalArgumentException("min and max must not be null");
65          }
66          if (min.compareTo(max) > 0) {
67              throw new IllegalArgumentException("min must be <= max");
68          }
69          this.min = min;
70          this.max = max;
71      }
72  
73      /***************************************************
74       *  Instance methods
75       ***************************************************/
76  
77      /**
78       * {@inheritDoc}
79       * Test if the passed in object is within the specified range.
80       */
81      public final boolean test(A o) {
82          return o.compareTo(min) >= 0 && o.compareTo(max) <= 0;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public final boolean equals(Object o) {
89          if (this == o) {
90              return true;
91          }
92          if (!(o instanceof IsWithinRange<?>)) {
93              return false;
94          }
95          final IsWithinRange<?> isWithinRange = (IsWithinRange<?>) o;
96          return max.equals(isWithinRange.max) && min.equals(isWithinRange.min);
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     public int hashCode() {
103         return BASE_HASH_CODE * min.hashCode() + max.hashCode() + NAME_HASH_CODE;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     public String toString() {
110         return "IsWithinRange(" + min + ", " + max + ")";
111     }
112 
113     /**
114      * Obtain an IsWithinRange instance.
115      * @param <A>
116      * @param min A
117      * @param max A
118      * @return IsWithinRange<A>
119      */
120     public static <A extends Comparable<A>> IsWithinRange<A> instance(A min, A max) {
121         return new IsWithinRange<A>(min, max);
122     }
123 }