BeanPropertyValueEqualsPredicate.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.beanutils2;

  18. import java.lang.reflect.InvocationTargetException;
  19. import java.util.Objects;
  20. import java.util.function.Predicate;

  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;

  23. /**
  24.  * <p>
  25.  * {@code Predicate} that evaluates a property value against a specified value.
  26.  * </p>
  27.  * <p>
  28.  * An implementation of {@link java.util.function.Predicate} that evaluates a property value on the object provided against a specified value and returns
  29.  * {@code true} if equal; {@code false} otherwise. The {@code BeanPropertyValueEqualsPredicate} constructor takes two parameters which determine what property
  30.  * will be evaluated on the target object and what its expected value should be.
  31.  * </p>
  32.  * <dl>
  33.  * <dt><strong> {@code public BeanPropertyValueEqualsPredicate( String propertyName, Object propertyValue )} </strong></dt>
  34.  * <dd>Will create a {@code Predicate} that will evaluate the target object and return
  35.  * {@code true</code> if the property specified by <code>propertyName} has a value which
  36.  *       is equal to the value specified by {@code propertyValue}. Or return
  37.  *       {@code false} otherwise.
  38.  *    </dd>
  39.  * </dl>
  40.  * <p>
  41.  * <strong>Note:</strong> Property names can be a simple, nested, indexed, or mapped property as defined by
  42.  * {@code org.apache.commons.beanutils2.PropertyUtils}.  If any object in the property path
  43.  * specified by {@code propertyName</code> is <code>null} then the outcome is based on the
  44.  * value of the {@code ignoreNull} attribute.
  45.  * </p>
  46.  * <p>
  47.  * A typical usage might look like:
  48.  * </p>
  49.  * <pre>{@code
  50.  * // create the closure
  51.  * BeanPropertyValueEqualsPredicate predicate =
  52.  *    new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE );
  53.  *
  54.  * // filter the Collection
  55.  * CollectionUtils.filter( peopleCollection, predicate );
  56.  * }</pre>
  57.  * <p>
  58.  * This would take a {@code Collection} of person objects and filter out any people whose
  59.  * {@code activeEmployee</code> property is <code>false}. Assuming...
  60.  * </p>
  61.  * <ul>
  62.  *    <li>
  63.  *       The top level object in the {@code peopleCollection} is an object which represents a
  64.  *       person.
  65.  *    </li>
  66.  *    <li>
  67.  *       The person object has a {@code getActiveEmployee()} method which returns
  68.  *       the boolean value for the object's {@code activeEmployee} property.
  69.  *    </li>
  70.  * </ul>
  71.  * <p>
  72.  * Another typical usage might look like:
  73.  * </p>
  74.  * <pre>{@code
  75.  * // create the closure
  76.  * BeanPropertyValueEqualsPredicate predicate =
  77.  *    new BeanPropertyValueEqualsPredicate( "personId", "456-12-1234" );
  78.  *
  79.  * // search the Collection
  80.  * CollectionUtils.find( peopleCollection, predicate );
  81.  * }</pre>
  82.  * <p>
  83.  * This would search a {@code Collection} of person objects and return the first object whose
  84.  * {@code personId</code> property value equals <code>456-12-1234}. Assuming...
  85.  * </p>
  86.  * <ul>
  87.  * <li>The top level object in the {@code peopleCollection} is an object which represents a person.</li>
  88.  * <li>The person object has a {@code getPersonId()} method which returns the value for the object's {@code personId} property.</li>
  89.  * </ul>
  90.  *
  91.  * @param <T> The type of the input to the predicate.
  92.  * @param <V> The property value type.
  93.  * @see org.apache.commons.beanutils2.PropertyUtils
  94.  * @see java.util.function.Predicate
  95.  */
  96. public class BeanPropertyValueEqualsPredicate<T, V> implements Predicate<T> {

  97.     /** For logging. Each subclass gets its own log instance. */
  98.     private final Log log = LogFactory.getLog(this.getClass());

  99.     /**
  100.      * The name of the property which will be evaluated when this {@code Predicate} is executed.
  101.      */
  102.     private final String propertyName;

  103.     /**
  104.      * The value that the property specified by {@code propertyName} will be compared to when this {@code Predicate} executes.
  105.      */
  106.     private final V propertyValue;

  107.     /**
  108.      * <p>
  109.      * Should {@code null} objects in the property path be ignored?
  110.      * </p>
  111.      * <p>
  112.      * Determines whether {@code null} objects in the property path will generate an
  113.      * {@code IllegalArgumentException</code> or not. If set to <code>true} then if any objects
  114.      * in the property path evaluate to {@code null} then the
  115.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged but
  116.      * not re-thrown and {@code false</code> will be returned.  If set to <code>false} then if
  117.      * any objects in the property path evaluate to {@code null} then the
  118.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged and re-thrown.
  119.      * </p>
  120.      */
  121.     private final boolean ignoreNull;

  122.     /**
  123.      * Constructor which takes the name of the property, its expected value to be used in evaluation, and assumes {@code ignoreNull</code> to be <code>false}.
  124.      *
  125.      * @param propertyName  The name of the property that will be evaluated against the expected value.
  126.      * @param propertyValue The value to use in object evaluation.
  127.      * @throws IllegalArgumentException If the property name provided is null or empty.
  128.      */
  129.     public BeanPropertyValueEqualsPredicate(final String propertyName, final V propertyValue) {
  130.         this(propertyName, propertyValue, false);
  131.     }

  132.     /**
  133.      * Constructor which takes the name of the property, its expected value to be used in evaluation, and a boolean which determines whether {@code null}
  134.      * objects in the property path will generate an {@code IllegalArgumentException} or not.
  135.      *
  136.      * @param propertyName  The name of the property that will be evaluated against the expected value.
  137.      * @param propertyValue The value to use in object evaluation.
  138.      * @param ignoreNull    Determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
  139.      * @throws IllegalArgumentException If the property name provided is null or empty.
  140.      */
  141.     public BeanPropertyValueEqualsPredicate(final String propertyName, final V propertyValue, final boolean ignoreNull) {
  142.         if (propertyName == null || propertyName.isEmpty()) {
  143.             throw new IllegalArgumentException("propertyName cannot be null or empty");
  144.         }
  145.         this.propertyName = propertyName;
  146.         this.propertyValue = propertyValue;
  147.         this.ignoreNull = ignoreNull;
  148.     }

  149.     /**
  150.      * Utility method which evaluates whether the actual property value equals the expected property value.
  151.      *
  152.      * @param expected The expected value.
  153.      * @param actual   The actual value.
  154.      * @return True if they are equal; false otherwise.
  155.      */
  156.     protected boolean evaluateValue(final V expected, final Object actual) {
  157.         return Objects.equals(expected, actual);
  158.     }

  159.     /**
  160.      * Returns the name of the property which will be evaluated when this {@code Predicate} is executed.
  161.      *
  162.      * @return The name of the property which will be evaluated when this {@code Predicate} is executed.
  163.      */
  164.     public String getPropertyName() {
  165.         return propertyName;
  166.     }

  167.     /**
  168.      * Returns the value that the property specified by {@code propertyName} will be compared to when this {@code Predicate} executes.
  169.      *
  170.      * @return The value that the property specified by {@code propertyName} will be compared to when this {@code Predicate} executes.
  171.      */
  172.     public V getPropertyValue() {
  173.         return propertyValue;
  174.     }

  175.     /**
  176.      * Returns the flag which determines whether {@code null} objects in the property path will generate an
  177.      * {@code IllegalArgumentException</code> or not. If set to <code>true} then
  178.      * if any objects in the property path evaluate to {@code null} then the
  179.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged but
  180.      * not re-thrown and {@code false</code> will be returned.  If set to <code>false} then if
  181.      * any objects in the property path evaluate to {@code null} then the
  182.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged and re-thrown.
  183.      *
  184.      * @return The flag which determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
  185.      */
  186.     public boolean isIgnoreNull() {
  187.         return ignoreNull;
  188.     }

  189.     /**
  190.      * Evaluates the object provided against the criteria specified when this {@code BeanPropertyValueEqualsPredicate} was constructed. Equality is based on
  191.      * either reference or logical equality as defined by the property object's equals method. If any object in the property path leading up to the target
  192.      * property is {@code null} then the outcome will be based on the value of the {@code ignoreNull} attribute. By default,
  193.      * {@code ignoreNull</code> is <code>false} and would result in an
  194.      * {@code IllegalArgumentException} if an object in the property path leading up to the
  195.      * target property is {@code null}.
  196.      *
  197.      * &#64;param object The object to be evaluated.
  198.      * &#64;return True if the object provided meets all the criteria for this {@code Predicate};
  199.      * false otherwise.
  200.      * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
  201.      * NoSuchMethodException is thrown when trying to access the property specified on the object
  202.      * provided. Or if an object in the property path provided is {@code null} and
  203.      * {@code ignoreNull</code> is set to <code>false}.
  204.      */
  205.     @Override
  206.     public boolean test(final T object) {

  207.         boolean evaluation = false;

  208.         try {
  209.             evaluation = evaluateValue(propertyValue, PropertyUtils.getProperty(object, propertyName));
  210.         } catch (final IllegalArgumentException e) {
  211.             final String errorMsg = "Problem during evaluation. Null value encountered in property path...";

  212.             if (!ignoreNull) {
  213.                 throw new IllegalArgumentException(errorMsg, e);
  214.             }
  215.             log.warn(errorMsg, e);
  216.         } catch (final IllegalAccessException e) {
  217.             final String errorMsg = "Unable to access the property provided.";
  218.             throw new IllegalArgumentException(errorMsg, e);
  219.         } catch (final InvocationTargetException e) {
  220.             final String errorMsg = "Exception occurred in property's getter";
  221.             throw new IllegalArgumentException(errorMsg, e);
  222.         } catch (final NoSuchMethodException e) {
  223.             final String errorMsg = "Property not found.";
  224.             throw new IllegalArgumentException(errorMsg, e);
  225.         }

  226.         return evaluation;
  227.     }
  228. }