博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Chapter 2 -- Preconditions
阅读量:6961 次
发布时间:2019-06-27

本文共 3898 字,大约阅读时间需要 12 分钟。

Using Guava's precondition checking utilities, explained. 
Updated 
Apr 23, 2012 by 

Preconditions

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

Guava提供了一系列precondition检查工具. 我们强烈推荐你静态引入这些工具

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().
    • 有一个额外的Object参数版本.异常抛出时会附带一个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. Example:
    • 有一个额外的String参数和任意个Object参数版本. 这种表现类似于printf, 但是为了GWT的兼容性和效率,他仅仅支持 s% 标识符. 例子:

 

checkArgument(i >=0,"Argument was %s but expected nonnegative", i); checkArgument(i < j,"Expected i < j, but %s > %s", i, j);

 

Signature (not including extra args) Description Exception thrown on failure

Checks that the boolean is true. Use for validating arguments to methods.

检查boolean是否是true. 用来验证方法参数

IllegalArgumentException

Checks that the value is not null. Returns the value directly, so you can use checkNotNull(value) inline.

检查值是否是null. 方法直接这个值,所以你可以直接在行内使用checkNotNull(value)

NullPointerException

Checks some state of the object, not dependent on the method arguments. For example, an Iterator might use this to check that next has been called before any call to remove.

检查对象的状态, 它不依赖于方法参数. 例如, Iterator可能会用这个方法来检查next是否在remove之前调用

IllegalStateException

Checks that index is a valid element index into a list, string, or array with the specified size. An element index may range from 0 inclusive to size exclusive. You don't pass the list, string, or array directly; you just pass its size.

检查index是否是一个合法的长度为size的list,string,array的element index.element index的范围从0(包括)到size(不包括).你不需要直接传list,string,array,只要传他们的ize

Returns index.

返回index

IndexOutOfBoundsException

Checks that index is a valid position index into a list, string, or array with the specified size. A position index may range from 0 inclusive to size inclusive. You don't pass the list, string, or array directly; you just pass its size.

检查index是否是一个合法的长度为size的list.string.array的position index.position index的范围从0(包括)到size(包括).你不需要直接传list,string,array,只需要传他们的size

Returns index.

IndexOutOfBoundsException

Checks that [start, end) is a valid sub range of a list, string, or array with the specified size. Comes with its own error message.

检查[start, end)是否是一个长度为size的list,string,array的子范围.

IndexOutOfBoundsException

 

We preferred rolling our own preconditions checks over e.g. the comparable utilities from Apache Commons for a few reasons. Piotr Jagielski  why he prefers our utilities, but briefly:

因为某些原因,我们更建议使用我们的preconditions而不是使用Apache Commons 的 comparable工具. Piotr Jagielski  为什么他愿意这么做, 简要的说一下原因:

  • After static imports, the Guava methods are clear and unambiguous. checkNotNull makes it clear what is being done, and what exception will be thrown.
  • 通过静态引入, Guava方法更清晰且没有歧义. checkNotNull 可以清楚的知道方法将会做什么, 它会抛出什么异常.
  • checkNotNull returns its argument after validation, allowing simple one-liners in constructors: this.field = checkNotNull(field).
  • checkNotNull 在验证完后返回它的参数, 允许使用简单的行内写法: this.field = checkNotNull(field).
  • Simple, varargs "printf-style" exception messages. (This advantage is also why we recommend continuing to use checkNotNull over introduced in JDK 7.)
  • 简单的可变参式"printf-style"异常信息. (这个有点也是我们为什么会在JDK7的Objects.requireNonNull的介绍中推荐使用checkNotNull的原因)

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.

我们推荐你切分preconditions到不同的行中,这样可以帮助你在debug时找出哪个precondition失败了. 另外, 你应该提供有用的错误信息, 这样当每个check在他自己的行上时更容易确认错误所在.

转载地址:http://ovgil.baihongyu.com/

你可能感兴趣的文章
大快DKhadoop大数据处理平台详解
查看>>
摄影菜鸟使用的相机镜头术语大全分享
查看>>
XenServer部署系列之06——网络配置
查看>>
Python黑科技:50行代码运用Python+OpenCV实现人脸追踪+详细教程+快速入门+图像识...
查看>>
软件测试质量和效率评价之我见
查看>>
kloxo增加了域名,怎么不能访问?如何重启web服务?
查看>>
Nginx调试入门
查看>>
Centos7安装jdk
查看>>
MySQL锁
查看>>
国学题库整理
查看>>
jquery chosen 插件 动态设置+更新选项值
查看>>
求最大值及其下标
查看>>
战力会议1
查看>>
水印情缘。。。
查看>>
为什么重写equals一定要重写hashCode?
查看>>
HDU Problem 4006 The kth great number 【队列】
查看>>
win8阉割版中文输入法
查看>>
Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)
查看>>
以Drools5.5为例说明“规则引擎在业务系统中应用”---起始篇
查看>>
linux清理内存
查看>>