Read Sean

Read me, read Sean.
posts - 508, comments - 655, trackbacks - 9, articles - 4

[Jakarta Commons笔记] Commons Collections - Transformer组

Posted on 2005-08-06 10:40 laogao 阅读(2663) 评论(1)  编辑  收藏 所属分类: On Java

 

接下来看Transformer组。

 

Transformer

ChainedTransformer

SwitchTransformer

TransformerUtils

 

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

 

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

 

/** Applicant.java */

 

package sean.study.commons.collections;

 

import org.apache.commons.lang.builder.ToStringBuilder;

import org.apache.commons.lang.builder.ToStringStyle;

 

public class Applicant {

   

    private String name;

    private int age;

    private String applyFor;

   

    public Applicant() {

    }

 

    public Applicant(String name, int age, String applyFor) {

        this.name = name;

        this.age = age;

        this.applyFor = applyFor;

    }

   

    public String toString() {

        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)

                .append("name", name)

                .append("age", age)

                .append("applyFor", applyFor)

                .toString();

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public String getApplyFor() {

        return applyFor;

    }

 

    public void setApplyFor(String applyFor) {

        this.applyFor = applyFor;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

}

 

/** Employee.java */

 

package sean.study.commons.collections;

 

import java.util.Date;

 

import org.apache.commons.lang.builder.ToStringBuilder;

import org.apache.commons.lang.builder.ToStringStyle;

import org.apache.commons.lang.time.DateFormatUtils;

 

public class Employee {

 

    private String name;

    private int age;

    private Date dateJoined;

    private String grade;

    private double salary;

   

    public Employee() {

    }

 

    public Employee(String name, int age, Date dateJoined, String grade, double salary) {

        this.name = name;

        this.age = age;

        this.dateJoined = dateJoined;

        this.grade = grade;

        this.salary = salary;

    }

   

    public String toString() {

        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)

                .append("name", name)

                .append("age", age)

                .append("dateJoined", DateFormatUtils.format(dateJoined, "yyyy-MM-dd"))

                .append("grade", grade)

                .append("salary", salary)

                .toString();

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public Date getDateJoined() {

        return dateJoined;

    }

 

    public void setDateJoined(Date dateJoined) {

        this.dateJoined = dateJoined;

    }

 

    public String getGrade() {

        return grade;

    }

 

    public void setGrade(String grade) {

        this.grade = grade;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public double getSalary() {

        return salary;

    }

 

    public void setSalary(double salary) {

        this.salary = salary;

    }

 

}

 

/** 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也可以有不同的实现,因为它们之间实际上完全是相互独立的。这就使我们有效的分离了逻辑和具体业务。

 

 

Feedback

# re: [Jakarta Commons笔记] Commons Collections - Transformer组  回复  更多评论   

2006-12-10 00:25 by
言简意赅,非常清楚

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


网站导航: