guava学习笔记(1)--参数检验

1.PreCondition中提供的参数检验方式,让你的代码变得优雅

PreCondition中的主要方法如下:

  • Methods
    Modifier and TypeMethod and Description
    static voidcheckArgument(boolean expression)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static voidcheckArgument(boolean expression, Object errorMessage)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static voidcheckArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static intcheckElementIndex(int index, int size)
    Ensures that index specifies a valid element in an array, list or string of size size.
    static intcheckElementIndex(int index, int size, String desc)
    Ensures that index specifies a valid element in an array, list or string of size size.
    static <T> TcheckNotNull(T reference)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static <T> TcheckNotNull(T reference, Object errorMessage)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static <T> TcheckNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
    Ensures that an object reference passed as a parameter to the calling method is not null.
    static intcheckPositionIndex(int index, int size)
    Ensures that index specifies a valid position in an array, list or string of size size.
    static intcheckPositionIndex(int index, int size, String desc)
    Ensures that index specifies a valid position in an array, list or string of size size.
    static voidcheckPositionIndexes(int start, int end, int size)
    Ensures that start and end specify a valid positions in an array, list or string of size size, and are in order.
    static voidcheckState(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 voidcheckState(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 voidcheckState(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 {
	@Test
	public 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'风格的自定义错误信息.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值