基于Java实现的《yakoo5.matcher》实用匹配器小工具

来源:互联网 发布:淘宝店装饰素材 编辑:程序博客网 时间:2024/06/10 04:15

背景

    在我的工作中经常有需要处理各种:如果某个条件成立,做对应的处理操作, 而且这些条件和处理操作比较多,业务多变,变更比较频繁,基于此开发了这个匹配器小工具,目前主要作为公司的一个权限控制组件的基础组件使用。

if( XX条件成立 ) {    // do something}


提供的功能

    提供常见的相等匹配(EqualsMatcher)、字符串匹配(RegexMatcher)、是否对象或集合为空匹配(NullMatcherEmptyMatcher)、反相匹配(NotMatcher),以及通过配置对象属性(含嵌套属性)的配置来实现复杂对象的匹配(PropertiesMatcherStringPropertyRegexMatcher),支持条件组合(ComplexMatcher)匹配等功能。

    可以在此匹配器的基础上实现自定义的判断、匹配、校验等功能,也可在此工具的基础上实现权限控制组件。

    当然也可以通过扩展/实现yakoo5.matcher.Matcher接口来实现自定义的匹配器,满足业务相关的功能。

 

API 概览

Demo(JUnit TestCase)

 PropertiesMatcherTest.java

public void testMatches_XmlConfig() {        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:spring-PropertiesMatcher.xml");        PropertiesMatcher matcher = (PropertiesMatcher) appContext.getBean("propertiesMatcher");        Person person = new Person("Benjamin", "Wu");        List<Person> children = new ArrayList<Person>(2);        children.add(new Person("Jake", "Chen"));        children.add(new Person("Jone", "Smith"));        person.setChildren(children);        assertTrue(matcher.matches(person);    }

Person.java

public class Person {    // 第一个名字    private String firstName;    // 姓氏    private String familyName;    // 孩子    private List<Person> children;    public Person() {    }    /**     * @param firstName     * @param familyName     */    public Person(String firstName, String familyName) {        super();        this.firstName = firstName;        this.familyName = familyName;    }    // --- Setters & Getters ---    // ……}

spring-PropertiesMatcher.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="propertiesMatcher" class="yakoo5.matcher.PropertiesMatcher"><constructor-arg><map><entry key="firstName"><bean class="yakoo5.matcher.RegexMatcher"><constructor-arg><value><![CDATA[^Ben\w+]]></value></constructor-arg></bean></entry><entry key="familyName"><bean class="yakoo5.matcher.RegexMatcher"><constructor-arg><value><![CDATA[^W\w+]]></value></constructor-arg></bean></entry><entry key="children[1].familyName"><bean class="yakoo5.matcher.RegexMatcher"><constructor-arg><value><![CDATA[^Smith\w*$]]></value></constructor-arg></bean></entry></map></constructor-arg></bean></beans>

 

下载地址:http://pan.baidu.com/s/15WAUB

压缩包内文件说明:

A)Matcher/dist/yakoo5.matcher-1.0.2.jar                - 编译后的二进制class代码jar包;

B)Matcher/dist/yakoo5.matcher-1.0.2-javadoc.jar  - javadoc api说明文档;

C)Matcher/dist/yakoo5.matcher-1.0.2-src.jar         - 源代码(含JUnit单元测试代码)。

备注:

         (1)本工具组件不适宜直接使用,适合基于此组件进行扩展开发以实现特定的业务功能(如简单的权限控制等);

        (2)另外,本工具组件代码未做到全覆盖单元测试,如发现bug或缺陷,以及可以改进的地方,欢迎各位朋友探讨批评指正。

联系方式:yakoo5@163.com

 

原创粉丝点击