com.feilong.core.util.closure.BeanPropertyValueChangeClosure Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* Licensed 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 com.feilong.core.util.closure;
import org.apache.commons.collections4.Closure;
import com.feilong.core.Validate;
import com.feilong.core.bean.PropertyUtil;
/**
* {@link org.apache.commons.collections4.Closure} 实现,用来更新指定属性的指定值.
* 构造函数:
*
*
*
* BeanPropertyValueChangeClosure 构造函数提供两个参数, 属性名称和指定的属性值
*
*
* public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue)
*
*
* 注意: Possibly indexed and/or nested name of the property to be
* modified,参见propertyName.
*
*
* 典型的使用示例:
*
*
*
*
* // create the closure
* BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("activeEmployee", Boolean.TRUE);
*
* // update the Collection
* CollectionUtils.forAllDo(peopleCollection, closure);
*
*
* 上面的示例将会提取 peopleCollection
的每个 person 对象, 并且更新activeEmployee
属性值为true
.
*
*
*
* @author feilong
* @param
* the generic type
* @see "org.apache.commons.beanutils.BeanPropertyValueChangeClosure"
* @see "org.apache.commons.collections4.CollectionUtils#forAllDo(Iterable, Closure)"
* @see "org.apache.commons.collections4.IterableUtils#forEach(Iterable, Closure)"
* @since 1.10.2
*/
public class BeanPropertyValueChangeClosure implements Closure{
/**
* 指定bean对象排序属性名字.
*
*
* 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
* propertyName
*
*/
private final String propertyName;
/** 指定的属性值. */
private final Object propertyValue;
//---------------------------------------------------------------
/**
* Instantiates a new bean property value change closure.
*
*
* 如果 propertyName
是null,抛出 {@link NullPointerException}
* 如果 propertyName
是blank,抛出 {@link IllegalArgumentException}
*
*
* @param propertyName
* 指定bean对象排序属性名字.
*
* 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见
* propertyName
*
* @param propertyValue
* the value
*/
public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue){
Validate.notBlank(propertyName, "propertyName can't be blank!");
this.propertyName = propertyName;
this.propertyValue = propertyValue;
}
//---------------------------------------------------------------
/*
* (non-Javadoc)
*
* @see org.apache.commons.collections4.Closure#execute(java.lang.Object)
*/
@Override
public void execute(T input){
if (null == input){//如果 input是null,跳过去
return;
}
PropertyUtil.setProperty(input, propertyName, propertyValue);
}
}