1 7 package javax.swing; 8 9 import java.util.ArrayList ; 10 import java.math.BigDecimal ; 11 import java.math.BigInteger ; 12 import java.util.Date ; 13 import java.util.List ; 14 import java.util.regex.Matcher ; 15 import java.util.regex.Pattern ; 16 17 79 public abstract class RowFilter<M,I> { 80 87 public enum ComparisonType { 88 92 BEFORE, 93 94 98 AFTER, 99 100 104 EQUAL, 105 106 110 NOT_EQUAL 111 } 112 113 117 private static void checkIndices(int[] columns) { 118 for (int i = columns.length - 1; i >= 0; i--) { 119 if (columns[i] < 0) { 120 throw new IllegalArgumentException ("Index must be >= 0"); 121 } 122 } 123 } 124 125 156 public static <M,I> RowFilter <M,I> regexFilter(String regex, 157 int... indices) { 158 return (RowFilter <M,I>)new RegexFilter(Pattern.compile(regex), 159 indices); 160 } 161 162 184 public static <M,I> RowFilter <M,I> dateFilter(ComparisonType type, 185 Date date, int... indices) { 186 return (RowFilter <M,I>)new DateFilter(type, date.getTime(), indices); 187 } 188 189 207 public static <M,I> RowFilter <M,I> numberFilter(ComparisonType type, 208 Number number, int... indices) { 209 return (RowFilter <M,I>)new NumberFilter(type, number, indices); 210 } 211 212 233 public static <M,I> RowFilter <M,I> orFilter( 234 Iterable <? extends RowFilter <? super M, ? super I>> filters) { 235 return new OrFilter<M,I>(filters); 236 } 237 238 259 public static <M,I> RowFilter <M,I> andFilter( 260 Iterable <? extends RowFilter <? super M, ? super I>> filters) { 261 return new AndFilter<M,I>(filters); 262 } 263 264 273 public static <M,I> RowFilter <M,I> notFilter(RowFilter <M,I> filter) { 274 return new NotFilter<M,I>(filter); 275 } 276 277 289 public abstract boolean include(Entry<? extends M, ? extends I> entry); 290 291 299 313 public static abstract class Entry<M, I> { 314 317 public Entry() { 318 } 319 320 325 public abstract M getModel(); 326 327 334 public abstract int getValueCount(); 335 336 346 public abstract Object getValue(int index); 347 348 365 public String getStringValue(int index) { 366 Object value = getValue(index); 367 return (value == null) ? "" : value.toString(); 368 } 369 370 378 public abstract I getIdentifier(); 379 } 380 381 382 private static abstract class GeneralFilter extends RowFilter <Object ,Object > { 383 private int[] columns; 384 385 GeneralFilter(int[] columns) { 386 checkIndices(columns); 387 this.columns = columns; 388 } 389 390 public boolean include(Entry<? extends Object ,? extends Object > value){ 391 int count = value.getValueCount(); 392 if (columns.length > 0) { 393 for (int i = columns.length - 1; i >= 0; i--) { 394 int index = columns[i]; 395 if (index < count) { 396 if (include(value, index)) { 397 return true; 398 } 399 } 400 } 401 } 402 else { 403 while (--count >= 0) { 404 if (include(value, count)) { 405 return true; 406 } 407 } 408 } 409 return false; 410 } 411 412 protected abstract boolean include( 413 Entry<? extends Object ,? extends Object > value, int index); 414 } 415 416 417 private static class RegexFilter extends GeneralFilter { 418 private Matcher matcher; 419 420 RegexFilter(Pattern regex, int[] columns) { 421 super(columns); 422 if (regex == null) { 423 throw new IllegalArgumentException ("Pattern must be non-null"); 424 } 425 matcher = regex.matcher(""); 426 } 427 428 protected boolean include( 429 Entry<? extends Object ,? extends Object > value, int index) { 430 matcher.reset(value.getStringValue(index)); 431 return matcher.find(); 432 } 433 } 434 435 436 private static class DateFilter extends GeneralFilter { 437 private long date; 438 private ComparisonType type; 439 440 DateFilter(ComparisonType type, long date, int[] columns) { 441 super(columns); 442 if (type == null) { 443 throw new IllegalArgumentException ("type must be non-null"); 444 } 445 this.type = type; 446 this.date = date; 447 } 448 449 protected boolean include( 450 Entry<? extends Object ,? extends Object > value, int index) { 451 Object v = value.getValue(index); 452 453 if (v instanceof Date ) { 454 long vDate = ((Date )v).getTime(); 455 switch(type) { 456 case BEFORE: 457 return (vDate < date); 458 case AFTER: 459 return (vDate > date); 460 case EQUAL: 461 return (vDate == date); 462 case NOT_EQUAL: 463 return (vDate != date); 464 default: 465 break; 466 } 467 } 468 return false; 469 } 470 } 471 472 473 474 475 private static class NumberFilter extends GeneralFilter { 476 private boolean isComparable; 477 private Number number; 478 private ComparisonType type; 479 480 NumberFilter(ComparisonType type, Number number, int[] columns) { 481 super(columns); 482 if (type == null || number == null) { 483 throw new IllegalArgumentException ( 484 "type and number must be non-null"); 485 } 486 this.type = type; 487 this.number = number; 488 isComparable = (number instanceof Comparable ); 489 } 490 491 @SuppressWarnings ("unchecked") 492 protected boolean include( 493 Entry<? extends Object ,? extends Object > value, int index) { 494 Object v = value.getValue(index); 495 496 if (v instanceof Number ) { 497 boolean compared = true; 498 int compareResult; 499 Class vClass = v.getClass(); 500 if (number.getClass() == vClass && isComparable) { 501 compareResult = ((Comparable )number).compareTo(v); 502 } 503 else { 504 compareResult = longCompare((Number )v); 505 } 506 switch(type) { 507 case BEFORE: 508 return (compareResult > 0); 509 case AFTER: 510 return (compareResult < 0); 511 case EQUAL: 512 return (compareResult == 0); 513 case NOT_EQUAL: 514 return (compareResult != 0); 515 default: 516 break; 517 } 518 } 519 return false; 520 } 521 522 private int longCompare(Number o) { 523 long diff = number.longValue() - o.longValue(); 524 525 if (diff < 0) { 526 return -1; 527 } 528 else if (diff > 0) { 529 return 1; 530 } 531 return 0; 532 } 533 } 534 535 536 private static class OrFilter<M,I> extends RowFilter <M,I> { 537 List <RowFilter <? super M,? super I>> filters; 538 539 OrFilter(Iterable <? extends RowFilter <? super M, ? super I>> filters) { 540 this.filters = new ArrayList <RowFilter <? super M,? super I>>(); 541 for (RowFilter <? super M, ? super I> filter : filters) { 542 if (filter == null) { 543 throw new IllegalArgumentException ( 544 "Filter must be non-null"); 545 } 546 this.filters.add(filter); 547 } 548 } 549 550 public boolean include(Entry<? extends M, ? extends I> value) { 551 for (RowFilter <? super M,? super I> filter : filters) { 552 if (filter.include(value)) { 553 return true; 554 } 555 } 556 return false; 557 } 558 } 559 560 561 private static class AndFilter<M,I> extends OrFilter<M,I> { 562 AndFilter(Iterable <? extends RowFilter <? super M,? super I>> filters) { 563 super(filters); 564 } 565 566 public boolean include(Entry<? extends M, ? extends I> value) { 567 for (RowFilter <? super M,? super I> filter : filters) { 568 if (!filter.include(value)) { 569 return false; 570 } 571 } 572 return true; 573 } 574 } 575 576 577 private static class NotFilter<M,I> extends RowFilter <M,I> { 578 private RowFilter <M,I> filter; 579 580 NotFilter(RowFilter <M,I> filter) { 581 if (filter == null) { 582 throw new IllegalArgumentException ( 583 "filter must be non-null"); 584 } 585 this.filter = filter; 586 } 587 588 public boolean include(Entry<? extends M, ? extends I> value) { 589 return !filter.include(value); 590 } 591 } 592 } 593 | Popular Tags |