View Javadoc
1   /*
2    * Copyright 2009-2013 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.orangesignal.csv.filters;
18  
19  import java.util.List;
20  import java.util.regex.Pattern;
21  
22  /**
23   * 区切り文字形式データフィルタの条件式ユーティリティを提供します。
24   * 
25   * @author Koji Sugisawa
26   * @since 1.2.3
27   */
28  abstract class CsvExpressionUtils {
29  
30  	/**
31  	 * デフォルトコンストラクタです。
32  	 */
33  	protected CsvExpressionUtils() {}
34  
35  	// ------------------------------------------------------------------------
36  
37  	/**
38  	 * 指定された項目位置のデータが <code>null</code> であるかどうかを返します。
39  	 * 
40  	 * @param values 区切り文字形式データの値リスト
41  	 * @param position 項目位置
42  	 * @return 指定された項目位置のデータが <code>null</code> の場合は <code>true</code> それ以外の場合は <code>false</code>
43  	 * @throws IllegalArgumentException 
44  	 */
45  	public static boolean isNull(final List<String> values, final int position) {
46  		validate(values, position);
47  		return values.get(position) == null;
48  	}
49  
50  	/**
51  	 * 指定された項目位置のデータが <code>null</code> でないかどうかを返します。
52  	 * 
53  	 * @param values 区切り文字形式データの値リスト
54  	 * @param position 項目位置
55  	 * @return 指定された項目位置のデータが <code>null</code> でない場合は <code>true</code> それ以外の場合は <code>false</code>
56  	 * @throws IllegalArgumentException 
57  	 */
58  	public static boolean isNotNull(final List<String> values, final int position) {
59  		return !isNull(values, position);
60  	}
61  
62  	/**
63  	 * 指定された項目位置のデータが空かどうかを返します。
64  	 * 
65  	 * @param values 区切り文字形式データの値リスト
66  	 * @param position 項目位置
67  	 * @return 指定された項目位置のデータが空の場合は <code>true</code> それ以外の場合は <code>false</code>
68  	 * @throws IllegalArgumentException 
69  	 */
70  	public static boolean isEmpty(final List<String> values, final int position) {
71  		validate(values, position);
72  		final String value = values.get(position);
73  		return value == null || value.isEmpty();
74  	}
75  
76  	/**
77  	 * 指定された項目位置のデータが空でないかどうかを返します。
78  	 * 
79  	 * @param values 区切り文字形式データの値リスト
80  	 * @param position 項目位置
81  	 * @return 指定された項目位置のデータが空でない場合は <code>true</code> それ以外の場合は <code>false</code>
82  	 * @throws IllegalArgumentException 
83  	 */
84  	public static boolean isNotEmpty(final List<String> values, final int position) {
85  		return !isEmpty(values, position);
86  	}
87  
88  	// ------------------------------------------------------------------------
89  
90  	/**
91  	 * 指定された項目位置のデータが判定基準値と等しいかどうかを返します。
92  	 * 
93  	 * @param values 区切り文字形式データの値リスト
94  	 * @param position 項目位置
95  	 * @param criteria 判定基準値
96  	 * @param ignoreCase 大文字と小文字を区別するかどうか
97  	 * @return 指定された項目位置のデータが判定基準値と等しい場合は <code>true</code> それ以外の場合は <code>false</code>
98  	 * @throws IllegalArgumentException 
99  	 */
100 	public static boolean eq(final List<String> values, final int position, final String criteria, final boolean ignoreCase) {
101 		validate(values, position);
102 		if (criteria == null) {
103 			throw new IllegalArgumentException("Criteria must not be null");
104 		}
105 		final String value = values.get(position);
106 		return value != null && ignoreCase ? criteria.equalsIgnoreCase(value) : criteria.equals(value);
107 	}
108 
109 	/**
110 	 * 指定された項目位置のデータが判定基準値と等しくないかどうかを返します。
111 	 * 
112 	 * @param values 区切り文字形式データの値リスト
113 	 * @param position 項目位置
114 	 * @param criteria 判定基準値
115 	 * @param ignoreCase 大文字と小文字を区別するかどうか
116 	 * @return 指定された項目位置のデータが判定基準値と等しくない場合は <code>true</code> それ以外の場合は <code>false</code>
117 	 * @throws IllegalArgumentException 
118 	 */
119 	public static boolean ne(final List<String> values, final int position, final String criteria, final boolean ignoreCase) {
120 		return !eq(values, position, criteria, ignoreCase);
121 	}
122 
123 	/**
124 	 * 指定された項目位置のデータが判定基準値群のいずれかと等しいかどうかを返します。
125 	 * 
126 	 * @param values 区切り文字形式データの値リスト
127 	 * @param position 項目位置
128 	 * @param criterias 判定基準値群
129 	 * @param ignoreCase 大文字と小文字を区別するかどうか
130 	 * @return 指定された項目位置のデータが判定基準値群のいずれかと等しい場合は <code>true</code> それ以外の場合は <code>false</code>
131 	 * @throws IllegalArgumentException 
132 	 */
133 	public static boolean in(final List<String> values, final int position, final String[] criterias, final boolean ignoreCase) {
134 		if (criterias == null) {
135 			throw new IllegalArgumentException("Criterias must not be null");
136 		}
137 		for (final String criteria : criterias) {
138 			if (eq(values, position, criteria, ignoreCase)) {
139 				return true;
140 			}
141 		}
142 		return false;
143 	}
144 
145 	/**
146 	 * 指定された項目位置のデータが判定基準値群のいずれとも等しくないかどうかを返します。
147 	 * 
148 	 * @param values 区切り文字形式データの値リスト
149 	 * @param position 項目位置
150 	 * @param criterias 判定基準値群
151 	 * @param ignoreCase 大文字と小文字を区別するかどうか
152 	 * @return 指定された項目位置のデータが判定基準値群のいずれとも等しくない場合は <code>true</code> それ以外の場合は <code>false</code>
153 	 * @throws IllegalArgumentException 
154 	 */
155 	public static boolean notIn(final List<String> values, final int position, final String[] criterias, final boolean ignoreCase) {
156 		return !in(values, position, criterias, ignoreCase);
157 	}
158 
159 	// ------------------------------------------------------------------------
160 
161 	/**
162 	 * 指定された項目位置のデータが正規表現パターンとマッチするかどうかを返します。
163 	 * 
164 	 * @param values 区切り文字形式データの値リスト
165 	 * @param position 項目位置
166 	 * @param pattern 判定する正規表現パターン
167 	 * @return 指定された項目位置のデータが正規表現パターンとマッチする場合は <code>true</code> それ以外の場合は <code>false</code>
168 	 * @throws IllegalArgumentException 
169 	 */
170 	public static boolean regex(final List<String> values, final int position, final Pattern pattern) {
171 		validate(values, position);
172 		final String value = values.get(position);
173 		return value != null && pattern.matcher(value).matches();
174 	}
175 
176 	// ------------------------------------------------------------------------
177 
178 	/**
179 	 * 指定された項目位置のデータが判定基準値よりも大きいかどうかを返します。
180 	 * 
181 	 * @param values 区切り文字形式データの値リスト
182 	 * @param position 項目位置
183 	 * @param criteria 判定基準値
184 	 * @return 指定された項目位置のデータが判定基準値よりも大きい場合は <code>true</code> それ以外の場合は <code>false</code>
185 	 * @throws IllegalArgumentException 
186 	 */
187 	public static boolean gt(final List<String> values, final int position, final String criteria) {
188 		validate(values, position);
189 		final String value = values.get(position);
190 		return value != null && criteria != null && value.compareTo(criteria) > 0;
191 	}
192 
193 	/**
194 	 * 指定された項目位置のデータが判定基準値よりも小さいかどうかを返します。
195 	 * 
196 	 * @param values 区切り文字形式データの値リスト
197 	 * @param position 項目位置
198 	 * @param criteria 判定基準値
199 	 * @return 指定された項目位置のデータが判定基準値よりも小さい場合は <code>true</code> それ以外の場合は <code>false</code>
200 	 * @throws IllegalArgumentException 
201 	 */
202 	public static boolean lt(final List<String> values, final int position, final String criteria) {
203 		validate(values, position);
204 		final String value = values.get(position);
205 		return value != null && criteria != null && value.compareTo(criteria) < 0;
206 	}
207 
208 	/**
209 	 * 指定された項目位置のデータが判定基準値以上かどうかを返します。
210 	 * 
211 	 * @param values 区切り文字形式データの値リスト
212 	 * @param position 項目位置
213 	 * @param criteria 判定基準値
214 	 * @return 指定された項目位置のデータが判定基準値以上の場合は <code>true</code> それ以外の場合は <code>false</code>
215 	 * @throws IllegalArgumentException 
216 	 */
217 	public static boolean ge(final List<String> values, final int position, final String criteria) {
218 		validate(values, position);
219 		final String value = values.get(position);
220 		return value != null && criteria != null && value.compareTo(criteria) >= 0;
221 	}
222 
223 	/**
224 	 * 指定された項目位置のデータが判定基準値以下かどうかを返します。
225 	 * 
226 	 * @param values 区切り文字形式データの値リスト
227 	 * @param position 項目位置
228 	 * @param criteria 判定基準値
229 	 * @return 指定された項目位置のデータが判定基準値以下の場合は <code>true</code> それ以外の場合は <code>false</code>
230 	 * @throws IllegalArgumentException 
231 	 */
232 	public static boolean le(final List<String> values, final int position, final String criteria) {
233 		validate(values, position);
234 		final String value = values.get(position);
235 		return value != null && criteria != null && value.compareTo(criteria) <= 0;
236 	}
237 
238 	/**
239 	 * 指定された項目位置のデータが下限値から上限値の範囲かどうかを返します。
240 	 * 
241 	 * @param values 区切り文字形式データの値リスト
242 	 * @param position 項目位置
243 	 * @param low 下限値
244 	 * @param high 上限値
245 	 * @return 指定された項目位置のデータが下限値から上限値の範囲の場合は <code>true</code> それ以外の場合は <code>false</code>
246 	 * @throws IllegalArgumentException 
247 	 */
248 	public static boolean between(final List<String> values, final int position, final String low, final String high) {
249 		return ge(values, position, low) && le(values, position, high);
250 	}
251 
252 	// ------------------------------------------------------------------------
253 
254 	/**
255 	 * 指定されたパラメータを検証します。
256 	 * 
257 	 * @param values 区切り文字形式データの値リスト
258 	 * @param position 項目位置
259 	 * @throws IllegalArgumentException パラメータが不正な場合
260 	 */
261 	protected static void validate(final List<String> values, final int position) {
262 		if (values == null) {
263 			throw new IllegalArgumentException("Values must not be null");
264 		}
265 		if (position < 0 || position >= values.size()) {
266 			throw new IllegalArgumentException(String.format("Invalid column position %d", position));
267 		}
268 	}
269 
270 }