美文网首页我爱编程
Guava——Preconditions

Guava——Preconditions

作者: jiangmo | 来源:发表于2018-04-04 10:57 被阅读17次

概述

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

相关文章

  • guava && lamba

    guava: Preconditions.checkArgument(AppUtil.notNullOrDefau...

  • Guava——Preconditions

    概述 Guava provides a number of precondition checking utili...

  • Guava——Preconditions

    Preconditions作为Guava中的异常的前置检查,提供了一系列方法。用于API实现者检查API参数的合法...

  • guava中Preconditions.checkState N

    前言 项目中遇到guava中Preconditions.checkState NoSuchMethod 的报错...

  • 002 Guava | Utilities

    本节介绍Guava的utilities中的工具包, Joiner Spliter Preconditions Ob...

  • Google Guava之Preconditions

    Preconditions比较简单的前置条件:让方法调用的前置条件判断更简单。 准备工作 官方仓库:https:/...

  • Guava-Preconditions & Strings

    Preconditions 用来做断言的,可以使代码变得优雅,不需要通过if去判断,并且还能抛出自定义内容的异常c...

  • guava前置条件Preconditions类

    简介 Preconditions可以快速有效简洁的完成参数校验,避免我们在代码中写很多if语句,guava将所有检...

  • 【Guava】PreConditions来校验参数

    前置条件:让方法调用的前置条件判断更简单。 在我们的日常开发中,经常要对入参进行一定的参数校验,比如是否为空,参数...

  • Preconditions的用法

    Google Guava中提供了一个Preconditions类,用于校验入参的正确性 源码分析: 示例如下:示例...

网友评论

    本文标题:Guava——Preconditions

    本文链接:https://www.haomeiwen.com/subject/zizhhftx.html