糊言乱语

志未半斤, 才无八两. 有苦有乐, 糊涂过活。
posts - 25, comments - 7, trackbacks - 0, articles - 42
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

接下来看Closure组。

 

Closure

ChainedClosure

IfClosure

WhileClosure

ClosureUtils

 

Closure这一组接口和类提供一个操作对象的execute方法,为我们在处理一系列对象时可以将处理逻辑分离出来。理论上讲,使用Transformer也可以达到类似的效果,只要输出对象和输入对象是同一个对象就好,但是Closure接口定义的execute方法返回void,并且从效果和功能区分上,Closure可以更好的诠释对象处理或执行的意思。而事实上,ClosureUtils中也提供了一个asClosure方法包装一个现成的Transformer

 

沿用前面的Emploee类,我们来给一组员工涨工资:

 

package sean.study.commons.collections;

 

import java.util.Arrays;

import java.util.Collection;

import java.util.Date;

import java.util.Iterator;

 

import org.apache.commons.collections.Closure;

import org.apache.commons.collections.CollectionUtils;

import org.apache.commons.lang.StringUtils;

 

public class ClosureUsage {

 

    public static void main(String[] args) {

        demoClosureUsage();

    }

   

    public static void demoClosureUsage() {

 

        System.out.println(StringUtils.center(" demoClosureUsage ", 40, "="));

       

        // data setup

        Employee[] employees = new Employee[] {

            new Employee("Tony", 26, new Date(), "E4", 2000),

            new Employee("Michelle", 24, new Date(), "E4", 2000),

            new Employee("Jack", 28, new Date(), "E5", 3000)

        };

        Collection empColl = Arrays.asList(employees);

        printColl("Before salary increase:", empColl);

       

        // closure setup

        Closure salaryIncreaseClosure = new Closure() {

            public void execute(Object obj) {

                Employee emp = (Employee) obj;

                emp.setSalary(emp.getSalary() * 1.20);

            }

        };

       

        // salary increase

        CollectionUtils.forAllDo(empColl, salaryIncreaseClosure);

        printColl("After salary increase:", empColl);

 

        System.out.println(StringUtils.repeat("=", 40));

    }

   

    public static void printColl(String label, Collection c) {

        if (StringUtils.isNotBlank(label)) {

            System.out.println(label);

        }

        Iterator iter = c.iterator();

        while (iter.hasNext()) {

            System.out.println(iter.next());

        }

    }

}

 

以下是运行结果:

 

=========== demoClosureUsage ===========

Before salary increase:

Employee[name=Tony,age=26,dateJoined=2005-08-05,grade=E4,salary=2000.0]

Employee[name=Michelle,age=24,dateJoined=2005-08-05,grade=E4,salary=2000.0]

Employee[name=Jack,age=28,dateJoined=2005-08-05,grade=E5,salary=3000.0]

After salary increase:

Employee[name=Tony,age=26,dateJoined=2005-08-05,grade=E4,salary=2400.0]

Employee[name=Michelle,age=24,dateJoined=2005-08-05,grade=E4,salary=2400.0]

Employee[name=Jack,age=28,dateJoined=2005-08-05,grade=E5,salary=3600.0]

========================================

 

我这里举的是一个相对简单的例子,在Closure这一组还有一些很方便的类,如ChainedClosure可以包装一组Closure作为整体执行;IfClosure在创建时需要提供给它一个Predicate和两个Closure,执行时先做Predicate判定再决定执行哪一个ClosureSwitchClosureSwitchTransformer类似,根据创建时传入的Predicate组和Closure组对应执行;WhileClosure则根据创建时传入的Predicate做判断,如果为true则执行Closure,直到Predicate返回false;等等。

 

具体用法请参考Javadoc

只有注册用户登录后才能发表评论。


网站导航: