001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.beanutils2; 019 020import java.lang.reflect.InvocationTargetException; 021import java.util.function.Predicate; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026/** 027 * <p> 028 * Predicate implementation that applies the given {@code Predicate} to the result of calling the given property getter. 029 * </p> 030 * 031 * @param <T> the type of the input to the predicate 032 */ 033public class BeanPredicate<T> implements Predicate<T> { 034 035 private final Log log = LogFactory.getLog(this.getClass()); 036 037 /** Name of the property whose value will be predicated */ 038 private String propertyName; 039 040 /** {@code Predicate} to be applied to the property value */ 041 private Predicate<T> predicate; 042 043 /** 044 * Constructs a {@code BeanPredicate} that applies the given {@code Predicate} to the named property value. 045 * 046 * @param propertyName the name of the property whose value is to be predicated, not null 047 * @param predicate the {@code Predicate} to be applied, not null 048 */ 049 public BeanPredicate(final String propertyName, final Predicate<T> predicate) { 050 this.propertyName = propertyName; 051 this.predicate = predicate; 052 } 053 054 /** 055 * Gets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}. 056 * 057 * @return {@code Predicate}, not null 058 */ 059 public Predicate<T> getPredicate() { 060 return predicate; 061 } 062 063 /** 064 * Gets the name of the property whose value is to be predicated. in the evaluation. 065 * 066 * @return the property name, not null 067 */ 068 public String getPropertyName() { 069 return propertyName; 070 } 071 072 /** 073 * Sets the {@code Predicate} to be applied to the value of the named property during {@link #test(Object)}. 074 * 075 * @param predicate {@code Predicate}, not null 076 */ 077 public void setPredicate(final Predicate<T> predicate) { 078 this.predicate = predicate; 079 } 080 081 /** 082 * Sets the name of the property whose value is to be predicated. 083 * 084 * @param propertyName the name of the property whose value is to be predicated, not null 085 */ 086 public void setPropertyName(final String propertyName) { 087 this.propertyName = propertyName; 088 } 089 090 /** 091 * Evaluates the given object by applying the {@link #getPredicate()} to a property value named by {@link #getPropertyName()}. 092 * 093 * @param object The object to test 094 * @return the result of the predicate evaluation 095 * @throws IllegalArgumentException when the property cannot be evaluated 096 */ 097 @Override 098 public boolean test(final Object object) { 099 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}