Commons Collections - Transformer

接下来看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;

    }

 

}

 

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;

    }

 

} 

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值