alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Beanutils example source code file (BeanPredicate.java)

This example Commons Beanutils source code file (BeanPredicate.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Beanutils tags/keywords

beanpredicate, beanpredicate, error, illegalaccessexception, illegalargumentexception, illegalargumentexception, log, object, predicate, predicate, problem, reflection, string, string, unable

The Commons Beanutils BeanPredicate.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.beanutils;

import org.apache.commons.collections.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>Predicate implementation that applies the given Predicate
 * to the result of calling the given property getter.
 * </p>
 */
public class BeanPredicate implements Predicate {
   
    private final Log log = LogFactory.getLog(this.getClass());
    
    /** Name of the property whose value will be predicated */
    private String propertyName;
    /** <code>Predicate to be applied to the property value */
    private Predicate predicate;

    /**
     * Constructs a <code>BeanPredicate that applies the given
     * <code>Predicate to the named property value.
     * @param propertyName the name of the property whose value is to be predicated,
     * not null
     * @param predicate the <code>Predicate to be applied,
     * not null
     */
    public BeanPredicate(String propertyName, Predicate predicate) {
        this.propertyName = propertyName;
        this.predicate = predicate;
    }

    /**
     * Evaluates the given object by applying the {@link #getPredicate()}
     * to a property value named by {@link #getPropertyName()}.
     *
     * @param object The object being evaluated
     * @return the result of the predicate evaluation
     * @throws IllegalArgumentException when the property cannot be evaluated
     */
    public boolean evaluate(Object object) {
       
        boolean evaluation = false;

        try {
            Object propValue = PropertyUtils.getProperty( object, propertyName );
            evaluation = predicate.evaluate(propValue);
        } catch (IllegalArgumentException e) {
            final String errorMsg = "Problem during evaluation.";
            log.error("ERROR: " + errorMsg, e);
            throw e;
        } catch (IllegalAccessException e) {
            final String errorMsg = "Unable to access the property provided.";
            log.error(errorMsg, e);
            throw new IllegalArgumentException(errorMsg);
        } catch (InvocationTargetException e) {
            final String errorMsg = "Exception occurred in property's getter";
            log.error(errorMsg, e);
            throw new IllegalArgumentException(errorMsg);
        } catch (NoSuchMethodException e) {
            final String errorMsg = "Property not found.";
            log.error(errorMsg, e);
            throw new IllegalArgumentException(errorMsg);
        }

        return evaluation;
    }

    /**
     * Gets the name of the property whose value is to be predicated.
     * in the evaluation.
     * @return the property name, not null
     */ 
    public String getPropertyName() {
        return propertyName;
    }

    /** 
     * Sets the name of the property whose value is to be predicated.
     * @param propertyName the name of the property whose value is to be predicated,
     * not null
     */
    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    /**
     * Gets the <code>Predicate to be applied to the value of the named property
     * during {@link #evaluate}.
     * @return <code>Predicate, not null
     */
    public Predicate getPredicate() {
        return predicate;
    }

    /** 
     * Sets the <code>Predicate to be applied to the value of the named property
     * during {@link #evaluate(Object)}.
     * @param predicate <code>Predicate, not null
     */
    public void setPredicate(Predicate predicate) {
        this.predicate = predicate;
    }

}

Other Commons Beanutils examples (source code examples)

Here is a short list of links related to this Commons Beanutils BeanPredicate.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.