Guava入门第三章(Preconditions)
Preconditions详细介绍
package com.lvshihao.guava;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
/**
*@author: LVSHIHAO
*@description: GUAVA Preconditions detailed introduction
*/
public class PreconditionsTest {
@Test(expected = NullPointerException.class)
public void testCheckNotNull(){
checkNotNull(null);
}
@Test
public void testCheckNotNullWithMessage(){
try{
checkNotNullWithMessage(null);
}catch (Exception e){
assertThat(e,is(NullPointerException.class));
assertThat(e.getMessage(),equalTo("The list should not be null"));
}
}
@Test
public void testCheckNotNullWithFormatMessage(){
try{
checkNotNullWithFormatMessage(null);
}catch (Exception e){
assertThat(e,is(NullPointerException.class));
assertThat(e.getMessage(),equalTo("The list should not be null and the size must be 2"));
}
}
@Test
public void testCheckArguments(){
/**
* Preconditions.checkArgument(); 检查传入的参数是否合法,false 抛出IllegalArgumentException异常
* Check whether the passed parameter is legitimate , false throws IllegalArgumentException
*/
try{
final String type ="A";
Preconditions.checkArgument(type.equals("B"));
}catch (Exception e){
assertThat(e,is(IllegalArgumentException.class));
}
}
@Test
public void testCheckState(){
/**
* Preconditions.checkArgument(); 检查传入的状态是否合法,false IllegalStateException
* Check whether the incoming state is legal, false throws IllegalStateException
*/
try{
final String state ="A";
Preconditions.checkState(state.equals("B"),"The state is illegal.");
}catch (Exception e){
assertThat(e,is(IllegalStateException.class));
assertThat(e.getMessage(),equalTo("The state is illegal."));
}
}
@Test
public void testCheckIndex(){
/**
* Preconditions.checkElementIndex(); 检查传入的Index是否越界,如果比传入集合的下标大就会抛出 IndexOutOfBoundsException
* CCheck whether the incoming Index is out of bounds, if it is larger than the subscript of the incoming collection, it will be thrown IndexOutOfBoundsException
*/
try{
ImmutableList<Object> list = ImmutableList.of("2");
Preconditions.checkElementIndex(0,list.size());
}catch (Exception e){
assertThat(e,is(IndexOutOfBoundsException.class));
}
}
private void checkNotNull(final List<String> list){
/**
* Preconditions.checkNotNull() 检查是否为null,如果为null 抛出NullPointerException异常,否则返回数据
* checkout is not null if is null throw NullPointerException Exception else return data
*/
Preconditions.checkNotNull(list);
}
private void checkNotNullWithMessage(final List<String> list){
/**
* 传入Message异常信息 incoming Exception Message
*/
Preconditions.checkNotNull(list,"The list should not be null");
}
private void checkNotNullWithFormatMessage(final List<String> list){
/**
* 格式化异常信息 format Exception Message
*/
Preconditions.checkNotNull(list,"The list should not be null and the size must be %s",2);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:吕世昊
个性签名:学习如逆水行舟,不进则退!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!