Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SimpleStringFilter.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. public boolean passesFilter(Object itemId, Item item) {
  38. final Property p = item.getItemProperty(propertyId);
  39. if (p == null || p.toString() == null) {
  40. return false;
  41. }
  42. final String value = ignoreCase ? p.toString().toLowerCase() : p
  43. .toString();
  44. if (onlyMatchPrefix) {
  45. if (!value.startsWith(filterString)) {
  46. return false;
  47. }
  48. } else {
  49. if (!value.contains(filterString)) {
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55. public boolean appliesToProperty(Object propertyId) {
  56. return this.propertyId.equals(propertyId);
  57. }
  58. @Override
  59. public boolean equals(Object obj) {
  60. // Only ones of the objects of the same class can be equal
  61. if (!(obj instanceof SimpleStringFilter)) {
  62. return false;
  63. }
  64. final SimpleStringFilter o = (SimpleStringFilter) obj;
  65. // Checks the properties one by one
  66. if (propertyId != o.propertyId && o.propertyId != null
  67. && !o.propertyId.equals(propertyId)) {
  68. return false;
  69. }
  70. if (filterString != o.filterString && o.filterString != null
  71. && !o.filterString.equals(filterString)) {
  72. return false;
  73. }
  74. if (ignoreCase != o.ignoreCase) {
  75. return false;
  76. }
  77. if (onlyMatchPrefix != o.onlyMatchPrefix) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. @Override
  83. public int hashCode() {
  84. return (propertyId != null ? propertyId.hashCode() : 0)
  85. ^ (filterString != null ? filterString.hashCode() : 0);
  86. }
  87. /**
  88. * Returns the property identifier to which this filter applies.
  89. *
  90. * @return property id
  91. */
  92. public Object getPropertyId() {
  93. return propertyId;
  94. }
  95. /**
  96. * Returns the filter string.
  97. *
  98. * Note: this method is intended only for implementations of lazy string
  99. * filters and may change in the future.
  100. *
  101. * @return filter string given to the constructor
  102. */
  103. public String getFilterString() {
  104. return filterString;
  105. }
  106. /**
  107. * Returns whether the filter is case-insensitive or case-sensitive.
  108. *
  109. * Note: this method is intended only for implementations of lazy string
  110. * filters and may change in the future.
  111. *
  112. * @return true if performing case-insensitive filtering, false for
  113. * case-sensitive
  114. */
  115. public boolean isIgnoreCase() {
  116. return ignoreCase;
  117. }
  118. /**
  119. * Returns true if the filter only applies to the beginning of the value
  120. * string, false for any location in the value.
  121. *
  122. * Note: this method is intended only for implementations of lazy string
  123. * filters and may change in the future.
  124. *
  125. * @return true if checking for matches at the beginning of the value only,
  126. * false if matching any part of value
  127. */
  128. public boolean isOnlyMatchPrefix() {
  129. return onlyMatchPrefix;
  130. }
  131. }