View Javadoc
1   package com.github.mygreen.supercsv.util;
2   
3   import java.util.Collection;
4   import java.util.Map;
5   
6   /**
7    * 引数チェックに関するユーティリティクラス。
8    *
9    * @since 2.0
10   * @author T.TSUCHIE
11   *
12   */
13  public class ArgUtils {
14      
15      /**
16       * 値がnullでないかどうか検証する。
17       * @param arg 検証対象の値
18       * @param name 検証対象の引数の名前
19       * @throws NullPointerException {@literal arg == null.}
20       */
21      public static void notNull(final Object arg, final String name) {
22          if(arg == null) {
23              throw new NullPointerException(String.format("%s should not be null.", name));
24          }
25      }
26      
27      /**
28       * 文字列が空 or nullでないかどうか検証する。
29       * @param arg 検証対象の値
30       * @param name 検証対象の引数の名前
31       * @throws NullPointerException {@literal arg == null}
32       * @throws IllegalArgumentException {@literal arg.isEmpty() == true}
33       */
34      public static void notEmpty(final String arg, final String name) {
35          if(arg == null) {
36              throw new NullPointerException(String.format("%s should not be null.", name));
37          }
38          
39          if(arg.isEmpty()) {
40              throw new IllegalArgumentException(String.format("%s should not be empty.", name));
41          }
42      }
43      
44      /**
45       * 配列のサイズが0または、nullでないかどうか検証する。
46       * @param arg 検証対象の値
47       * @param name 検証対象の引数の名前
48       * @throws NullPointerException {@literal arg == null}
49       * @throws IllegalArgumentException {@literal arg.length == 0.}
50       */
51      public static void notEmpty(final Object[] arg, final String name) {
52          if(arg == null) {
53              throw new NullPointerException(String.format("%s should not be null.", name));
54          }
55          
56          if(arg.length == 0) {
57              throw new IllegalArgumentException(String.format("%s should has length ararys.", name));
58          }
59      }
60      
61      /**
62       * Collection(リスト、セット)のサイズが0または、nullでないかどうか検証する。
63       * @param arg 検証対象の値
64       * @param name 検証対象の引数の名前
65       * @throws NullPointerException {@literal arg == null}
66       * @throws IllegalArgumentException {@literal arg.size() == 0.}
67       */
68      public static void notEmpty(final Collection<?> arg, final String name) {
69          if(arg == null) {
70              throw new NullPointerException(String.format("%s should not be null.", name));
71          }
72          
73          if(arg.isEmpty()) {
74              throw new IllegalArgumentException(String.format("%s should not be empty.", name));
75          }
76      }
77      
78      /**
79       * マップのサイズが0または、nullでないかどうか検証する。
80       * @param arg 検証対象の値
81       * @param name 検証対象の引数の名前
82       * @throws NullPointerException {@literal arg == null}
83       * @throws IllegalArgumentException {@literal arg.size() == 0.}
84       */
85      public static void notEmpty(final Map<?, ?> arg, final String name) {
86          if(arg == null) {
87              throw new NullPointerException(String.format("%s should not be null.", name));
88          }
89          
90          if(arg.isEmpty()) {
91              throw new IllegalArgumentException(String.format("%s should not be empty.", name));
92          }
93      }
94      
95      /**
96       * 引数が {@literal 'arg' >= 'min'} の関係か検証する。
97       * @param arg 検証対象の値
98       * @param min 最小値
99       * @param name 検証対象の引数の名前
100      * @throws NullPointerException {@literal arg == null}
101      * @throws IllegalArgumentException {@literal arg < min.}
102      */
103     @SuppressWarnings({"unchecked", "rawtypes"})
104     public static <T extends Comparable> void notMin(final T arg, final T min, final String name) {
105         
106         if(arg == null) {
107             throw new NullPointerException(String.format("%s should not be null.", name));
108         }
109         
110         if(arg.compareTo(min) < 0) {
111             throw new IllegalArgumentException(String.format("%s cannot be smaller than %s", name, min.toString()));
112         }
113         
114     }
115     
116     /**
117      * 引数が {@literal 'arg' <= 'max'} の関係か検証する。
118      * @param arg 検証対象の値
119      * @param max 最大値
120      * @param name 検証対象の引数の名前
121      * @throws NullPointerException {@literal arg == null}
122      * @throws IllegalArgumentException {@literal arg > max.}
123      */
124     @SuppressWarnings({"rawtypes", "unchecked"})
125     public static <T extends Comparable> void notMax(final T arg, final T max, final String name) {
126         
127         if(arg == null) {
128             throw new NullPointerException(String.format("%s should not be null.", name));
129         }
130         
131         if(arg.compareTo(max) > 0) {
132             throw new IllegalArgumentException(String.format("%s cannot be greater than %s", name, max.toString()));
133         }
134         
135     }
136     
137 }