Sillycat Framework(II)Enhancement of BeanFilter

介绍SillycatFramework中BeanFilter接口的实现细节,包括过滤规则配置、属性忽略逻辑及Spring配置策略等。
Sillycat Framework(II)Enhancement of BeanFilter

1. filter the bean properties
2. consider the Map=null
3. consider the property is POJO, and POJO is null
4. consider the properties file loading and configuration policy

1. BeanFilter interface in Java
BeanFilter.java:
package com.sillycat.easymarket.filter;
public interface BeanFilter {
public void filter(Object obj, String rule);
}

2. BeanFilterImpl implemetation class in Groovy
BeanFilterImpl.groovy:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
import com.sillycat.easymarket.utils.SystemConfigurationAdapter;
import org.springframework.stereotype.Component;

public class BeanFilterImpl implements BeanFilter {

private static final String DEFAULT_RULE = "rule1";

private static final String DOT = ".";

private final Log log = LogFactory.getLog(this.getClass());

@Autowired
SystemConfigurationAdapter filterConfiguration;

public void filter(Object obj, String rule) {
if (null == obj) {
log.info("null Object do not need filter!");
return;
}
if (null == rule || "".equals(rule.trim())) {
rule = DEFAULT_RULE;
}
log.info("filter rule...." + rule);
List<String> filters = filterConfiguration.getStringList(rule);
if (filters != null && !filters.isEmpty()) {
for (int i = 0; i < filters.size(); i++) {
String filter = filters.get(i);
try {
if (!needIgnore(obj, filter)) {
log.info("doing filter rule...." + filter);
PropertyUtils.setProperty(obj, filter, null);
}
} catch (IllegalAccessException e) {
log.error(e);
} catch (InvocationTargetException e) {
log.error(e);
} catch (NoSuchMethodException e) {
log.error(e);
}
}
}
}
private boolean needIgnore(Object obj, String filter)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
if (null == filter && "".equals(filter.trim())) {
flag = true;
return flag;
}
if (isNullValueInPath(obj, filter, 0)) {
flag = true;
return flag;
}
return flag;
}
private boolean isNullValueInPath(Object obj, String filter, int start)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
boolean flag = false;
int location = 0;
if ((location = filter.indexOf(DOT, start)) > 0) {
String name = filter.substring(start, location);
Object obj_tmp = BeanUtils.getProperty(obj, name);
if (obj_tmp == null) {
flag = true;
return flag;
}
isNullValueInPath(obj, filter, location + 1);
}
return flag;
}
}

3. the filter.properties control the filter processor
#filter rule 1
rule1=personPassword
rule1=address.city
rule1=others.other2

#filter rule 2
rule2=personPassword
rule2=address.country
rule2=others.other1
rule2=others.other2
rule2=address.email

4. TestCase of this groovy implementation:
package com.sillycat.easymarket.filter;
import java.util.HashMap;
import java.util.Map;
import com.sillycat.core.BaseTestCase;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilterTest extends BaseTestCase {
private BeanFilter beanFilter;
public void setUp() throws Exception {
super.setUp();
beanFilter = (BeanFilter) ctx.getBean("beanFilter");
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testDumy() {
assertTrue(true);
}
public void testFilter() {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("hua.luo@chengdu.digby.com");
a.setId(Integer.valueOf(1));
//p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
//p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
beanFilter.filter(p, null);
System.out.println(p);
System.out.println(p.getOthers());
}
}

5. properties file loading policy in spring
<bean id="filterConfiguration" class="com.sillycat.easymarket.utils.SystemConfigurationAdapter">
<property name="configfile" value="filter.properties"/>
</bean>

package com.sillycat.easymarket.utils;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SystemConfigurationAdapter {
private final Log log = LogFactory.getLog(this.getClass());
private CompositeConfiguration config;
private PropertiesConfiguration propertiesConfig;
private String configfile = "easygroovy.properties";
public void init() {
config = new CompositeConfiguration();
if (propertiesConfig == null) {
try {
propertiesConfig = new PropertiesConfiguration(configfile);
log.info("configuration file loading..." + configfile);
propertiesConfig
.setReloadingStrategy(new FileChangedReloadingStrategy());
config.addConfiguration(propertiesConfig);
} catch (ConfigurationException e) {
log.error(e);
}
}
}
public String getString(String propertyKey) {
if (config == null) {
init();
}
return config.getString(propertyKey);
}
public String getString(String propertyKey, String defaultValue) {
if (config == null) {
init();
}
return config.getString(propertyKey, defaultValue);
}
public int getInt(String propertyKey) {
if (config == null) {
init();
}
return config.getInt(propertyKey);
}
public int getInt(String key, int defaultValue) {
if (config == null) {
init();
}
return config.getInt(key, defaultValue);
}
public float getFloat(String propertyKey) {
if (config == null) {
init();
}
return config.getFloat(propertyKey);
}
public float getFloat(String propertyKey, float defaultValue) {
if (config == null) {
init();
}
return config.getFloat(propertyKey, defaultValue);
}
public boolean getBoolean(String propertyKey) {
if (config == null) {
init();
}
return config.getBoolean(propertyKey);
}
public boolean getBoolean(String propertyKey, boolean defualtValue) {
return config.getBoolean(propertyKey, defualtValue);
}
public String[] getStringArray(String propertyKey) {
if (config == null) {
init();
}
return config.getStringArray(propertyKey);
}
public List<String> getStringList(String propertyKey) {
List<String> list = new ArrayList<String>();
String[] strArr = getStringArray(propertyKey);
for (String value : strArr) {
list.add(value);
}
return list;
}
@SuppressWarnings("rawtypes")
public List getList(String propertyKey) {
if (config == null) {
init();
}
return config.getList(propertyKey);
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
}
【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学与科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理与编程实现方法,重点聚焦于直流最优潮流模型的构建与求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现与学习。此外,文档还列举了大量与电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理与Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路与技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码与工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模与求解的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值