Guava——Preconditions

概述

Guava provides a number of precondition checking utilities. We strongly recommend importing these statically.

Each method has three variants:

  • No extra arguments. Any exceptions are thrown without error messages.
  • An extra Object argument. Any exception is thrown with the error message object.toString().
  • An extra String argument, with an arbitrary number of additional Object arguments. This behaves something like printf, but for GWT compatibility and efficiency, it only allows %s indicators.

Note: checkNotNull, checkArgument and checkState have a large number of overloads taking combinations of primitive and Object parameters rather than a varargs array — this allows calls such as those above to avoid both primitive boxing and varags array allocation in the vast majority of cases.

使用建議

We recommend that you split up preconditions into distinct lines, which can help you figure out which precondition failed while debugging. Additionally, you should provide helpful error messages, which is easier when each check is on its own line.

We preferred rolling our own preconditions checks over e.g. the comparable utilities from Apache Commons for a few reasons. Briefly:

  • After static imports, the Guava methods are clear and unambiguous. checkNotNull makes it clear what is being done, and what exception will be thrown.
  • checkNotNull returns its argument after validation, allowing simple one-liners in constructors: this.field = checkNotNull(field);.
  • Simple, varargs "printf-style" exception messages. (This advantage is also why we recommend continuing to use checkNotNull over Objects.requireNonNull)

實(shí)例

public static void testPreconditions(){
        // ====都是快速失敗顶猜,直接拋出異常
        Integer i1 = 2;
        // 檢查參數(shù)
        Preconditions.checkArgument(i1>1);
        Preconditions.checkArgument(i1>1, "ok");

        String s1 = "s1";
        // 是否空
        s1 = Preconditions.checkNotNull(s1, "%s is null", s1);

        // Ensures the truth of an expression involving the state of the calling instance
        Preconditions.checkState(s1 != null);

        List<Integer> list1 = Arrays.asList(1, 2, 3,4);
        // 檢查元素index是否越界
        Preconditions.checkElementIndex(2, list1.size());
        // 檢查position是否越界
        Preconditions.checkPositionIndex(1, list1.size());
        // 檢查[start, end]是否越界
        Preconditions.checkPositionIndexes(1, 3, list1.size());
    }

Ref:
https://github.com/google/guava/wiki/PreconditionsExplained

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市赊窥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異佑笋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)斑鼻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進(jìn)店門蒋纬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事颠锉》ㄉ” “怎么了史汗?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵琼掠,是天一觀的道長。 經(jīng)常有香客問我停撞,道長瓷蛙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任戈毒,我火速辦了婚禮艰猬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘埋市。我一直安慰自己冠桃,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布道宅。 她就那樣靜靜地躺著食听,像睡著了一般。 火紅的嫁衣襯著肌膚如雪污茵。 梳的紋絲不亂的頭發(fā)上樱报,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天,我揣著相機(jī)與錄音泞当,去河邊找鬼迹蛤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛襟士,可吹牛的內(nèi)容都是我干的盗飒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼陋桂,長吁一口氣:“原來是場噩夢啊……” “哼箩兽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起章喉,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤汗贫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后秸脱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體落包,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年摊唇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了咐蝇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡巷查,死狀恐怖有序,靈堂內(nèi)的尸體忽然破棺而出抹腿,到底是詐尸還是另有隱情,我是刑警寧澤旭寿,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布警绩,位于F島的核電站,受9級特大地震影響盅称,放射性物質(zhì)發(fā)生泄漏肩祥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一缩膝、第九天 我趴在偏房一處隱蔽的房頂上張望混狠。 院中可真熱鬧,春花似錦疾层、人聲如沸将饺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽予弧。三九已至,卻和暖如春舅逸,著一層夾襖步出監(jiān)牢的瞬間桌肴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工琉历, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坠七,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓旗笔,卻偏偏與公主長得像彪置,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蝇恶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,324評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,060評論 3 20
  • 愛是一件衣服拳魁,褲子,襪子頭發(fā)愛是紅薯撮弧,愛是三餐潘懊,愛是一個(gè)學(xué)校,愛是一群伙伴贿衍,愛是一群教者授舟,愛是一個(gè)里程,五公里外的...
    笑顏于粵閱讀 166評論 0 0
  • 在下雨天贸辈、深夜時(shí)释树,我總是莫名的郁悶,低沉,從一件件小事奢啥,到未來種種秸仙,似乎都成了灰色,無力桩盲,無趣寂纪,不僅是事情還是人,...
    苔上青閱讀 152評論 0 0
  • 文|王不二 出水芙蓉 一半在泥里正驻,一半在藍(lán)天里 茍且著卻不忘努力生長 那連著花苞和須根的干弊攘,透過清水看到浮動的藍(lán)天...
    旅人還有平凡閱讀 291評論 0 25