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.collections4.functors;
18  
19  import static org.apache.commons.collections4.functors.ComparatorPredicate.comparatorPredicate;
20  
21  import java.util.Comparator;
22  
23  import org.apache.commons.collections4.Predicate;
24  import org.apache.commons.collections4.functors.ComparatorPredicate.Criterion;
25  import org.junit.jupiter.api.Test;
26  
27  public class ComparatorPredicateTest extends AbstractPredicateTest {
28      private static final class TestComparator<T extends Comparable<T>> implements Comparator<T> {
29          @Override
30          public int compare(final T first, final T second) {
31              return first.compareTo(second);
32          }
33      }
34  
35      @Override
36      protected Predicate<?> generatePredicate() {
37          return comparatorPredicate(Integer.valueOf(10), new TestComparator<>());
38      }
39  
40      @Test
41      public void testCompareEquals() {
42          final Integer value = Integer.valueOf(10);
43          final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<>());
44          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
45          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
46          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
47      }
48  
49      @Test
50      public void testCompareGreater() {
51          final Integer value = Integer.valueOf(10);
52          final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<>(), Criterion.GREATER);
53          assertPredicateTrue(p, Integer.valueOf(value.intValue() - 1));
54          assertPredicateFalse(p, Integer.valueOf(value.intValue()));
55          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
56      }
57  
58      @Test
59      public void testCompareGreaterOrEqual() {
60          final Integer value = Integer.valueOf(10);
61          final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<>(), Criterion.GREATER_OR_EQUAL);
62          assertPredicateTrue(p, Integer.valueOf(value.intValue() - 1));
63          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
64          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
65      }
66  
67      @Test
68      public void testCompareLess() {
69          final Integer value = Integer.valueOf(10);
70          final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<>(), Criterion.LESS);
71          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
72          assertPredicateFalse(p, Integer.valueOf(value.intValue()));
73          assertPredicateTrue(p, Integer.valueOf(value.intValue() + 1));
74      }
75  
76      @Test
77      public void testCompareLessOrEqual() {
78          final Integer value = Integer.valueOf(10);
79          final Predicate<Integer> p = comparatorPredicate(value, new TestComparator<>(), Criterion.LESS_OR_EQUAL);
80          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
81          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
82          assertPredicateTrue(p, Integer.valueOf(value.intValue() + 1));
83      }
84  }