Skip to content

Instantly share code, notes, and snippets.

@YukiYoshikawa
Created May 3, 2013 09:29
Show Gist options
  • Save YukiYoshikawa/5508183 to your computer and use it in GitHub Desktop.
Save YukiYoshikawa/5508183 to your computer and use it in GitHub Desktop.
package trial.yy.guava.client.base;
import com.google.common.base.Preconditions;
/**
* com.google.common.base.Preconditionsを試すためのサンプル
* User: yy
*/
public class PreconditionsClient {
public static void main(String[] args) {
System.out.println("### Preconditions.checkArgument execute.");
function1(1);
try {
function1(-1);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println("### Preconditions.checkNotNull execute.");
function2("Jon");
try {
function2(null);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
/**
* Preconditions.checkArgument で引数チェックを行うメソッド
* @param arg 0以上の値
* @throws IllegalArgumentException 引数に0未満の値を指定した場合
*/
private static void function1(int arg) {
System.out.println("execute function1 start");
/*
* 以下と同等のチェック処理
* if (arg < 0) {
 * throw new IllegalArgumentException("引数は0以上の値を指定してください。 [arg: " + arg + "]");
* }
*/
Preconditions.checkArgument(arg >= 0, "引数は0以上の値を指定してください。 [arg: %s]", arg);
System.out.println("execute function1 [argument: " + arg + "]");
System.out.println("execute function1 end");
}
/**
* Preconditions.checkNotNull で引数チェックを行うメソッド
* @param arg 何らかの文字列
* @throws NullPointerException 引数がnullの場合
*/
private static void function2(String arg) {
System.out.println("execute function2 start");
/*
* 以下と同等のチェック処理
* if (arg == null) {
 * throw new NullPointerException("引数がnullです。");
* }
*/
Preconditions.checkNotNull(arg, "引数がnullです。");
System.out.println("execute function2 [argument: " + arg + "]");
System.out.println("execute function2 end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment