BeanPredicate.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.function.Predicate;

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

  22. /**
  23.  * <p>
  24.  * Predicate implementation that applies the given {@code Predicate} to the result of calling the given property getter.
  25.  * </p>
  26.  *
  27.  * @param <T> the type of the input to the predicate
  28.  */
  29. public class BeanPredicate<T> implements Predicate<T> {

  30.     private final Log log = LogFactory.getLog(this.getClass());

  31.     /** Name of the property whose value will be predicated */
  32.     private String propertyName;

  33.     /** {@code Predicate} to be applied to the property value */
  34.     private Predicate<T> predicate;

  35.     /**
  36.      * Constructs a {@code BeanPredicate} that applies the given {@code Predicate} to the named property value.
  37.      *
  38.      * @param propertyName the name of the property whose value is to be predicated, not null
  39.      * @param predicate    the {@code Predicate} to be applied, not null
  40.      */
  41.     public BeanPredicate(final String propertyName, final Predicate<T> predicate) {
  42.         this.propertyName = propertyName;
  43.         this.predicate = predicate;
  44.     }

  45.     /**
  46.      * Gets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}.
  47.      *
  48.      * @return {@code Predicate}, not null
  49.      */
  50.     public Predicate<T> getPredicate() {
  51.         return predicate;
  52.     }

  53.     /**
  54.      * Gets the name of the property whose value is to be predicated. in the evaluation.
  55.      *
  56.      * @return the property name, not null
  57.      */
  58.     public String getPropertyName() {
  59.         return propertyName;
  60.     }

  61.     /**
  62.      * Sets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}.
  63.      *
  64.      * @param predicate {@code Predicate}, not null
  65.      */
  66.     public void setPredicate(final Predicate<T> predicate) {
  67.         this.predicate = predicate;
  68.     }

  69.     /**
  70.      * Sets the name of the property whose value is to be predicated.
  71.      *
  72.      * @param propertyName the name of the property whose value is to be predicated, not null
  73.      */
  74.     public void setPropertyName(final String propertyName) {
  75.         this.propertyName = propertyName;
  76.     }

  77.     /**
  78.      * Evaluates the given object by applying the {@link #getPredicate()} to a property value named by {@link #getPropertyName()}.
  79.      *
  80.      * @param object The object to test
  81.      * @return the result of the predicate evaluation
  82.      * @throws IllegalArgumentException when the property cannot be evaluated
  83.      */
  84.     @Override
  85.     public boolean test(final Object object) {
  86.         boolean evaluation = false;

  87.         try {
  88.             final T propValue = (T) PropertyUtils.getProperty(object, propertyName);
  89.             evaluation = predicate.test(propValue);
  90.         } catch (final IllegalArgumentException e) {
  91.             final String errorMsg = "Problem during evaluation.";
  92.             log.error(errorMsg, e);
  93.             throw e;
  94.         } catch (final IllegalAccessException e) {
  95.             final String errorMsg = "Unable to access the property provided.";
  96.             log.error(errorMsg, e);
  97.             throw new IllegalArgumentException(errorMsg);
  98.         } catch (final InvocationTargetException e) {
  99.             final String errorMsg = "Exception occurred in property's getter";
  100.             log.error(errorMsg, e);
  101.             throw new IllegalArgumentException(errorMsg);
  102.         } catch (final NoSuchMethodException e) {
  103.             final String errorMsg = "Property not found.";
  104.             log.error(errorMsg, e);
  105.             throw new IllegalArgumentException(errorMsg);
  106.         }

  107.         return evaluation;
  108.     }

  109. }