EqualPredicate.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.Objects;

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

  22. /**
  23.  * Predicate implementation that returns true if the input is the same object
  24.  * as the one stored in this predicate by equals.
  25.  *
  26.  * @param <T> the type of the input to the predicate.
  27.  * @since 3.0
  28.  */
  29. public final class EqualPredicate<T> extends AbstractPredicate<T> implements Serializable {

  30.     /** Serial version UID */
  31.     private static final long serialVersionUID = 5633766978029907089L;

  32.     /**
  33.      * Creates the predicate.
  34.      *
  35.      * @param <T> the type that the predicate queries
  36.      * @param object  the object to compare to
  37.      * @return the predicate
  38.      */
  39.     public static <T> Predicate<T> equalPredicate(final T object) {
  40.         if (object == null) {
  41.             return NullPredicate.nullPredicate();
  42.         }
  43.         return new EqualPredicate<>(object);
  44.     }

  45.     /**
  46.      * Creates the identity predicate.
  47.      *
  48.      * @param <T> the type that the predicate queries
  49.      * @param object  the object to compare to
  50.      * @param equator  the equator to use for comparison
  51.      * @return the predicate
  52.      * @since 4.0
  53.      */
  54.     public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) {
  55.         if (object == null) {
  56.             return NullPredicate.nullPredicate();
  57.         }
  58.         return new EqualPredicate<>(object, equator);
  59.     }

  60.     /** The value to compare to */
  61.     private final T test;

  62.     /** The equator to use for comparison */
  63.     private final Equator<T> equator;

  64.     /**
  65.      * Constructor that performs no validation.
  66.      * Use {@code equalPredicate} if you want that.
  67.      *
  68.      * @param object  the object to compare to
  69.      */
  70.     public EqualPredicate(final T object) {
  71.         // do not use the DefaultEquator to keep backwards compatibility
  72.         // the DefaultEquator returns also true if the two object references are equal
  73.         this(object, null);
  74.     }

  75.     /**
  76.      * Constructor that performs no validation.
  77.      * Use {@code equalPredicate} if you want that.
  78.      *
  79.      * @param test  the object to compare to
  80.      * @param equator  the equator to use for comparison
  81.      * @since 4.0
  82.      */
  83.     public EqualPredicate(final T test, final Equator<T> equator) {
  84.         this.test = test;
  85.         this.equator = equator;
  86.     }

  87.     /**
  88.      * Gets the value.
  89.      *
  90.      * @return the value
  91.      * @since 3.1
  92.      */
  93.     public Object getValue() {
  94.         return test;
  95.     }

  96.     /**
  97.      * Evaluates the predicate returning true if the input equals the stored value.
  98.      *
  99.      * @param object  the input object
  100.      * @return true if input object equals stored value
  101.      */
  102.     @Override
  103.     public boolean test(final T object) {
  104.         if (equator != null) {
  105.             return equator.equate(test, object);
  106.         }
  107.         return Objects.equals(test, object);
  108.     }

  109. }