1. /*
  2. * Copyright 2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.beanutils;
  17. import org.apache.commons.collections.Predicate;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import java.lang.reflect.InvocationTargetException;
  21. /**
  22. * <p>Predicate implementation that applies the given <code>Predicate</code>
  23. * to the result of calling the given property getter.
  24. * </p>
  25. */
  26. public class BeanPredicate implements Predicate {
  27. private final Log log = LogFactory.getLog(this.getClass());
  28. /** Name of the property whose value will be predicated */
  29. private String propertyName;
  30. /** <code>Predicate</code> to be applied to the property value */
  31. private Predicate predicate;
  32. /**
  33. * Constructs a <code>BeanPredicate</code> that applies the given
  34. * <code>Predicate</code> to the named property value.
  35. * @param propertyName the name of the property whose value is to be predicated,
  36. * not null
  37. * @param predicate the <code>Predicate</code> to be applied,
  38. * not null
  39. */
  40. public BeanPredicate(String propertyName, Predicate predicate) {
  41. this.propertyName = propertyName;
  42. this.predicate = predicate;
  43. }
  44. /**
  45. * Evaluates the given object by applying the {@link #getPredicate()}
  46. * to a property value named by {@link #getPropertyName()}.
  47. * @throws IllegalAccessException when the property cannot be evaluated
  48. */
  49. public boolean evaluate(Object object) {
  50. boolean evaluation = false;
  51. try {
  52. Object propValue = PropertyUtils.getProperty( object, propertyName );
  53. evaluation = predicate.evaluate(propValue);
  54. } catch (IllegalArgumentException e) {
  55. final String errorMsg = "Problem during evaluation.";
  56. log.error("ERROR: " + errorMsg, e);
  57. throw e;
  58. } catch (IllegalAccessException e) {
  59. final String errorMsg = "Unable to access the property provided.";
  60. log.error(errorMsg, e);
  61. throw new IllegalArgumentException(errorMsg);
  62. } catch (InvocationTargetException e) {
  63. final String errorMsg = "Exception occurred in property's getter";
  64. log.error(errorMsg, e);
  65. throw new IllegalArgumentException(errorMsg);
  66. } catch (NoSuchMethodException e) {
  67. final String errorMsg = "Property not found.";
  68. log.error(errorMsg, e);
  69. throw new IllegalArgumentException(errorMsg);
  70. }
  71. return evaluation;
  72. }
  73. /**
  74. * Gets the name of the property whose value is to be predicated.
  75. * in the evaluation.
  76. * @return the property name, not null
  77. */
  78. public String getPropertyName() {
  79. return propertyName;
  80. }
  81. /**
  82. * Sets the name of the property whose value is to be predicated.
  83. * @param propertyName the name of the property whose value is to be predicated,
  84. * not null
  85. */
  86. public void setPropertyName(String propertyName) {
  87. this.propertyName = propertyName;
  88. }
  89. /**
  90. * Gets the <code>Predicate</code> to be applied to the value of the named property
  91. * during {@link #evaluate}.
  92. * @return <code>Predicate</code>, not null
  93. */
  94. public Predicate getPredicate() {
  95. return predicate;
  96. }
  97. /**
  98. * Sets the <code>Predicate</code> to be applied to the value of the named property
  99. * during {@link evaluate}.
  100. * @param predicate <code>Predicate</code>, not null
  101. */
  102. public void setPredicate(Predicate predicate) {
  103. this.predicate = predicate;
  104. }
  105. }