Commons Collections Transforme

 

接下来看Transformer组。

Transformer

ChainedTransformer

SwitchTransformer

TransformerUtils 

我们有时候需要将某个对象转换成另一个对象供另一组方法调用,而这两类对象的类型有可能并不是出于同一个继承体系的,或者说出了很基本的Object之外没有共同的父类,或者我们根本不关心他们是不是有其他继承关系,甚至就是同一个类的实例只是对我们而言无所谓,我们为了它能够被后续的调用者有意义的识别和处理,在这样的情形,我们就可以利用Transformer。除了基本的转型Transformer之外,Commons Collections还提供了Transformer链和带条件的Transformer,使得我们很方便的组装出有意义的转型逻辑。 

假定我们在处理员工聘用时,需要将原来的Applicant对象转换为Employee对象,而Applicant类和Employee类无论继承关系、字段内容、具体业务职能等等都不是同一派系的,只是某些字段是相关的,且要求必要的转换,那么这个时候我们使用Transformer就可以比较方便的实现这项功能,并且由于它的实现是灵活的、模块化的,使得今后的维护也变得清晰和易于处理。代码如下: 

 

 

/** TransformerUsage.java */
 
package sean.study.commons.collections;
 
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
 
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.SwitchTransformer;
import org.apache.commons.lang.StringUtils;
 
public class TransformerUsage {
 
    public static void main(String[] args) {
        demoTransformerUsage();
    }
   
    public static void demoTransformerUsage() {
 
        System.out.println(StringUtils.center(" demoTransformerUsage ", 40, "="));
 
        // data setup
        Applicant[] applicants = new Applicant[] {
            new Applicant("Tony", 26, "Developer"),
            new Applicant("Michelle", 24, "Tester"),
            new Applicant("Jack", 28, "Project Manager")
        };
        List appList = Arrays.asList(applicants);
       
        // predicate setup
        Predicate isDeveloper = new Predicate() {
            public boolean evaluate(Object obj) {
                Applicant app = (Applicant) obj;
                return "Developer".equalsIgnoreCase(app.getApplyFor());
            }
        };
        Predicate isTester = new Predicate() {
            public boolean evaluate(Object obj) {
                Applicant app = (Applicant) obj;
                return "Tester".equalsIgnoreCase(app.getApplyFor());
            }
        };
        Predicate isPM = new Predicate() {
            public boolean evaluate(Object obj) {
                Applicant app = (Applicant) obj;
                return "Project Manager".equalsIgnoreCase(app.getApplyFor());
            }
        };
        Predicate[] checkApplyFor = new Predicate[] {
            isDeveloper,
            isTester,
            isPM
        };
       
        // transformer setup
        Transformer developerTransformer = new Transformer() {
            public Object transform(Object obj) {
                Applicant app = (Applicant) obj;
                return new Employee(
                    app.getName(), app.getAge(), new Date(), "E4", 2000
                );
            }
        };
        Transformer testerTransformer = new Transformer() {
            public Object transform(Object obj) {
                Applicant app = (Applicant) obj;
                return new Employee(
                    app.getName(), app.getAge(), new Date(), "E4", 2000
                );
            }
        };
        Transformer pmTransformer = new Transformer() {
            public Object transform(Object obj) {
                Applicant app = (Applicant) obj;
                return new Employee(
                    app.getName(), app.getAge(), new Date(), "E5", 3000
                );
            }
        };
        Transformer[] transformers = new Transformer[] {
            developerTransformer,
            testerTransformer,
            pmTransformer
        };
       
        // transform
        Transformer employTransformer = new SwitchTransformer(
            checkApplyFor, transformers, null
        );
        Collection employed = CollectionUtils.collect(appList, employTransformer);
       
        // output
        System.out.println("Applicants: ");
        Iterator iter1 = appList.iterator();
        while (iter1.hasNext()) {
            System.out.println(iter1.next());
        }
        System.out.println("Employed: ");
        Iterator iter2 = employed.iterator();
        while (iter2.hasNext()) {
            System.out.println(iter2.next());
        }
 
        System.out.println(StringUtils.repeat("=", 40));
    }
 
}
 
以下是运行结果:
 
========= demoTransformerUsage =========
Applicants:
Applicant[name=Tony,age=26,applyFor=Developer]
Applicant[name=Michelle,age=24,applyFor=Tester]
Applicant[name=Jack,age=28,applyFor=Project Manager]
Employed:
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]
========================================
 
我们首先定义一组Predicate,用于在SwitchTransformer中判断采用那个具体的Transformer,这个具体的Transformer也是通过数组同时传递给SwitchTransformer的构造方法的。不同的Predicate可以有不同的实现,不同的Transformer也可以有不同的实现,因为它们之间实际上完全是相互独立的。这就使我们有效的分离了逻辑和具体业务。
来源:http://www.blogjava.net/sean/archive/2005/08/06/9466.html

你可能感兴趣的:(Collections)