Java Guava 다시 - 2 우혁이 아빠 2016. 5. 19. 15:58 1. Preconditions package com.tisotry.tazz009.guava; import com.google.common.base.Preconditions; public class PreconditionsTest { public static void main(String[] args) { PreconditionsTest test = new PreconditionsTest(); try { System.out.println(test.sqrt(4)); // 제곱근(루트) System.out.println(test.sqrt(-3.0)); // 제곱근(루트) } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { System.out.println(test.sum(null, 3)); } catch (NullPointerException e) { System.out.println(e.getMessage()); } try { System.out.println(test.getValue(6)); } catch (IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } private double sqrt(double d) throws IllegalArgumentException { Preconditions.checkArgument( d > 0.0, "Illegal Argument passed: Negative value %s.", d); return Math.sqrt(d); } private int sum(Integer a, Integer b) { a = Preconditions.checkNotNull( a, "Illegal Argument passed: First parameter is Null."); b = Preconditions.checkNotNull( b, "Illegal Argument passed: Second parameter is Null."); return a+b; } private int getValue(int i) { int[] data = {1, 2, 3, 4, 5}; Preconditions.checkElementIndex( i, data.length, "Illegal Argument passed: Invalid index."); return 0; } } 저작자표시 (새창열림)