Vincent.Chan‘s Blog

          常用鏈接

          統計

          積分與排名

          網站

          最新評論

          [Jakarta Commons筆記] Commons Collections - Predicate組

          接下來看Predicate

           

          Predicate

          AndPredicate

          OrPredicate

          AllPredicate

          OnePredicate

          NonePredicate

          PredicateUtils

           

          PredicateCommons Collections中定義的一個接口,可以在org.apache.commons.collections包中找到。其中定義的方法簽名如下:

           

          public boolean evaluate(Object object)

           

          它以一個Object對象為參數,處理后返回一個boolean值,檢驗某個對象是否滿足某個條件。其實這個Predicate以及上一篇筆記提到的Comparator還有我們即將看到的TransformerClosure等都有些類似C/C++中的函數指針,它們都只是提供簡單而明確定義的函數功能而已。

           

          跟其他組類似,Commons Collections也提供了一組定義好的Predicate類供我們使用,這些類都放在org.apache.commons.collections.functors包中。當然,我們也可以自定義Predicate,只要實現這個Predicate接口即可。在Commons Collections中我們也可以很方便使用的一組預定義復合Predicate,我們提供2個或不定數量個Predicate,然后交給它,它可以幫我們處理額外的邏輯,如AndPredicate處理兩個Predicate,只有當兩者都返回true它才返回trueAnyPredicate處理多個Predicate,當其中一個滿足就返回true,等等。

           

          看看具體的代碼中如何使用這些Predicate吧:

           

          package sean.study.commons.collections;

           

          import org.apache.commons.collections.Predicate;

          import org.apache.commons.collections.PredicateUtils;

          import org.apache.commons.collections.functors.InstanceofPredicate;

          import org.apache.commons.collections.functors.NotNullPredicate;

          import org.apache.commons.lang.BooleanUtils;

          import org.apache.commons.lang.StringUtils;

           

          public class PredicateUsage {

           

              public static void main(String[] args) {

                  demoPredicates();

              }

             

              public static void demoPredicates() {

                  System.out.println(StringUtils.center(" demoPredicates ", 40, "="));

                  Predicate p1 = new InstanceofPredicate(String.class);

                  Predicate p2 = NotNullPredicate.getInstance();

                  Predicate p3 = new Predicate() {

                      public boolean evaluate(Object obj) {

                          String str = (String) obj;

                          return StringUtils.isAlphanumeric(str)

                              && str.length() >= 6

                              && str.length() <= 10;

                      }

                  };

                  Predicate p4 = PredicateUtils.allPredicate(new Predicate[]{p1, p2, p3});                String input = "ABCD1234";

                  Object[] raw = new Object[] {

                      "Is '",

                      input,

                      "' a valid input? ",

                      BooleanUtils.toStringYesNo(p4.evaluate(input)),

                      "."

                  };

                  System.out.println(StringUtils.join(raw));

                  System.out.println(StringUtils.repeat("=", 40));

              }

           

          }

           

          輸出結果如下:

           

          ============ demoPredicates ============

          Is 'ABCD1234' a valid input? yes.

          ========================================

           

          這里面我首先定義了3個簡單的Predicatep1判斷對象是否為String的實例,p2判斷是否對象為nullp3是自定義的,判斷是否為數字字母的組合,并且長度在6~10字符。然后我用AllPredicate將它們組合到一起,成為p4,當p1p2p3都滿足時,p4evaluate方法才返回true。利用Predicate我們可以把判斷truefalse的邏輯從特定的業務代碼分離出來,以后我們重用也好,重新組裝也好,都是很方便的。

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

          主站蜘蛛池模板: 察雅县| 友谊县| 青海省| 信丰县| 广昌县| 天峨县| 石柱| 浙江省| 青海省| 徐水县| 义乌市| 富顺县| 眉山市| 聂拉木县| 合作市| 格尔木市| 车险| 马关县| 泉州市| 乐安县| 北京市| 泽库县| 新乡市| 青冈县| 肥东县| 库伦旗| 镇巴县| 江阴市| 文水县| 呈贡县| 台中市| 张家界市| 报价| 亳州市| 通城县| 稻城县| 武威市| 平安县| 曲松县| 子洲县| 噶尔县|