BeanPropertyValueChangeConsumer.java

  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. package org.apache.commons.beanutils2;

  18. import java.lang.reflect.InvocationTargetException;
  19. import java.util.function.Consumer;

  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;

  22. /**
  23.  * <p>
  24.  * {@code Closure} that sets a property.
  25.  * </p>
  26.  * <p>
  27.  * An implementation of {@link java.util.function.Consumer} that updates a specified property on the object provided with a specified value. The
  28.  * {@code BeanPropertyValueChangeClosure} constructor takes two parameters which determine what property will be updated and with what value.
  29.  * <dl>
  30.  * <dt>{@code public BeanPropertyValueChangeClosure( String propertyName, Object propertyValue )}</dt>
  31.  * <dd>Will create a {@code Closure} that will update an object by setting the property specified by
  32.  * {@code propertyName</code> to the value specified by <code>propertyValue}.
  33.  *    </dd>
  34.  * </dl>
  35.  *
  36.  * <p>
  37.  * <strong>Note:</strong> Property names can be a simple, nested, indexed, or mapped property as defined by
  38.  * {@code org.apache.commons.beanutils2.PropertyUtils}.  If any object in the property path
  39.  * specified by {@code propertyName</code> is <code>null} then the outcome is based on the
  40.  * value of the {@code ignoreNull} attribute.
  41.  * </p>
  42.  * <p>
  43.  * A typical usage might look like:
  44.  * </p>
  45.  * <pre>{@code
  46.  * // create the closure
  47.  * BeanPropertyValueChangeClosure closure =
  48.  *    new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE );
  49.  *
  50.  * // update the Collection
  51.  * CollectionUtils.forAllDo( peopleCollection, closure );
  52.  * }</pre>
  53.  *
  54.  * This would take a {@code Collection} of person objects and update the
  55.  * {@code activeEmployee</code> property of each object in the <code>Collection} to {@code true}. Assuming...
  56.  * <ul>
  57.  * <li>The top level object in the {@code peopleCollection} is an object which represents a person.</li>
  58.  * <li>The person object has a {@code setActiveEmployee( boolean )} method which updates the value for the object's {@code activeEmployee} property.</li>
  59.  * </ul>
  60.  *
  61.  * @param <T> The type of the input to the operation
  62.  * @param <V> The property value type.
  63.  * @see org.apache.commons.beanutils2.PropertyUtils
  64.  * @see java.util.function.Consumer
  65.  */
  66. public class BeanPropertyValueChangeConsumer<T, V> implements Consumer<T> {

  67.     /** For logging. Each subclass gets its own log instance. */
  68.     private final Log log = LogFactory.getLog(this.getClass());

  69.     /**
  70.      * The name of the property which will be updated when this {@code Closure} executes.
  71.      */
  72.     private final String propertyName;

  73.     /**
  74.      * The value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
  75.      */
  76.     private final V propertyValue;

  77.     /**
  78.      * Determines whether {@code null} objects in the property path will generate an
  79.      * {@code IllegalArgumentException</code> or not. If set to <code>true} then if any objects
  80.      * in the property path leading up to the target property evaluate to {@code null} then the
  81.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged but
  82.      * not re-thrown.  If set to {@code false} then if any objects in the property path leading
  83.      * up to the target property evaluate to {@code null} then the
  84.      * {@code IllegalArgumentException</code> throw by <code>PropertyUtils} will be logged and re-thrown.
  85.      */
  86.     private final boolean ignoreNull;

  87.     /**
  88.      * Constructor which takes the name of the property to be changed, the new value to set the property to, and assumes
  89.      * {@code ignoreNull</code> to be <code>false}.
  90.      *
  91.      * @param propertyName  The name of the property that will be updated with the value specified by {@code propertyValue}.
  92.      * @param propertyValue The value that {@code propertyName} will be set to on the target object.
  93.      * @throws IllegalArgumentException If the propertyName provided is null or empty.
  94.      */
  95.     public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue) {
  96.         this(propertyName, propertyValue, false);
  97.     }

  98.     /**
  99.      * 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}
  100.      * objects in the property path will generate an {@code IllegalArgumentException} or not.
  101.      *
  102.      * @param propertyName  The name of the property that will be updated with the value specified by {@code propertyValue}.
  103.      * @param propertyValue The value that {@code propertyName} will be set to on the target object.
  104.      * @param ignoreNull    Determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
  105.      * @throws IllegalArgumentException If the propertyName provided is null or empty.
  106.      */
  107.     public BeanPropertyValueChangeConsumer(final String propertyName, final V propertyValue, final boolean ignoreNull) {
  108.         if (propertyName == null || propertyName.isEmpty()) {
  109.             throw new IllegalArgumentException("propertyName cannot be null or empty");
  110.         }
  111.         this.propertyName = propertyName;
  112.         this.propertyValue = propertyValue;
  113.         this.ignoreNull = ignoreNull;
  114.     }

  115.     /**
  116.      * Updates the target object provided using the property update criteria provided when this {@code BeanPropertyValueChangeClosure} was constructed. If any
  117.      * 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
  118.      * {@code ignoreNull</code> attribute. By default, <code>ignoreNull} is
  119.      * {@code false</code> and would result in an <code>IllegalArgumentException} if an object
  120.      * in the property path leading up to the target property is {@code null}.
  121.      *
  122.      * &#64;param object The object to be updated.
  123.      * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
  124.      * NoSuchMethodException is thrown when trying to access the property specified on the object
  125.      * provided. Or if an object in the property path provided is {@code null} and
  126.      * {@code ignoreNull</code> is set to <code>false}.
  127.      */
  128.     @Override
  129.     public void accept(final Object object) {

  130.         try {
  131.             PropertyUtils.setProperty(object, propertyName, propertyValue);
  132.         } catch (final IllegalArgumentException e) {
  133.             final String errorMsg = "Unable to execute Closure. Null value encountered in property path...";

  134.             if (!ignoreNull) {
  135.                 throw new IllegalArgumentException(errorMsg, e);
  136.             }
  137.             log.warn(errorMsg, e);
  138.         } catch (final IllegalAccessException e) {
  139.             final String errorMsg = "Unable to access the property provided.";
  140.             throw new IllegalArgumentException(errorMsg, e);
  141.         } catch (final InvocationTargetException e) {
  142.             final String errorMsg = "Exception occurred in property's getter";
  143.             throw new IllegalArgumentException(errorMsg, e);
  144.         } catch (final NoSuchMethodException e) {
  145.             final String errorMsg = "Property not found";
  146.             throw new IllegalArgumentException(errorMsg, e);
  147.         }
  148.     }

  149.     /**
  150.      * Returns the name of the property which will be updated when this {@code Closure} executes.
  151.      *
  152.      * @return The name of the property which will be updated when this {@code Closure} executes.
  153.      */
  154.     public String getPropertyName() {
  155.         return propertyName;
  156.     }

  157.     /**
  158.      * Returns the value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
  159.      *
  160.      * @return The value that the property specified by {@code propertyName} will be updated to when this {@code Closure} executes.
  161.      */
  162.     public V getPropertyValue() {
  163.         return propertyValue;
  164.     }

  165.     /**
  166.      * Returns the flag that determines whether {@code null} objects in the property path will generate an
  167.      * {@code IllegalArgumentException</code> or not. If set to <code>true} then
  168.      * if any objects in the property path leading up to the target property evaluate to
  169.      * {@code null</code> then the <code>IllegalArgumentException} throw by
  170.      * {@code PropertyUtils</code> will be logged but not re-thrown.  If set to <code>false} then
  171.      * if any objects in the property path leading up to the target property evaluate to
  172.      * {@code null</code> then the <code>IllegalArgumentException} throw by {@code PropertyUtils} will be logged and re-thrown.
  173.      *
  174.      * @return The flag that determines whether {@code null} objects in the property path will generate an {@code IllegalArgumentException} or not.
  175.      */
  176.     public boolean isIgnoreNull() {
  177.         return ignoreNull;
  178.     }
  179. }