糊言亂語

          志未半斤, 才無八兩. 有苦有樂, 糊涂過活。
          posts - 25, comments - 7, trackbacks - 0, articles - 42
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          搜索

          •  

          最新評論

          接下來看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í)行哪一個ClosureSwitchClosureSwitchTransformer類似,根據(jù)創(chuàng)建時傳入的Predicate組和Closure組對應(yīng)執(zhí)行;WhileClosure則根據(jù)創(chuàng)建時傳入的Predicate做判斷,如果為true則執(zhí)行Closure,直到Predicate返回false;等等。

           

          具體用法請參考Javadoc
          主站蜘蛛池模板: 桦南县| 油尖旺区| 无极县| 务川| 石阡县| 常熟市| 临高县| 沁阳市| 昌图县| 女性| 河源市| 弥渡县| 宁国市| 新兴县| 伊宁县| 克拉玛依市| 集贤县| 肇州县| 长兴县| 舟山市| 荔波县| 太保市| 洛浦县| 明星| 九龙县| 济宁市| 顺昌县| 故城县| 行唐县| 洛川县| 常德市| 彰武县| 南京市| 昌邑市| 民丰县| 肇源县| 耒阳市| 宣化县| 星子县| 正安县| 治多县|