    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.beanutils2;
    019
    020import java.lang.reflect.InvocationTargetException;
    021import java.util.function.Consumer;
    022
    023import org.apache.commons.logging.Log;
    024import org.apache.commons.logging.LogFactory;
    025
    026/**
    027 * <p>
    028 * {@code Closure} that sets a property.
    029 * </p>
    030 * <p>
    031 * An implementation of {@link java.util.function.Consumer} that updates a specified property on the object provided with a specified value. The
    032 * {@code BeanPropertyValueChangeClosure} constructor takes two parameters which determine what property will be updated and with what value.
    033 * <dl>
    034 * <dt>{@code public BeanPropertyValueChangeClosure( String propertyName, Object propertyValue )}</dt>
    035 * <dd>Will create a {@code Closure} that will update an object by setting the property specified by
    036 * {@code propertyName</code> to the value specified by <code>propertyValue}.
    037 *    </dd>
    038 * </dl>
    039 *
    040 * <p>
    041 * <strong>Note:</strong> Property names can be a simple, nested, indexed, or mapped property as defined by
    042 * {@code org.apache.commons.beanutils2.PropertyUtils}.  If any object in the property path
    043 * specified by {@code propertyName</code> is <code>null} then the outcome is based on the
    044 * value of the {@code ignoreNull} attribute.
    045 * </p>
    046 * <p>
    047 * A typical usage might look like:
    048 * </p>
    049 * <pre>{@code
    050 * // create the closure
    051 * BeanPropertyValueChangeClosure closure =
    052 *    new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE );
    053 *
    054 * // update the Collection
    055 * CollectionUtils.forAllDo( peopleCollection, closure );
    056 * }</pre>
    057 *
    058 * This would take a {@code Collection} of person objects and update the
    059 * {@code activeEmployee</code> property of each object in the <code>Collection} to {@code true}. Assuming...
    060 * <ul>
    061 * <li>The top level object in the {@code peopleCollection} is an object which represents a person.</li>
    062 * <li>The person object has a {@code setActiveEmployee( boolean )} method which updates the value for the object's {@code activeEmployee} property.</li>
    063 * </ul>
    064 *
    065 * @param <T> The type of the input to the operation
    066 * @param <V> The property value type.
    067 * @see org.apache.commons.beanutils2.PropertyUtils
    068 * @see java.util.function.Consumer
    069 */
    070public class BeanPropertyValueChangeConsumer<T, V> implements Consumer<T> {
    071
    072    /** For logging. Each subclass gets its own log instance. */
    073    private final Log log = LogFactory.getLog(this.getClass());
    074
    075    /**
    076     * The name of the property which will be updated when this {@code Closure} executes.
    077     */
    078    private final String propertyName;
    079
    080    /**
    081     * The value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
    082     */
    083    private final V propertyValue;
    084
    085    /**
    086     * Determines whether {@code null} objects in the property path will generate an
    087     * {@code IllegalArgumentException</code> or not. If set to <code>true} then if any objects
    088     * in the property path leading up to the target property evaluate to {@code null} then the
    089     * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged but
    090     * not re-thrown.  If set to {@code false} then if any objects in the property path leading
    091     * up to the target property evaluate to {@code null} then the
    092     * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged and re-thrown.
    093     */
    094    private final boolean ignoreNull;
    095
    096    /**
    097     * Constructor which takes the name of the property to be changed, the new value to set the property to, and assumes
    098     * {@code ignoreNull</code> to be <code>false}.
    099     *
    100     * @param propertyName  The name of the property that will be updated with the value specified by {@code propertyValue}.
    101     * @param propertyValue The value that {@code propertyName} will be set to on the target object.
    102     * @throws IllegalArgumentException If the propertyName provided is null or empty.
    103     */
    104    public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue) {
    105        this(propertyName, propertyValue, false);
    106    }
    107
    108    /**
    109     * Constructor which takes the name of the property to be changed, the new value to set the property to and a boolean which determines whether {@code null}
    110     * objects in the property path will generate an {@code IllegalArgumentException} or not.
    111     *
    112     * @param propertyName  The name of the property that will be updated with the value specified by {@code propertyValue}.
    113     * @param propertyValue The value that {@code propertyName} will be set to on the target object.
    114     * @param ignoreNull    Determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
    115     * @throws IllegalArgumentException If the propertyName provided is null or empty.
    116     */
    117    public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue, final boolean ignoreNull) {
    118        if (propertyName == null || propertyName.isEmpty()) {
    119            throw new IllegalArgumentException("propertyName cannot be null or empty");
    120        }
    121        this.propertyName = propertyName;
    122        this.propertyValue = propertyValue;
    123        this.ignoreNull = ignoreNull;
    124    }
    125
    126    /**
    127     * Updates the target object provided using the property update criteria provided when this {@code BeanPropertyValueChangeClosure} was constructed. If any
    128     * object in the property path leading up to the target property is {@code null} then the outcome will be based on the value of the
    129     * {@code ignoreNull</code> attribute. By default, <code>ignoreNull} is
    130     * {@code false</code> and would result in an <code>IllegalArgumentException} if an object
    131     * in the property path leading up to the target property is {@code null}.
    132     *
    133     * &#64;param object The object to be updated.
    134     * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
    135     * NoSuchMethodException is thrown when trying to access the property specified on the object
    136     * provided. Or if an object in the property path provided is {@code null} and
    137     * {@code ignoreNull</code> is set to <code>false}.
    138     */
    139    @Override
    140    public void accept(final Object object) {
    141
    142        try {
    143            PropertyUtils.setProperty(object, propertyName, propertyValue);
    144        } catch (final IllegalArgumentException e) {
    145            final String errorMsg = "Unable to execute Closure. Null value encountered in property path...";
    146
    147            if (!ignoreNull) {
    148                throw new IllegalArgumentException(errorMsg, e);
    149            }
    150            log.warn(errorMsg, e);
    151        } catch (final IllegalAccessException e) {
    152            final String errorMsg = "Unable to access the property provided.";
    153            throw new IllegalArgumentException(errorMsg, e);
    154        } catch (final InvocationTargetException e) {
    155            final String errorMsg = "Exception occurred in property's getter";
    156            throw new IllegalArgumentException(errorMsg, e);
    157        } catch (final NoSuchMethodException e) {
    158            final String errorMsg = "Property not found";
    159            throw new IllegalArgumentException(errorMsg, e);
    160        }
    161    }
    162
    163    /**
    164     * Returns the name of the property which will be updated when this {@code Closure} executes.
    165     *
    166     * @return The name of the property which will be updated when this {@code Closure} executes.
    167     */
    168    public String getPropertyName() {
    169        return propertyName;
    170    }
    171
    172    /**
    173     * Returns the value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
    174     *
    175     * @return The value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
    176     */
    177    public V getPropertyValue() {
    178        return propertyValue;
    179    }
    180
    181    /**
    182     * Returns the flag that determines whether {@code null} objects in the property path will generate an
    183     * {@code IllegalArgumentException</code> or not. If set to <code>true} then
    184     * if any objects in the property path leading up to the target property evaluate to
    185     * {@code null</code> then the <code>IllegalArgumentException} throw by
    186     * {@code PropertyUtils</code> will be logged but not re-thrown.  If set to <code>false} then
    187     * if any objects in the property path leading up to the target property evaluate to
    188     * {@code null</code> then the <code>IllegalArgumentException} throw by {@code PropertyUtils} will be logged and re-thrown.
    189     *
    190     * @return The flag that determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
    191     */
    192    public boolean isIgnoreNull() {
    193        return ignoreNull;
    194    }
    195}