Vincent.Chan‘s Blog

          常用鏈接

          統計

          積分與排名

          網站

          最新評論

          [Jakarta Commons筆記] Commons Collections - Closure組

          接下來看Closure組。

           

          Closure

          ChainedClosure

          IfClosure

          WhileClosure

          ClosureUtils

           

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

                  }

              }

          }

           

          以下是運行結果:

           

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

           

          具體用法請參考Javadoc。

          posted on 2006-03-10 23:15 Vincent.Chen 閱讀(232) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 山丹县| 罗定市| 治多县| 韶关市| 广德县| 霞浦县| 集安市| 延庆县| 双流县| 西平县| 阿克| 天津市| 新源县| 化德县| 昆明市| 黑龙江省| 盐山县| 遵义市| 枣庄市| 勃利县| 桑日县| 宁远县| 库车县| 蓬溪县| 宿迁市| 顺昌县| 仙居县| 南华县| 抚州市| 大理市| 临桂县| 澄城县| 乐亭县| 芦溪县| 南澳县| 饶平县| 大庆市| 海林市| 三河市| 黎城县| 九台市|