1 16 17 package org.apache.commons.beanutils; 18 19 import org.apache.commons.collections.Predicate; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.lang.reflect.InvocationTargetException ; 24 25 30 public class BeanPredicate implements Predicate { 31 32 private final Log log = LogFactory.getLog(this.getClass()); 33 34 35 private String propertyName; 36 37 private Predicate predicate; 38 39 47 public BeanPredicate(String propertyName, Predicate predicate) { 48 this.propertyName = propertyName; 49 this.predicate = predicate; 50 } 51 52 57 public boolean evaluate(Object object) { 58 59 boolean evaluation = false; 60 61 try { 62 Object propValue = PropertyUtils.getProperty( object, propertyName ); 63 evaluation = predicate.evaluate(propValue); 64 } catch (IllegalArgumentException e) { 65 final String errorMsg = "Problem during evaluation."; 66 log.error("ERROR: " + errorMsg, e); 67 throw e; 68 } catch (IllegalAccessException e) { 69 final String errorMsg = "Unable to access the property provided."; 70 log.error(errorMsg, e); 71 throw new IllegalArgumentException (errorMsg); 72 } catch (InvocationTargetException e) { 73 final String errorMsg = "Exception occurred in property's getter"; 74 log.error(errorMsg, e); 75 throw new IllegalArgumentException (errorMsg); 76 } catch (NoSuchMethodException e) { 77 final String errorMsg = "Property not found."; 78 log.error(errorMsg, e); 79 throw new IllegalArgumentException (errorMsg); 80 } 81 82 return evaluation; 83 } 84 85 90 public String getPropertyName() { 91 return propertyName; 92 } 93 94 99 public void setPropertyName(String propertyName) { 100 this.propertyName = propertyName; 101 } 102 103 108 public Predicate getPredicate() { 109 return predicate; 110 } 111 112 117 public void setPredicate(Predicate predicate) { 118 this.predicate = predicate; 119 } 120 121 } 122 | Popular Tags |