ComparatorPredicate.java

  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. import java.io.Serializable;
  19. import java.util.Comparator;
  20. import java.util.Objects;

  21. import org.apache.commons.collections4.Predicate;

  22. /**
  23.  * Predicate that compares the input object with the one stored in the predicate using a comparator.
  24.  * In addition, the comparator result can be evaluated in accordance to a supplied criterion value.
  25.  *
  26.  * <p>In order to demonstrate the use of the predicate, the following variables are declared:</p>
  27.  *
  28.  * <pre>
  29.  * Integer ONE = Integer.valueOf(1);
  30.  * Integer TWO = Integer.valueOf(2);
  31.  *
  32.  * Comparator comparator = new Comparator() {
  33.  *
  34.  *     public int compare(Object first, Object second) {
  35.  *         return ((Integer) second) - ((Integer) first);
  36.  *     }
  37.  *
  38.  * };
  39.  * </pre>
  40.  *
  41.  * <p>Using the declared variables, the {@code ComparatorPredicate} can be used in the
  42.  * following way:</p>
  43.  *
  44.  * <pre>
  45.  * ComparatorPredicate.comparatorPredicate(ONE, comparator).test(TWO);
  46.  * </pre>
  47.  *
  48.  * <p>The input variable {@code TWO} in compared to the stored variable {@code ONE} using
  49.  * the supplied {@code comparator}. This is the default usage of the predicate and will return
  50.  * {@code true} if the underlying comparator returns {@code 0}. In addition to the default
  51.  * usage of the predicate, it is possible to evaluate the comparator's result in several ways. The
  52.  * following {@link Criterion} enumeration values are provided by the predicate:
  53.  * </p>
  54.  *
  55.  * <ul>
  56.  *     <li>EQUAL</li>
  57.  *     <li>GREATER</li>
  58.  *     <li>GREATER_OR_EQUAL</li>
  59.  *     <li>LESS</li>
  60.  *     <li>LESS_OR_EQUAL</li>
  61.  * </ul>
  62.  *
  63.  * <p>The following examples demonstrates how these constants can be used in order to manipulate the
  64.  * evaluation of a comparator result.</p>
  65.  *
  66.  * <pre>
  67.  * ComparatorPredicate.comparatorPredicate(ONE, comparator,<strong>ComparatorPredicate.Criterion.GREATER</strong>).test(TWO);
  68.  * </pre>
  69.  *
  70.  * <p>The input variable TWO is compared to the stored variable ONE using the supplied {@code comparator}
  71.  * using the {@code GREATER} evaluation criterion constant. This instructs the predicate to
  72.  * return {@code true} if the comparator returns a value greater than {@code 0}.</p>
  73.  *
  74.  * @param <T> the type of the input to the predicate.
  75.  * @since 4.0
  76.  */
  77. public class ComparatorPredicate<T> extends AbstractPredicate<T> implements Serializable {

  78.     /**
  79.      * Enumerates the comparator criteria.
  80.      */
  81.     public enum Criterion {

  82.         /**
  83.          * Equal criterion.
  84.          */
  85.         EQUAL,

  86.         /**
  87.          * Greater criterion.
  88.          */
  89.         GREATER,

  90.         /**
  91.          * Less criterion.
  92.          */
  93.         LESS,

  94.         /**
  95.          * Greater or equal criterion.
  96.          */
  97.         GREATER_OR_EQUAL,

  98.         /**
  99.          * Less or equal Criterion.
  100.          */
  101.         LESS_OR_EQUAL,
  102.     }

  103.     private static final long serialVersionUID = -1863209236504077399L;

  104.     /**
  105.      * Creates the comparator predicate
  106.      *
  107.      * @param <T> the type that the predicate queries
  108.      * @param object  the object to compare to
  109.      * @param comparator  the comparator to use for comparison
  110.      * @return the predicate
  111.      * @throws NullPointerException if comparator is null
  112.      */
  113.     public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator) {
  114.         return comparatorPredicate(object, comparator, Criterion.EQUAL);
  115.     }

  116.     /**
  117.      * Creates the comparator predicate
  118.      *
  119.      * @param <T> the type that the predicate queries
  120.      * @param object  the object to compare to
  121.      * @param comparator  the comparator to use for comparison
  122.      * @param criterion  the criterion to use to evaluate comparison
  123.      * @return the predicate
  124.      * @throws NullPointerException if comparator or criterion is null
  125.      */
  126.     public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator,
  127.                                                        final Criterion criterion) {
  128.         return new ComparatorPredicate<>(object, Objects.requireNonNull(comparator, "comparator"),
  129.                 Objects.requireNonNull(criterion, "criterion"));
  130.     }

  131.     /** The internal object to compare with */
  132.     private final T object;

  133.     /** The comparator to use for comparison */
  134.     private final Comparator<T> comparator;

  135.     /** The comparison evaluation criterion to use */
  136.     private final Criterion criterion;

  137.     /**
  138.      * Constructor that performs no validation.
  139.      * Use {@code comparatorPredicate} if you want that.
  140.      *
  141.      * @param object  the object to compare to
  142.      * @param comparator  the comparator to use for comparison
  143.      * @param criterion  the criterion to use to evaluate comparison
  144.      */
  145.     public ComparatorPredicate(final T object, final Comparator<T> comparator, final Criterion criterion) {
  146.         this.object = object;
  147.         this.comparator = comparator;
  148.         this.criterion = criterion;
  149.     }

  150.     /**
  151.      * Evaluates the predicate. The predicate evaluates to {@code true} in the following cases:
  152.      *
  153.      * <ul>
  154.      * <li>{@code comparator.compare(object, input) == 0 &amp;&amp; criterion == EQUAL}</li>
  155.      * <li>{@code comparator.compare(object, input) &lt; 0 &amp;&amp; criterion == LESS}</li>
  156.      * <li>{@code comparator.compare(object, input) &gt; 0 &amp;&amp; criterion == GREATER}</li>
  157.      * <li>{@code comparator.compare(object, input) &gt;= 0 &amp;&amp; criterion == GREATER_OR_EQUAL}</li>
  158.      * <li>{@code comparator.compare(object, input) &lt;= 0 &amp;&amp; criterion == LESS_OR_EQUAL}</li>
  159.      * </ul>
  160.      *
  161.      * @see org.apache.commons.collections4.Predicate#test(Object)
  162.      * @see java.util.Comparator#compare(Object first, Object second)
  163.      * @param target  the target object to compare to
  164.      * @return {@code true} if the comparison succeeds according to the selected criterion
  165.      * @throws IllegalStateException if the criterion is invalid (really not possible)
  166.      */
  167.     @Override
  168.     public boolean test(final T target) {

  169.         boolean result = false;
  170.         final int comparison = comparator.compare(object, target);
  171.         switch (criterion) {
  172.         case EQUAL:
  173.             result = comparison == 0;
  174.             break;
  175.         case GREATER:
  176.             result = comparison > 0;
  177.             break;
  178.         case LESS:
  179.             result = comparison < 0;
  180.             break;
  181.         case GREATER_OR_EQUAL:
  182.             result = comparison >= 0;
  183.             break;
  184.         case LESS_OR_EQUAL:
  185.             result = comparison <= 0;
  186.             break;
  187.         default:
  188.             throw new IllegalStateException("The current criterion '" + criterion + "' is invalid.");
  189.         }

  190.         return result;
  191.     }
  192. }