Google Guava学习笔记——基础工具类Preconditions类的使用
Preconditions类是一组静态方法用来验证我们代码的状态。Preconditons类很重要,它能保证我们的代码按照我们期望的执行,如果不是我们期望的,我们会立即得到反馈是哪里出来问题,现在我们使用Preconditions来保证我们代码的行为,并且对调试也非常方便。
当然,你也可以自己写预处理的代码,就像下面一样:
if (someObj == null) {
throw new IllegalArgumentException(" someObj must not be null");
}
但使用Preconditions类来处理参数为空的情况,会更加清晰可读:
Preconditions.checkNotNull(label, "Label can't not be null");
下面的类演示了Preconditions类的使用:
package com.guava.felix;
import com.google.common.base.Preconditions;
public class PreconditionsDemo {
private String label;
private int[] values = new int[5];
private int currentIndex;
public PreconditionsDemo(String label) {
this.label = Preconditions.checkNotNull(label, "Label can't not be null");
}
public void updateCurrentIndexValue(int index, int valueToSet) {
// check index valid first
this.currentIndex = Preconditions.checkElementIndex(index, values.length, "Index out of bounds for values.");
// validate valueToSet
Preconditions.checkArgument(valueToSet <= 100, "Value can't be more than 100");
values[this.currentIndex] = valueToSet;
}
public void doOperation() {
Preconditions.checkState(validateObjectState(), "Can't perform operation.");
}
private boolean validateObjectState() {
return this.label.equalsIgnoreCase("open") && values[this.currentIndex] == 10;
}
public static void main(String[] args) {
PreconditionsDemo demo = new PreconditionsDemo("open");
demo.updateCurrentIndexValue(4, 10);
demo.doOperation();
}
}
下面是例子中四个方法的总结:
checkNotNull(T object, Object message): object对象不为空的话返回,否则抛出异常。
checkElementIndex(int index, int size, Object message): index表示你要访问的数组,集合,字符串的位置,size表示的是长度,如果 index是有效的则返回,否则抛出异常;
checkArgument(Boolean expression, Object message):这个方法根据传递进来的参数是否满足方法中的逻辑判断,如果符合,返回true,否则抛出异常。
checkStatus(Boolean expression, Object message):这个方法没有参数,它判断一个对象的状态,期望返回true,否则抛出异常。
Google Guava学习笔记——基础工具类Preconditions类的使用的更多相关文章
- Google Guava学习笔记——基础工具类Joiner的使用
Guava 中有一些基础的工具类,如下所列: 1,Joiner 类:根据给定的分隔符把字符串连接到一起.MapJoiner 执行相同的操作,但是针对 Map 的 key 和 value. 2,Spli ...
- Google Guava学习笔记——基础工具类String处理类的使用
不管你喜欢何种编程语言,很多时候针对string编程的处理都是乏味而且爱出错误的,很多时候,我们需要从文件或是数据库中读取数据,或者根据需求重新格式化或排序字符串给用户显示.幸运的是,Guava提供了 ...
- Google Guava学习笔记——基础工具类针对Object类的使用
Guava 提供了一系列针对Object操作的方法. 1. toString方法 为了方便调试重写toString()方法是很有必要的,但写起来比较无聊,不管如何,Objects类提供了toStrin ...
- Google Guava学习笔记——基础工具类Splitter的使用
另一项经常对字符串的操作就是根据指定的分隔符对字符串进行分隔.我们基本上会使用String.split方法: String testString = "Monday,Tuesday,,Thu ...
- Google Guava学习笔记——简介
Google Guava是什么东西?首先要追溯到2007年的“Google Collections Library”项目,它提供对Java 集合操作的工具类.后来Guava被进化为Java程序员开发必 ...
- Guava学习笔记(2):Preconditions优雅的检验参数
转自:http://www.cnblogs.com/peida/p/Guava_Preconditions.html 在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按 ...
- Guava学习笔记:Preconditions优雅的检验参数
在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常流程执行下去.对于可预知的一些数据上的错误,我们一定要做事前检测和判断,来避免程序流程出错,而不是完全通过错误处 ...
- Guava学习笔记:Preconditions优雅的检验参数(java)
http://www.cnblogs.com/peida/p/guava_preconditions.html 在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常 ...
- Guava学习笔记目录
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
随机推荐
- ubuntu下使用nethogs监控网络流量
NetHogs是一款小巧免费的开源命令行工具,用来按进程或程序实时统计网络带宽使用率. 对于使用类似于“repo tool”.“depot_tools”等工具checkout源码时非常有用,可以查看当 ...
- 记一下ajax里get与post的异同
以下均为个人笔记,如有错误,欢迎指正 get与post在写法上来讲基本上差不多 $.ajax({ type:"get",//get data:data, success:funct ...
- spring 知识梳理
https://github.com/spring-projects/spring-framework spring github地址
- while循环中不支持循环使用curl
<?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...
- 【Window】Tor(洋葱头路由)+Privoxy 网络实践(附带Java实例代码)
1.背景 平时我们需要访问onion后缀的网站,需要通过Tor(The Onion Router,洋葱路由器).一般来说安装Tor Broswer就可以满足需要.但是项目我要做的是通过程序来获取oni ...
- VPN连接失败
连接VPN是总提示 本来我以为是VPN服务器的问题,可是别人就能连接成功,所以只能是我自己机子的问题.后来检查了一下连接属性,终于发现了问题: 这里“允许使用这些协议”应该是处于选中状态,而我的属性里 ...
- 简单翻译和补充:1. GNU ARM Eclipse
原文链接: GNU ARM Eclipse GNU 介绍: GNU 计划,又称革奴计划,是由RichardStallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.Ric ...
- Microsoft Azure 的一些限制 Global
Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...
- poj 3061 Subsequence
题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...
- Amazon Kindle Device is hiring in Beijing Shanghai and Shenzhen!
This is Angela from recruitment team of Amazon Kindle Device Software & Applications, we are exp ...