一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

[Jakarta Commons筆記] Commons Collections - Transformer組

 moonboat 2008-11-12

接下來看Transformer組。

 

Transformer

ChainedTransformer

SwitchTransformer

TransformerUtils

 

我們有時(shí)候需要將某個(gè)對象轉(zhuǎn)換成另一個(gè)對象供另一組方法調(diào)用,而這兩類對象的類型有可能并不是出于同一個(gè)繼承體系的,或者說出了很基本的Object之外沒有共同的父類,或者我們根本不關(guān)心他們是不是有其他繼承關(guān)系,甚至就是同一個(gè)類的實(shí)例只是對我們而言無所謂,我們?yōu)榱怂軌虮缓罄m(xù)的調(diào)用者有意義的識別和處理,在這樣的情形,我們就可以利用Transformer。除了基本的轉(zhuǎn)型Transformer之外,Commons Collections還提供了Transformer鏈和帶條件的Transformer,使得我們很方便的組裝出有意義的轉(zhuǎn)型邏輯。

 

假定我們在處理員工聘用時(shí),需要將原來的Applicant對象轉(zhuǎn)換為Employee對象,而Applicant類和Employee類無論繼承關(guān)系、字段內(nèi)容、具體業(yè)務(wù)職能等等都不是同一派系的,只是某些字段是相關(guān)的,且要求必要的轉(zhuǎn)換,那么這個(gè)時(shí)候我們使用Transformer就可以比較方便的實(shí)現(xiàn)這項(xiàng)功能,并且由于它的實(shí)現(xiàn)是靈活的、模塊化的,使得今后的維護(hù)也變得清晰和易于處理。代碼如下:

 

/** 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));

    }

 

}

 

以下是運(yùn)行結(jié)果:

 

========= 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中判斷采用那個(gè)具體的Transformer,這個(gè)具體的Transformer也是通過數(shù)組同時(shí)傳遞給SwitchTransformer的構(gòu)造方法的。不同的Predicate可以有不同的實(shí)現(xiàn),不同的Transformer也可以有不同的實(shí)現(xiàn),因?yàn)樗鼈冎g實(shí)際上完全是相互獨(dú)立的。這就使我們有效的分離了邏輯和具體業(yè)務(wù)。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    亚洲一区二区三区福利视频| 99久久精品免费精品国产| 亚洲国产天堂av成人在线播放| 日本在线 一区 二区| 大伊香蕉一区二区三区| 日本高清中文精品在线不卡| 亚洲中文字幕熟女丝袜久久| 熟妇人妻av中文字幕老熟妇| 日韩欧美一区二区久久婷婷| 国产精品免费自拍视频| 亚洲一区二区三区三州| 欧美激情一区=区三区| 欧美一区二区三区播放| 久久99青青精品免费| 亚洲黄香蕉视频免费看| 国产目拍亚洲精品区一区| 99精品国产一区二区青青| av一区二区三区天堂| 国产又粗又爽又猛又黄的 | 超薄丝袜足一区二区三区| 国产又色又粗又黄又爽| 日韩欧美综合中文字幕| 日本欧美一区二区三区在线播| 中文字幕一区二区三区中文| 国产亚洲二区精品美女久久| 国产精品一区二区三区欧美| 国产美女精品人人做人人爽| 99精品国产一区二区青青| 国产情侣激情在线对白| 欧美丝袜诱惑一区二区| 大香伊蕉欧美一区二区三区| 亚洲国产精品久久网午夜| 日本免费一级黄色录像| 色丁香一区二区黑人巨大| 国产成人综合亚洲欧美日韩| 蜜桃传媒视频麻豆第一区| 国产成人精品国内自产拍| 欧美整片精品日韩综合| 大尺度剧情国产在线视频| 亚洲中文字幕有码在线观看| 国产精品日韩欧美一区二区|