学生教材网 >程序设计 > 代码分享 > Java > 浏览文章

guava 前置条件 Preconditions

来源:网络编辑:佚名时间:2016-01-06人气:

薄氏家书,witkin 大学,十殿阎罗王

<无详细内容> 标签: <无>

代码片段(1) [全屏查看所有代码]

1. [代码][Java]代码     跳至 [1] [全屏预览]

/**
 * Created by lgq on 16-1-5.
 */
public class TestPreconditions {

    /**
     * 前置条件检查,当条件不满足时,就会抛出异常
     */
    @Test
    public void testPreconditions(){
        String constantName = null;
        //Preconditions.checkNotNull(constantName,"constantName 为 null");

        String str = "abc";
        Preconditions.checkNotNull(str,"str 为 null"); // 不会输出

        int age = 23;
        Integer number = 12;
        //Preconditions.checkArgument(age < 20, "age 必须要< 20");
        Preconditions.checkArgument(number < 20, "number 必须要< 20");


        List<Integer> integerList = Lists.newArrayList(3,5,6,98,2,45);
        for(int i=0; i<integerList.size(); i++) {
            // 检查数组下标是否越界
            int passIndex = Preconditions.checkElementIndex(i, integerList.size());
            //System.out.println("下标:"+ passIndex + " ==> 满足要求");
        }

        // 检查下标值7 是否在集合integerList中
        //Preconditions.checkElementIndex(7, integerList.size());

        //Preconditions.checkPositionIndex(8, integerList.size());

        Preconditions.checkPositionIndexes(2, 4, integerList.size());

        // 判断是否为true,当为false时,会抛出IllegalStateException
        Preconditions.checkState(4 < 3);

    }

}

guava MultiMap demo

<无详细内容>标签:<无>-->-->-->1.[代码][Java]代码/***Createdbylgqon16-1-6.

guava MultiSet demo

<无详细内容>标签:<无>-->-->-->1.[代码][Java]代码/***Createdbylgqon16-1-6.

热门推荐