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 5.1KB

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