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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. public boolean appliesToProperty(Object propertyId) {
  42. for (Filter filter : getFilters()) {
  43. if (filter.appliesToProperty(propertyId)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. @Override
  50. public boolean equals(Object obj) {
  51. if (obj == null || !getClass().equals(obj.getClass())) {
  52. return false;
  53. }
  54. AbstractJunctionFilter other = (AbstractJunctionFilter) obj;
  55. // contents comparison with equals()
  56. return Arrays.equals(filters.toArray(), other.filters.toArray());
  57. }
  58. @Override
  59. public int hashCode() {
  60. int hash = getFilters().size();
  61. for (Filter filter : filters) {
  62. hash = (hash << 1) ^ filter.hashCode();
  63. }
  64. return hash;
  65. }
  66. }