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.

SimpleStringFilter.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.filter;
  5. import com.vaadin.data.Container.Filter;
  6. import com.vaadin.data.Item;
  7. import com.vaadin.data.Property;
  8. /**
  9. * Simple string filter for matching items that start with or contain a
  10. * specified string. The matching can be case-sensitive or case-insensitive.
  11. *
  12. * This filter also directly supports in-memory filtering. When performing
  13. * in-memory filtering, values of other types are converted using toString(),
  14. * but other (lazy container) implementations do not need to perform such
  15. * conversions and might not support values of different types.
  16. *
  17. * Note that this filter is modeled after the pre-6.6 filtering mechanisms, and
  18. * might not be very efficient e.g. for database filtering.
  19. *
  20. * TODO this might still change
  21. *
  22. * @since 6.6
  23. */
  24. public final class SimpleStringFilter implements Filter {
  25. final Object propertyId;
  26. final String filterString;
  27. final boolean ignoreCase;
  28. final boolean onlyMatchPrefix;
  29. public SimpleStringFilter(Object propertyId, String filterString,
  30. boolean ignoreCase, boolean onlyMatchPrefix) {
  31. this.propertyId = propertyId;
  32. this.filterString = ignoreCase ? filterString.toLowerCase()
  33. : filterString;
  34. this.ignoreCase = ignoreCase;
  35. this.onlyMatchPrefix = onlyMatchPrefix;
  36. }
  37. @Override
  38. public boolean passesFilter(Object itemId, Item item) {
  39. final Property<?> p = item.getItemProperty(propertyId);
  40. if (p == null) {
  41. return false;
  42. }
  43. Object propertyValue = p.getValue();
  44. if (propertyValue == null) {
  45. return false;
  46. }
  47. final String value = ignoreCase ? propertyValue.toString()
  48. .toLowerCase() : propertyValue.toString();
  49. if (onlyMatchPrefix) {
  50. if (!value.startsWith(filterString)) {
  51. return false;
  52. }
  53. } else {
  54. if (!value.contains(filterString)) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. @Override
  61. public boolean appliesToProperty(Object propertyId) {
  62. return this.propertyId.equals(propertyId);
  63. }
  64. @Override
  65. public boolean equals(Object obj) {
  66. // Only ones of the objects of the same class can be equal
  67. if (!(obj instanceof SimpleStringFilter)) {
  68. return false;
  69. }
  70. final SimpleStringFilter o = (SimpleStringFilter) obj;
  71. // Checks the properties one by one
  72. if (propertyId != o.propertyId && o.propertyId != null
  73. && !o.propertyId.equals(propertyId)) {
  74. return false;
  75. }
  76. if (filterString != o.filterString && o.filterString != null
  77. && !o.filterString.equals(filterString)) {
  78. return false;
  79. }
  80. if (ignoreCase != o.ignoreCase) {
  81. return false;
  82. }
  83. if (onlyMatchPrefix != o.onlyMatchPrefix) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. @Override
  89. public int hashCode() {
  90. return (propertyId != null ? propertyId.hashCode() : 0)
  91. ^ (filterString != null ? filterString.hashCode() : 0);
  92. }
  93. /**
  94. * Returns the property identifier to which this filter applies.
  95. *
  96. * @return property id
  97. */
  98. public Object getPropertyId() {
  99. return propertyId;
  100. }
  101. /**
  102. * Returns the filter string.
  103. *
  104. * Note: this method is intended only for implementations of lazy string
  105. * filters and may change in the future.
  106. *
  107. * @return filter string given to the constructor
  108. */
  109. public String getFilterString() {
  110. return filterString;
  111. }
  112. /**
  113. * Returns whether the filter is case-insensitive or case-sensitive.
  114. *
  115. * Note: this method is intended only for implementations of lazy string
  116. * filters and may change in the future.
  117. *
  118. * @return true if performing case-insensitive filtering, false for
  119. * case-sensitive
  120. */
  121. public boolean isIgnoreCase() {
  122. return ignoreCase;
  123. }
  124. /**
  125. * Returns true if the filter only applies to the beginning of the value
  126. * string, false for any location in the value.
  127. *
  128. * Note: this method is intended only for implementations of lazy string
  129. * filters and may change in the future.
  130. *
  131. * @return true if checking for matches at the beginning of the value only,
  132. * false if matching any part of value
  133. */
  134. public boolean isOnlyMatchPrefix() {
  135. return onlyMatchPrefix;
  136. }
  137. }