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