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.io.IOException; 20 import java.util.Comparator; 21 import java.util.regex.Pattern; 22 23 /** 24 * DSL (Domain Specific Language) 形式でスマートなフィルタ条件の定義が可能な {@link BeanFilter} の実装クラスを提供します。 25 * 26 * @author Koji Sugisawa 27 * @since 1.2.3 28 */ 29 public class SimpleBeanFilter implements BeanFilter { 30 31 private BeanLogicalExpression expr; 32 33 /** 34 * デフォルトコンストラクタです。 35 */ 36 public SimpleBeanFilter() { 37 this(new BeanAndExpression()); 38 } 39 40 /** 41 * コンストラクタです。 42 * 43 * @param expr 論理演算 Java プログラム要素フィルタ 44 * @throws IllegalArgumentException <code>expr</code> が <code>null</code> の場合 45 */ 46 public SimpleBeanFilter(final BeanLogicalExpression expr) { 47 if (expr == null) { 48 throw new IllegalArgumentException(String.format("%s must not be null", BeanLogicalExpression.class.getSimpleName())); 49 } 50 this.expr = expr; 51 } 52 53 /** 54 * 指定された Java プログラム要素フィルタを追加します。 55 * 56 * @param filter Java プログラム要素フィルタ 57 * @return このオブジェクトへの参照 58 */ 59 public SimpleBeanFilter add(final BeanFilter filter) { 60 expr.add(filter); 61 return this; 62 } 63 64 /** 65 * 指定された Java プログラム要素のフィールド値が <code>null</code> であるかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 66 * 67 * @param name フィールド名 68 * @return このオブジェクトへの参照 69 * @throws IllegalArgumentException <code>name</code> が <code>null</code> の場合 70 */ 71 public SimpleBeanFilter isNull(final String name) { 72 expr.add(BeanExpressions.isNull(name)); 73 return this; 74 } 75 76 /** 77 * 指定された Java プログラム要素のフィールド値が <code>null</code> でないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 78 * 79 * @param name フィールド名 80 * @return このオブジェクトへの参照 81 * @throws IllegalArgumentException <code>name</code> が <code>null</code> の場合 82 */ 83 public SimpleBeanFilter isNotNull(final String name) { 84 expr.add(BeanExpressions.isNotNull(name)); 85 return this; 86 } 87 88 /** 89 * 指定された Java プログラム要素のフィールド値が空かどうかでフィルタを適用する Java プログラム要素フィルタを構築して返します。 90 * 91 * @param name フィールド名 92 * @return このオブジェクトへの参照 93 * @throws IllegalArgumentException <code>name</code> が <code>null</code> の場合 94 */ 95 public SimpleBeanFilter isEmpty(final String name) { 96 expr.add(BeanExpressions.isEmpty(name)); 97 return this; 98 } 99 100 /** 101 * 指定された Java プログラム要素のフィールド値が空でないかどうかでフィルタを適用する Java プログラム要素フィルタを構築して返します。 102 * 103 * @param name フィールド名 104 * @return このオブジェクトへの参照 105 * @throws IllegalArgumentException <code>name</code> が <code>null</code> の場合 106 */ 107 public SimpleBeanFilter isNotEmpty(final String name) { 108 expr.add(BeanExpressions.isNotEmpty(name)); 109 return this; 110 } 111 112 /** 113 * 指定された Java プログラム要素のフィールド値が判定基準値と等しいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 114 * 115 * @param name フィールド名 116 * @param criteria 判定基準値 117 * @return このオブジェクトへの参照 118 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 119 */ 120 public SimpleBeanFilter eq(final String name, final Object criteria) { 121 expr.add(BeanExpressions.eq(name, criteria)); 122 return this; 123 } 124 125 /** 126 * 指定された Java プログラム要素のフィールド値が判定基準値と等しいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 127 * 128 * @param name フィールド名 129 * @param criteria 判定基準値 130 * @param ignoreCase 大文字と小文字を区別するかどうか 131 * @return このオブジェクトへの参照 132 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 133 */ 134 public SimpleBeanFilter eq(final String name, final String criteria, final boolean ignoreCase) { 135 expr.add(BeanExpressions.eq(name, criteria, ignoreCase)); 136 return this; 137 } 138 139 /** 140 * 指定された Java プログラム要素のフィールド値が判定基準値と等しくないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 141 * 142 * @param name フィールド名 143 * @param criteria 判定基準値 144 * @return このオブジェクトへの参照 145 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 146 */ 147 public SimpleBeanFilter ne(final String name, final Object criteria) { 148 expr.add(BeanExpressions.ne(name, criteria)); 149 return this; 150 } 151 152 /** 153 * 指定された Java プログラム要素のフィールド値が判定基準値と等しくないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 154 * 155 * @param name フィールド名 156 * @param criteria 判定基準値 157 * @param ignoreCase 大文字と小文字を区別するかどうか 158 * @return このオブジェクトへの参照 159 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 160 */ 161 public SimpleBeanFilter ne(final String name, final String criteria, final boolean ignoreCase) { 162 expr.add(BeanExpressions.ne(name, criteria, ignoreCase)); 163 return this; 164 } 165 166 /** 167 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれかと等しいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 168 * 169 * @param name フィールド名 170 * @param criterias 判定基準値群 171 * @return このオブジェクトへの参照 172 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 173 */ 174 public SimpleBeanFilter in(final String name, final Object... criterias) { 175 expr.add(BeanExpressions.in(name, criterias)); 176 return this; 177 } 178 179 /** 180 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれかと等しいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 181 * 182 * @param name フィールド名 183 * @param criterias 判定基準値群 184 * @return このオブジェクトへの参照 185 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 186 */ 187 public SimpleBeanFilter in(final String name, final String... criterias) { 188 expr.add(BeanExpressions.in(name, criterias)); 189 return this; 190 } 191 192 /** 193 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれかと等しいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 194 * 195 * @param name フィールド名 196 * @param criterias 判定基準値群 197 * @param ignoreCase 大文字と小文字を区別するかどうか 198 * @return このオブジェクトへの参照 199 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 200 */ 201 public SimpleBeanFilter in(final String name, final String[] criterias, final boolean ignoreCase) { 202 expr.add(BeanExpressions.in(name, criterias, ignoreCase)); 203 return this; 204 } 205 206 /** 207 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれとも等しくないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 208 * 209 * @param name フィールド名 210 * @param criterias 判定基準値群 211 * @return このオブジェクトへの参照 212 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 213 */ 214 public SimpleBeanFilter notIn(final String name, final Object... criterias) { 215 expr.add(BeanExpressions.notIn(name, criterias)); 216 return this; 217 } 218 219 /** 220 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれとも等しくないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 221 * 222 * @param name フィールド名 223 * @param criterias 判定基準値群 224 * @return このオブジェクトへの参照 225 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 226 */ 227 public SimpleBeanFilter notIn(final String name, final String[] criterias) { 228 expr.add(BeanExpressions.notIn(name, criterias)); 229 return this; 230 } 231 232 /** 233 * 指定された Java プログラム要素のフィールド値が判定基準値群のいずれとも等しくないかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 234 * 235 * @param name フィールド名 236 * @param criterias 判定基準値群 237 * @param ignoreCase 大文字と小文字を区別するかどうか 238 * @return このオブジェクトへの参照 239 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 240 */ 241 public SimpleBeanFilter notIn(final String name, final String[] criterias, final boolean ignoreCase) { 242 expr.add(BeanExpressions.notIn(name, criterias, ignoreCase)); 243 return this; 244 } 245 246 /** 247 * 指定された Java プログラム要素のフィールド値が正規表現パターンとマッチするかどうかでフィルタを適用する区切り文字形式データフィルタを追加します。 248 * 249 * @param name 項目名 250 * @param pattern 正規表現パターン 251 * @return このオブジェクトへの参照 252 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 253 */ 254 public SimpleBeanFilter regex(final String name, final String pattern) { 255 expr.add(BeanExpressions.regex(name, pattern)); 256 return this; 257 } 258 259 /** 260 * 指定された Java プログラム要素のフィールド値が正規表現パターンとマッチするかどうかでフィルタを適用する区切り文字形式データフィルタを追加します。 261 * 262 * @param name 項目名 263 * @param pattern 正規表現パターン 264 * @param ignoreCase 大文字と小文字を区別するかどうか 265 * @return このオブジェクトへの参照 266 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 267 */ 268 public SimpleBeanFilter regex(final String name, final String pattern, final boolean ignoreCase) { 269 expr.add(BeanExpressions.regex(name, pattern, ignoreCase)); 270 return this; 271 } 272 273 /** 274 * 指定された Java プログラム要素のフィールド値が正規表現パターンとマッチするかどうかでフィルタを適用する区切り文字形式データフィルタを追加します。 275 * 276 * @param name 項目名 277 * @param pattern 正規表現パターン 278 * @param flags マッチフラグ 279 * @return このオブジェクトへの参照 280 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 281 */ 282 public SimpleBeanFilter regex(final String name, final String pattern, final int flags) { 283 expr.add(BeanExpressions.regex(name, pattern, flags)); 284 return this; 285 } 286 287 /** 288 * 指定された Java プログラム要素のフィールド値が正規表現パターンとマッチするかどうかでフィルタを適用する区切り文字形式データフィルタを追加します。 289 * 290 * @param name 項目名 291 * @param pattern 正規表現パターン 292 * @return このオブジェクトへの参照 293 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 294 */ 295 public SimpleBeanFilter regex(final String name, final Pattern pattern) { 296 expr.add(BeanExpressions.regex(name, pattern)); 297 return this; 298 } 299 300 /** 301 * 指定された Java プログラム要素のフィールド値が判定基準値より大きいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 302 * 303 * @param name フィールド名 304 * @param criteria 判定基準値 305 * @return このオブジェクトへの参照 306 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 307 */ 308 public SimpleBeanFilter gt(final String name, final Object criteria) { 309 expr.add(BeanExpressions.gt(name, criteria)); 310 return this; 311 } 312 313 /** 314 * 指定された Java プログラム要素のフィールド値が判定基準値より大きいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 315 * 316 * @param name フィールド名 317 * @param criteria 判定基準値 318 * @param comparator コンパレータ (オプション) 319 * @return このオブジェクトへの参照 320 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 321 */ 322 @SuppressWarnings("unchecked") 323 public SimpleBeanFilter gt(final String name, final Object criteria, @SuppressWarnings("rawtypes") final Comparator comparator) { 324 expr.add(BeanExpressions.gt(name, criteria, comparator)); 325 return this; 326 } 327 328 /** 329 * 指定された Java プログラム要素のフィールド値が判定基準値より小さいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 330 * 331 * @param name フィールド名 332 * @param criteria 判定基準値 333 * @return このオブジェクトへの参照 334 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 335 */ 336 public SimpleBeanFilter lt(final String name, final Object criteria) { 337 expr.add(BeanExpressions.lt(name, criteria)); 338 return this; 339 } 340 341 /** 342 * 指定された Java プログラム要素のフィールド値が判定基準値より小さいかどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 343 * 344 * @param name フィールド名 345 * @param criteria 判定基準値 346 * @param comparator コンパレータ (オプション) 347 * @return このオブジェクトへの参照 348 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 349 */ 350 public SimpleBeanFilter lt(final String name, final Object criteria, @SuppressWarnings("rawtypes") final Comparator comparator) { 351 expr.add(BeanExpressions.lt(name, criteria, comparator)); 352 return this; 353 } 354 355 /** 356 * 指定された Java プログラム要素のフィールド値が判定基準値以上かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 357 * 358 * @param name フィールド名 359 * @param criteria 判定基準値 360 * @return このオブジェクトへの参照 361 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 362 */ 363 public SimpleBeanFilter ge(final String name, final Object criteria) { 364 expr.add(BeanExpressions.ge(name, criteria)); 365 return this; 366 } 367 368 /** 369 * 指定された Java プログラム要素のフィールド値が判定基準値以上かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 370 * 371 * @param name フィールド名 372 * @param criteria 判定基準値 373 * @param comparator コンパレータ (オプション) 374 * @return このオブジェクトへの参照 375 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 376 */ 377 public SimpleBeanFilter ge(final String name, final Object criteria, @SuppressWarnings("rawtypes") final Comparator comparator) { 378 expr.add(BeanExpressions.ge(name, criteria, comparator)); 379 return this; 380 } 381 382 /** 383 * 指定された Java プログラム要素のフィールド値が判定基準値以下かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 384 * 385 * @param name フィールド名 386 * @param criteria 判定基準値 387 * @return このオブジェクトへの参照 388 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 389 */ 390 public SimpleBeanFilter le(final String name, final Object criteria) { 391 expr.add(BeanExpressions.le(name, criteria)); 392 return this; 393 } 394 395 /** 396 * 指定された Java プログラム要素のフィールド値が判定基準値以下かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 397 * 398 * @param name フィールド名 399 * @param criteria 判定基準値 400 * @param comparator コンパレータ (オプション) 401 * @return このオブジェクトへの参照 402 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 403 */ 404 public SimpleBeanFilter le(final String name, final Object criteria, @SuppressWarnings("rawtypes") final Comparator comparator) { 405 expr.add(BeanExpressions.le(name, criteria, comparator)); 406 return this; 407 } 408 409 /** 410 * 指定された Java プログラム要素のフィールド値が下限値から上限値の範囲かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 411 * 412 * @param name フィールド名 413 * @param low 下限値 414 * @param high 上限値 415 * @return このオブジェクトへの参照 416 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 417 */ 418 public SimpleBeanFilter between(final String name, final Object low, final Object high) { 419 expr.add(BeanExpressions.between(name, low, high)); 420 return this; 421 } 422 423 /** 424 * 指定された Java プログラム要素のフィールド値が下限値から上限値の範囲かどうかでフィルタを適用する Java プログラム要素フィルタを追加します。 425 * 426 * @param name フィールド名 427 * @param low 下限値 428 * @param high 上限値 429 * @param comparator コンパレータ (オプション) 430 * @return このオブジェクトへの参照 431 * @throws IllegalArgumentException パラメータが <code>null</code> の場合 432 */ 433 public SimpleBeanFilter between(final String name, final Object low, final Object high, @SuppressWarnings("rawtypes") final Comparator comparator) { 434 expr.add(BeanExpressions.between(name, low, high, comparator)); 435 return this; 436 } 437 438 /** 439 * 指定された Java プログラム要素フィルタの論理否定でフィルタを適用する Java プログラム要素フィルタを追加します。 440 * 441 * @param filter 論理否定する Java プログラム要素フィルタ 442 * @return このオブジェクトへの参照 443 * @throws IllegalArgumentException <code>filter</code> が <code>null</code> の場合 444 */ 445 public SimpleBeanFilter not(final BeanFilter filter) { 446 expr.add(BeanExpressions.not(filter)); 447 return this; 448 } 449 450 @Override 451 public boolean accept(final Object bean) throws IOException { 452 return expr.accept(bean); 453 } 454 455 @Override 456 public String toString() { 457 final String name = getClass().getName(); 458 final int period = name.lastIndexOf('.'); 459 return period > 0 ? name.substring(period + 1) : name; 460 } 461 462 }