You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Filter.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.vaadin.data.util;
  2. import java.io.Serializable;
  3. import com.vaadin.data.Item;
  4. import com.vaadin.data.Property;
  5. /**
  6. * A default filter that can be used to implement
  7. * {@link com.vaadin.data.Container.Filterable}.
  8. *
  9. * @since 5.4
  10. */
  11. @SuppressWarnings("serial")
  12. public class Filter implements Serializable {
  13. final Object propertyId;
  14. final String filterString;
  15. final boolean ignoreCase;
  16. final boolean onlyMatchPrefix;
  17. Filter(Object propertyId, String filterString, boolean ignoreCase,
  18. boolean onlyMatchPrefix) {
  19. this.propertyId = propertyId;
  20. ;
  21. this.filterString = ignoreCase ? filterString.toLowerCase()
  22. : filterString;
  23. this.ignoreCase = ignoreCase;
  24. this.onlyMatchPrefix = onlyMatchPrefix;
  25. }
  26. /**
  27. * Check if an item passes the filter.
  28. *
  29. * @param item
  30. * @return true if the item is accepted by this filter
  31. */
  32. public boolean passesFilter(Item item) {
  33. final Property p = item.getItemProperty(propertyId);
  34. if (p == null || p.toString() == null) {
  35. return false;
  36. }
  37. final String value = ignoreCase ? p.toString().toLowerCase() : p
  38. .toString();
  39. if (onlyMatchPrefix) {
  40. if (!value.startsWith(filterString)) {
  41. return false;
  42. }
  43. } else {
  44. if (!value.contains(filterString)) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. @Override
  51. public boolean equals(Object obj) {
  52. // Only ones of the objects of the same class can be equal
  53. if (!(obj instanceof Filter)) {
  54. return false;
  55. }
  56. final Filter o = (Filter) obj;
  57. // Checks the properties one by one
  58. if (propertyId != o.propertyId && o.propertyId != null
  59. && !o.propertyId.equals(propertyId)) {
  60. return false;
  61. }
  62. if (filterString != o.filterString && o.filterString != null
  63. && !o.filterString.equals(filterString)) {
  64. return false;
  65. }
  66. if (ignoreCase != o.ignoreCase) {
  67. return false;
  68. }
  69. if (onlyMatchPrefix != o.onlyMatchPrefix) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. @Override
  75. public int hashCode() {
  76. return (propertyId != null ? propertyId.hashCode() : 0)
  77. ^ (filterString != null ? filterString.hashCode() : 0);
  78. }
  79. }