회사에서 정보 공유 메일중에서 Guava의 Precondtion을 이용하자는 의견이 있었다.
평소에도 Guava를 많이 사용하긴 했는데, 말나온김에 구글링을 한 번 해봤다.
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 |
public static void fooPlainJavaValidation(String name, int start, int end) { if (null == name) { throw new NullPointerException("Name must not be null"); } if (start >= end) { throw new IllegalArgumentException( "Start (" + start + ") must be smaller than end (" + end + ")"); } } public static void fooSpringFrameworkAssert(String name, int start, int end) { Assert.notNull(name, "Name must not be null"); Assert.isTrue(start < end, "Start (" + start + ") must be smaller than end (" + end + ")"); } public static void fooApacheCommonsValidate(String name, int start, int end) { Validate.notNull(name, "Name must not be null"); Validate.isTrue(start < end, "Start (%s) must be smaller than end (%s)", start, end); } public static void fooGuavaPreconditions(String name, int start, int end) { Preconditions.checkNotNull(name, "Name must not be null"); Preconditions.checkArgument(start < end, "Start (%s) must be smaller than end (%s)", start, end); } public static void fooPlainJavaAsserts(String name, int start, int end) { assert (null != name) : "Name must not be null"; assert (start < end) : "Start(" + start + ") must be smaller than end(" + end + ")"; } |
특별히 장단점이 없다면 개인의 취향에 따라서 골라서 써도 좋을 것 같다.
하지만 내생각(원작자 블로거의 생각도..)에는 Guava가 제일 나은 듯.
출처 : http://www.sw-engineering-candies.com/blog-1/comparison-of-ways-to-check-preconditions-in-java