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  
18  package org.apache.commons.beanutils2;
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.function.Predicate;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * <p>
28   * Predicate implementation that applies the given {@code Predicate} to the result of calling the given property getter.
29   * </p>
30   *
31   * @param <T> the type of the input to the predicate
32   */
33  public class BeanPredicate<T> implements Predicate<T> {
34  
35      private final Log log = LogFactory.getLog(this.getClass());
36  
37      /** Name of the property whose value will be predicated */
38      private String propertyName;
39  
40      /** {@code Predicate} to be applied to the property value */
41      private Predicate<T> predicate;
42  
43      /**
44       * Constructs a {@code BeanPredicate} that applies the given {@code Predicate} to the named property value.
45       *
46       * @param propertyName the name of the property whose value is to be predicated, not null
47       * @param predicate    the {@code Predicate} to be applied, not null
48       */
49      public BeanPredicate(final String propertyName, final Predicate<T> predicate) {
50          this.propertyName = propertyName;
51          this.predicate = predicate;
52      }
53  
54      /**
55       * Gets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}.
56       *
57       * @return {@code Predicate}, not null
58       */
59      public Predicate<T> getPredicate() {
60          return predicate;
61      }
62  
63      /**
64       * Gets the name of the property whose value is to be predicated. in the evaluation.
65       *
66       * @return the property name, not null
67       */
68      public String getPropertyName() {
69          return propertyName;
70      }
71  
72      /**
73       * Sets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}.
74       *
75       * @param predicate {@code Predicate}, not null
76       */
77      public void setPredicate(final Predicate<T> predicate) {
78          this.predicate = predicate;
79      }
80  
81      /**
82       * Sets the name of the property whose value is to be predicated.
83       *
84       * @param propertyName the name of the property whose value is to be predicated, not null
85       */
86      public void setPropertyName(final String propertyName) {
87          this.propertyName = propertyName;
88      }
89  
90      /**
91       * Evaluates the given object by applying the {@link #getPredicate()} to a property value named by {@link #getPropertyName()}.
92       *
93       * @param object The object to test
94       * @return the result of the predicate evaluation
95       * @throws IllegalArgumentException when the property cannot be evaluated
96       */
97      @Override
98      public boolean test(final Object object) {
99          boolean evaluation = false;
100 
101         try {
102             final T propValue = (T) PropertyUtils.getProperty(object, propertyName);
103             evaluation = predicate.test(propValue);
104         } catch (final IllegalArgumentException e) {
105             final String errorMsg = "Problem during evaluation.";
106             log.error(errorMsg, e);
107             throw e;
108         } catch (final IllegalAccessException e) {
109             final String errorMsg = "Unable to access the property provided.";
110             log.error(errorMsg, e);
111             throw new IllegalArgumentException(errorMsg);
112         } catch (final InvocationTargetException e) {
113             final String errorMsg = "Exception occurred in property's getter";
114             log.error(errorMsg, e);
115             throw new IllegalArgumentException(errorMsg);
116         } catch (final NoSuchMethodException e) {
117             final String errorMsg = "Property not found.";
118             log.error(errorMsg, e);
119             throw new IllegalArgumentException(errorMsg);
120         }
121 
122         return evaluation;
123     }
124 
125 }