本文共 4862 字,大约阅读时间需要 16 分钟。
在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常流程执行下去。对于可预知的一些数据上的错误,我们一定要做事前检测和判断,来避免程序流程出错,而不是完全通过错误处理来保证流程正确执行,毕竟错误处理是比较消耗资源的方式。在平常情况下我们对参数的判断都需要自己来逐个写方法判断,代码量不少并且复用性不高,比如总是用if .... else .... 这时,我们可以使用Preconditions的方法做一些基本验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package com.hanchao.test; import com.google.common.base.Preconditions; /** * 简化我们的参数验证的测试 * @author liweihan * */ public class PreconditionsTest { public static void main(String[] args) { //1. try { getPersonByPrecondition( 1 , null ); } catch (Exception e) { System.out.println(e.getMessage()); } //2. try { getPersonByPrecondition( 2 , "" ); } catch (Exception e) { System.out.println(e.getMessage()); } try { getPersonByPrecondition(- 2 , "tom" ); } catch (Exception e) { System.out.println(e.getMessage()); } try { getPersonByPrecondition( 1 , "tom3333333333333333333333" ); } catch (Exception e) { System.out.println(e.getMessage()); } getPersonByPrecondition( 2 , "JERRY" ); //3. String name = "name" ; //null; String str = Preconditions.checkNotNull(name); System.out.println(str); } private static void getPersonByPrecondition( int age, String name) { //1.验证是否为null // Preconditions.checkNotNull(name); Preconditions.checkNotNull(name, "name为null" ); //2.验证参数 Preconditions.checkArgument(name.length()> 0 , "name为''" ); Preconditions.checkArgument(age> 0 , "age must > 0" ); Preconditions.checkArgument(age > 0 && name.length() < 10 , "name too lang and max length is " , 10 ); System.out.println( "a person age:" + age + ",neme:" + name); } } |
对一些方法的说明:
Preconditions里面的方法:
1 .checkArgument(boolean) :
功能描述:检查boolean是否为真。 用作方法中检查参数
失败时抛出的异常类型: IllegalArgumentException
2.checkNotNull(T):
功能描述:检查value不为null, 直接返回value;
失败时抛出的异常类型:NullPointerException
3.checkState(boolean):
功能描述:检查对象的一些状态,不依赖方法参数。 例如, Iterator可以用来next是否在remove之前被调用。
失败时抛出的异常类型:IllegalStateException
4.checkElementIndex(int index, int size):
功能描述:检查index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
失败时抛出的异常类型:IndexOutOfBoundsException
5.checkPositionIndex(int index, int size):
功能描述:检查位置index是否为在一个长度为size的list, string或array合法的范围。 index的范围区间是[0, size)(包含0不包含size)。无需直接传入list, string或array, 只需传入大小。返回index。
失败时抛出的异常类型:IndexOutOfBoundsException
6.checkPositionIndexes(int start, int end, int size):
功能描述:检查[start, end)是一个长度为size的list, string或array合法的范围子集。伴随着错误信息。
失败时抛出的异常类型:IndexOutOfBoundsException
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | package com.hanchao.test; import java.util.ArrayList; import java.util.List; import com.google.common.base.Preconditions; /** * 简化我们的参数验证的测试2 * @author liweihan * */ public class PreconditionsTest2 { public static void main(String[] args) { //1. List<Integer> intList = new ArrayList<Integer>(); for ( int i = 0 ; i < 10 ; i++) { try { checkState(intList, 9 ); intList.add(i); } catch (Exception e) { System.out.println(e.getMessage()); } } //2. try { checkPositionIndex(intList, 3 ); } catch (Exception e) { System.out.println(e.getMessage()); } try { checkPositionIndex(intList, 13 ); } catch (Exception e) { System.out.println(e.getMessage()); } //3. try { checkPositionIndexes(intList, 3 , 7 ); } catch (Exception e) { System.out.println(e.getMessage()); } try { checkPositionIndexes(intList, 3 , 17 ); } catch (Exception e) { System.out.println(e.getMessage()); } try { checkPositionIndexes(intList, 13 , 17 ); } catch (Exception e) { System.out.println(e.getMessage()); } //4. try { checkElementIndex(intList, 6 ); } catch (Exception e) { System.out.println(e.getMessage()); } try { checkElementIndex(intList, 16 ); } catch (Exception e) { System.out.println(e.getMessage()); } } //4. private static void checkElementIndex(List<Integer> intList, int index) throws Exception{ Preconditions.checkElementIndex(index, intList.size(), "index:" +index+ "不在list中,list的size为:" +intList.size()); } //3. private static void checkPositionIndexes(List<Integer> intList, int start, int end) throws Exception{ Preconditions.checkPositionIndexes(start, end, intList.size()); } //2. private static void checkPositionIndex(List<Integer> intList, int index) { Preconditions.checkPositionIndex(index, intList.size(), "index:" +index+ "不在list中,list的size为:" +intList.size()); } //1. private static void checkState(List<Integer> intList, int index) { //表达式为True时不抛出异常 Preconditions.checkState(intList.size()<index, "intList size 不能大于" +index); } } |