免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
[Jakarta Commons筆記](méi) Commons Collections -Predicate組

接下來(lái)看Predicate

 

Predicate

AndPredicate

OrPredicate

AllPredicate

OnePredicate

NonePredicate

PredicateUtils

 

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

 

public boolean evaluate(Object object)

 

它以一個(gè)Object對(duì)象為參數(shù),處理后返回一個(gè)boolean值,檢驗(yàn)?zāi)硞€(gè)對(duì)象是否滿足某個(gè)條件。其實(shí)這個(gè)Predicate以及上一篇筆記提到的Comparator還有我們即將看到的TransformerClosure等都有些類似C/C++中的函數(shù)指針,它們都只是提供簡(jiǎn)單而明確定義的函數(shù)功能而已。

 

跟其他組類似,Commons Collections也提供了一組定義好的Predicate類供我們使用,這些類都放在org.apache.commons.collections.functors包中。當(dāng)然,我們也可以自定義Predicate,只要實(shí)現(xiàn)這個(gè)Predicate接口即可。在Commons Collections中我們也可以很方便使用的一組預(yù)定義復(fù)合Predicate,我們提供2個(gè)或不定數(shù)量個(gè)Predicate,然后交給它,它可以幫我們處理額外的邏輯,如AndPredicate處理兩個(gè)Predicate,只有當(dāng)兩者都返回true它才返回true;AnyPredicate處理多個(gè)Predicate,當(dāng)其中一個(gè)滿足就返回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));

    }

 

}

 

輸出結(jié)果如下:

 

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

Is 'ABCD1234' a valid input? yes.

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

 

這里面我首先定義了3個(gè)簡(jiǎn)單的Predicate,p1判斷對(duì)象是否為String的實(shí)例,p2判斷是否對(duì)象為null,p3是自定義的,判斷是否為數(shù)字字母的組合,并且長(zhǎng)度在6~10字符。然后我用AllPredicate將它們組合到一起,成為p4,當(dāng)p1、p2、p3都滿足時(shí),p4evaluate方法才返回true。利用Predicate我們可以把判斷truefalse的邏輯從特定的業(yè)務(wù)代碼分離出來(lái),以后我們重用也好,重新組裝也好,都是很方便的。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
《Jakarta commons cookbook》學(xué)習(xí)筆記
【開(kāi)發(fā)常識(shí)】這個(gè)問(wèn)題,直接導(dǎo)致年終獎(jiǎng)沒(méi)了……(慘兮兮)
Commons-Collections
Matrix - 與 Java 共舞 - 開(kāi)始使用Commons Chain (第一部分)
commons
看例子練apache commons之lang篇
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服