Using Guava's precondition checking utilities, explained. 
explained

Updated Apr 23, 2012 by wasserman.louis

Preconditions

Guava provides a number of precondition checking utilities. We strongly recommend importing these statically. (How to do this easily in Eclipse.)

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
checkArgument(boolean)

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

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

IllegalArgumentException
checkNotNull(T)

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

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

NullPointerException
checkState(boolean)

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
checkElementIndex(int index, int size)

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
checkPositionIndex(int index, int size)

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
checkPositionIndexes(int start, int end, int size)

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 discusses 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 overObjects.requireNonNull 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在他自己的行上时更容易确认错误所在.

Chapter 2 -- Preconditions的更多相关文章

  1. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

  2. Chapter 20: Diagnostics

    WHAT'S IN THIS CHAPTER?n Code contractsn Tracingn Event loggingn Performance monitoringWROX.COM CODE ...

  3. Guava学习笔记(2):Preconditions优雅的检验参数

    转自:http://www.cnblogs.com/peida/p/Guava_Preconditions.html 在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按 ...

  4. guava学习--Preconditions

    转载:https://my.oschina.net/realfighter/blog/349819 Preconditions是guava提供的用于进行代码校验的工具类,其中提供了许多重要的静态校验方 ...

  5. Chapter 4: Troubleshoot and debug web applications

    Prevent and troubleshoot runtime issues Troubleshooting performance, security and errors using perfo ...

  6. Modern C++ CHAPTER 2(读书笔记)

    CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...

  7. Guava学习笔记:Preconditions优雅的检验参数

    在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常流程执行下去.对于可预知的一些数据上的错误,我们一定要做事前检测和判断,来避免程序流程出错,而不是完全通过错误处 ...

  8. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper

    前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝 ...

  9. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- Messenger

    Messenger类实际是对Aidl方式的一层封装.本文只是对如何在Service中使用Messenger类实现与客户端的通信进行讲解,对Messenger的底层不做说明.阅读Android Prog ...

随机推荐

  1. Machine Schedule HDU1150

    有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则机器A需要设置为模式yi. ...

  2. SpringBoot属性配置

    一:url的配置 1.配置 默认配置文件是application.properties 2.配置 配置端口 配置context path 3.启动效果 4.第二种配置方式 要先删除applicatio ...

  3. odoo导入功能二开

    原来有的导入功能相信很多小伙伴对其功能不是很满意,不过没关系,我们可以二开啊,把它的功能改造成你想要的样子,接下来让我们看看怎么办吧 例如我想把员工导入功能中添加上用户同步注册功能 首先,我要找到原模 ...

  4. windows10 下安装tensorflow 并且在jupyter notebook 上使用tensorflow

    一.安装jupyter notebook并配置环境 首先建议大家安装anaconda,最新版本请到官网下载(点击下载连接),没错,直接点击下载python3.6版本的(当然选择做自己电脑相应的位数,我 ...

  5. Android-认识Service

    Android-认识Service 学习自 郭霖的博客 https://developer.android.google.cn/reference/android/app/Service#WhatIs ...

  6. BZOJ.4514.[SDOI2016]数字配对(费用流SPFA 二分图)

    BZOJ 洛谷 \(Solution\) 很显然的建二分图后跑最大费用流,但有个问题是一个数是只能用一次的,这样二分图两部分都有这个数. 那么就用两倍的.如果\(i\)可以向\(j'\)连边,\(j\ ...

  7. NOI.AC NOIP模拟赛 第五场 游记

    NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...

  8. 【Go命令教程】12. go tool pprof

    我们可以使用 go tool pprof 命令来交互式的访问概要文件的内容.命令将会分析指定的概要文件,并会根据我们的要求为我们提供高可读性的输出信息. 在 Go 语言中,我们可以通过标准库的代码包 ...

  9. 液晶电视插有线电视信号线的是哪个接口 HDMI是什么接口

    1.液晶电视插有线电视信号线的接口(模拟信号)是射频接口(也叫RF接口,同轴电缆接口,闭路线接口),数字信号就得通过机顶盒转换成模拟信号视频输出至电视,才能正常收看电视节目. 2.电视机或高清机顶盒上 ...

  10. C# Windows服务开发从入门到精通

    一.课程介绍 大家都知道如果想要程序一直运行在windows服务器上,最好是把程序写成windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需要用户直接登录,直接开机就可以启动 ...