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)

实例

public static void testPreconditions(){
        // ====都是快速失败,直接抛出异常
        Integer i1 = 2;
        // 检查参数
        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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,299评论 3 20
  • 爱是一件衣服,裤子,袜子头发爱是红薯,爱是三餐,爱是一个学校,爱是一群伙伴,爱是一群教者,爱是一个里程,五公里外的...
    笑颜于粤阅读 172评论 0 0
  • 在下雨天、深夜时,我总是莫名的郁闷,低沉,从一件件小事,到未来种种,似乎都成了灰色,无力,无趣,不仅是事情还是人,...
    苔上青阅读 160评论 0 0
  • 文|王不二 出水芙蓉 一半在泥里,一半在蓝天里 苟且着却不忘努力生长 那连着花苞和须根的干,透过清水看到浮动的蓝天...
    旅人还有平凡阅读 312评论 0 25