Read Sean

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

          [Jakarta Commons筆記] Commons Collections - Closure組

          Posted on 2005-08-06 12:31 laogao 閱讀(2154) 評論(1)  編輯  收藏 所屬分類: On Java

           

          接下來看Closure組。

           

          Closure

          ChainedClosure

          IfClosure

          WhileClosure

          ClosureUtils

           

          Closure這一組接口和類提供一個操作對象的execute方法,為我們在處理一系列對象時可以將處理邏輯分離出來。理論上講,使用Transformer也可以達到類似的效果,只要輸出對象和輸入對象是同一個對象就好,但是Closure接口定義的execute方法返回void,并且從效果和功能區(qū)分上,Closure可以更好的詮釋對象處理或執(zhí)行的意思。而事實上,ClosureUtils中也提供了一個asClosure方法包裝一個現(xiàn)成的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());

                  }

              }

          }

           

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

           

          =========== 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作為整體執(zhí)行;IfClosure在創(chuàng)建時需要提供給它一個Predicate和兩個Closure,執(zhí)行時先做Predicate判定再決定執(zhí)行哪一個Closure;SwitchClosureSwitchTransformer類似,根據(jù)創(chuàng)建時傳入的Predicate組和Closure組對應(yīng)執(zhí)行;WhileClosure則根據(jù)創(chuàng)建時傳入的Predicate做判斷,如果為true則執(zhí)行Closure,直到Predicate返回false;等等。

           

          具體用法請參考Javadoc。

           

           

          Feedback

          # re: [Jakarta Commons筆記] Commons Collections - Closure組  回復(fù)  更多評論   

          2008-05-16 13:47 by nickevin
          05年的帖 08年來看依然很受用
          主站蜘蛛池模板: 西藏| 福安市| 友谊县| 尤溪县| 堆龙德庆县| 临桂县| 手游| 永丰县| 美姑县| 东兴市| 桐柏县| 五华县| 珲春市| 忻州市| 江津市| 青州市| 同仁县| 桂东县| 阳泉市| 延川县| 上杭县| 合江县| 微博| 海门市| 昌乐县| 湛江市| 福贡县| 霍林郭勒市| 萨迦县| 宽甸| 闸北区| 平和县| 莒南县| 雅江县| 盈江县| 浏阳市| 镇宁| 汶上县| 出国| 柞水县| 天等县|