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 26 108 public class BeanPropertyValueEqualsPredicate implements Predicate { 109 110 111 private final Log log = LogFactory.getLog(this.getClass()); 112 113 116 private String propertyName; 117 118 122 private Object propertyValue; 123 124 137 private boolean ignoreNull; 138 139 147 public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue) { 148 this(propertyName, propertyValue, false); 149 } 150 151 162 public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue, boolean ignoreNull) { 163 super(); 164 165 if ((propertyName != null) && (propertyName.length() > 0)) { 166 this.propertyName = propertyName; 167 this.propertyValue = propertyValue; 168 this.ignoreNull = ignoreNull; 169 } else { 170 throw new IllegalArgumentException ("propertyName cannot be null or empty"); 171 } 172 } 173 174 192 public boolean evaluate(Object object) { 193 194 boolean evaluation = false; 195 196 try { 197 evaluation = evaluateValue(propertyValue, 198 PropertyUtils.getProperty(object, propertyName)); 199 } catch (IllegalArgumentException e) { 200 final String errorMsg = "Problem during evaluation. Null value encountered in property path..."; 201 202 if (ignoreNull) { 203 log.warn("WARNING: " + errorMsg, e); 204 } else { 205 log.error("ERROR: " + errorMsg, e); 206 throw e; 207 } 208 } catch (IllegalAccessException e) { 209 final String errorMsg = "Unable to access the property provided."; 210 log.error(errorMsg, e); 211 throw new IllegalArgumentException (errorMsg); 212 } catch (InvocationTargetException e) { 213 final String errorMsg = "Exception occurred in property's getter"; 214 log.error(errorMsg, e); 215 throw new IllegalArgumentException (errorMsg); 216 } catch (NoSuchMethodException e) { 217 final String errorMsg = "Property not found."; 218 log.error(errorMsg, e); 219 throw new IllegalArgumentException (errorMsg); 220 } 221 222 return evaluation; 223 } 224 225 233 private boolean evaluateValue(Object expected, Object actual) { 234 return (expected == actual) || ((expected != null) && expected.equals(actual)); 235 } 236 237 244 public String getPropertyName() { 245 return propertyName; 246 } 247 248 255 public Object getPropertyValue() { 256 return propertyValue; 257 } 258 259 272 public boolean isIgnoreNull() { 273 return ignoreNull; 274 } 275 } 276 | Popular Tags |