guava学习笔记(1)--参数检验
来源:互联网 发布:路由器,mac在哪里 编辑:程序博客网 时间:2024/05/07 06:15
1.PreCondition中提供的参数检验方式,让你的代码变得优雅
PreCondition中的主要方法如下:
Methods Modifier and Type Method and Description static void
checkArgument(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.static void
checkArgument(boolean expression,Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.static void
checkArgument(boolean expression,String errorMessageTemplate, Object... errorMessageArgs)
Ensures the truth of an expression involving one or more parameters to the calling method.static int
checkElementIndex(int index, int size)
Ensures thatindex
specifies a valid element in an array, list or string of sizesize
.static int
checkElementIndex(int index, int size,String desc)
Ensures thatindex
specifies a valid element in an array, list or string of sizesize
.static <T> T
checkNotNull(T reference)
Ensures that an object reference passed as a parameter to the calling method is not null.static <T> T
checkNotNull(T reference,Object errorMessage)
Ensures that an object reference passed as a parameter to the calling method is not null.static <T> T
checkNotNull(T reference,String errorMessageTemplate, Object... errorMessageArgs)
Ensures that an object reference passed as a parameter to the calling method is not null.static int
checkPositionIndex(int index, int size)
Ensures thatindex
specifies a valid position in an array, list or string of sizesize
.static int
checkPositionIndex(int index, int size,String desc)
Ensures thatindex
specifies a valid position in an array, list or string of sizesize
.static void
checkPositionIndexes(int start, int end, int size)
Ensures thatstart
andend
specify a validpositions in an array, list or string of sizesize
, and are in order.static void
checkState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.static void
checkState(boolean expression,Object errorMessage)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.static void
checkState(boolean expression,String errorMessageTemplate, Object... errorMessageArgs)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
Demo1:
package org.sh.demo01;import java.util.List;import org.junit.Test;import com.google.common.base.Preconditions;public class GuavaDemo01 {@Testpublic void Preconditions(){try {getObjectPreConditions(8,"suhao");} catch (Exception e) {System.out.println(e.getMessage());}try {getObjectPreConditions(8,null);} catch (Exception e) {System.out.println(e.getMessage());}try {getObjectPreConditions(8,"");} catch (Exception e) {System.out.println(e.getMessage());}try {getObjectPreConditions(-1,"suhao");} catch (Exception e) {System.out.println(e.getMessage());}}public void getObjectPreConditions(int age ,String name) throws Exception{Preconditions.checkNotNull(name,"name must not be null");Preconditions.checkArgument(name.length()>0,"name 为\'\'");Preconditions.checkArgument(age>0,"age must rl 0");System.out.println("name="+name+",age="+age);}}
Guava的preconditions有这样几个优点:
在静态导入后, 方法很明确无歧义, checkNotNull可以清楚地告诉你它是干什么的, 它会抛出怎样的异常.
checkNotNull在验证通过后直接返回, 可以这样方便地写代码: this.field = checkNotNull(field).
简单而又强大的可变参数'printf'风格的自定义错误信息.
0 0
- guava学习笔记(1)--参数检验
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记【3】:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Guava学习笔记:Preconditions优雅的检验参数
- Java-类库-Guava-Preconditions检验参数
- Preconditions优雅的检验参数(Guava)
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- 云时代,你的安全谁负责
- halcon自动全局阈值与动态阈值分割方法
- $this = this;
- asmlinkage
- Android Notification的作用和用法
- guava学习笔记(1)--参数检验
- 二维码简单生成
- 泛型详解
- STL概述
- 组件和控件
- HTC王雪红:风雨彩虹,铿锵玫瑰
- kbhit函数(讲解)
- OSGI中的service依赖关系管理
- lua语法