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.

AbstractJunctionFilter.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.filter;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import com.vaadin.data.Container.Filter;
  9. /**
  10. * Abstract base class for filters that are composed of multiple sub-filters.
  11. *
  12. * The method {@link #appliesToProperty(Object)} is provided to help
  13. * implementing {@link Filter} for in-memory filters.
  14. *
  15. * @since 6.6
  16. */
  17. public abstract class AbstractJunctionFilter implements Filter {
  18. protected final Collection<Filter> filters;
  19. public AbstractJunctionFilter(Filter... filters) {
  20. this.filters = Collections.unmodifiableCollection(Arrays
  21. .asList(filters));
  22. }
  23. /**
  24. * Returns an unmodifiable collection of the sub-filters of this composite
  25. * filter.
  26. *
  27. * @return
  28. */
  29. public Collection<Filter> getFilters() {
  30. return filters;
  31. }
  32. /**
  33. * Returns true if a change in the named property may affect the filtering
  34. * result. If some of the sub-filters are not in-memory filters, true is
  35. * returned.
  36. *
  37. * By default, all sub-filters are iterated to check if any of them applies.
  38. * If there are no sub-filters, false is returned - override in subclasses
  39. * to change this behavior.
  40. */
  41. @Override
  42. public boolean appliesToProperty(Object propertyId) {
  43. for (Filter filter : getFilters()) {
  44. if (filter.appliesToProperty(propertyId)) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. @Override
  51. public boolean equals(Object obj) {
  52. if (obj == null || !getClass().equals(obj.getClass())) {
  53. return false;
  54. }
  55. AbstractJunctionFilter other = (AbstractJunctionFilter) obj;
  56. // contents comparison with equals()
  57. return Arrays.equals(filters.toArray(), other.filters.toArray());
  58. }
  59. @Override
  60. public int hashCode() {
  61. int hash = getFilters().size();
  62. for (Filter filter : filters) {
  63. hash = (hash << 1) ^ filter.hashCode();
  64. }
  65. return hash;
  66. }
  67. }