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.6KB

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