Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import static com.google.common.base.Preconditions.checkNotNull;
  2.  
  3. import java.util.Set;
  4. import java.util.function.Function;
  5.  
  6. import com.google.common.collect.ImmutableSet;
  7.  
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. import ca.odell.glazedlists.matchers.AbstractMatcherEditor;
  12. import ca.odell.glazedlists.matchers.Matcher;
  13.  
  14. public final class SetMatcherEditor<E, O> extends AbstractMatcherEditor<E> {
  15.  
  16. // ~ Enumerations ----------------------------------------------------------------------------------------------
  17.  
  18. public enum Mode {
  19. EMPTY_MATCHES_NONE, EMPTY_MATCHES_ALL
  20. }
  21.  
  22. // ~ Static fields ---------------------------------------------------------------------------------------------
  23.  
  24. private static final Logger L = LoggerFactory.getLogger(SetMatcherEditor.class);
  25.  
  26. // ~ Static methods --------------------------------------------------------------------------------------------
  27.  
  28. public static <E, O> SetMatcherEditor<E, O> create(final Function<E, O> fn) {
  29. return create(fn, Mode.EMPTY_MATCHES_NONE);
  30. }
  31.  
  32. public static <E, O> SetMatcherEditor<E, O> create(final Function<E, O> fn, final Mode mode) {
  33. return new SetMatcherEditor<>(fn, mode);
  34. }
  35.  
  36. // ~ Instance fields -------------------------------------------------------------------------------------------
  37.  
  38. private final Function<E, O> function;
  39. private final Mode mode;
  40. private ImmutableSet<O> set;
  41.  
  42. // ~ Constructors ----------------------------------------------------------------------------------------------
  43.  
  44. private SetMatcherEditor(final Function<E, O> function, final Mode mode) {
  45. this.function = checkNotNull(function);
  46. this.mode = mode;
  47. this.set = ImmutableSet.of();
  48.  
  49. if (this.mode == Mode.EMPTY_MATCHES_ALL) {
  50. this.fireMatchAll();
  51. } else {
  52. this.fireMatchNone();
  53. }
  54. }
  55.  
  56. // ~ Methods ---------------------------------------------------------------------------------------------------
  57.  
  58. public void setMatchSet(final Set<O> newSet) {
  59. ImmutableSet<O> oldSet = this.set;
  60. this.set = ImmutableSet.copyOf(newSet);
  61.  
  62. if (oldSet.equals(this.set)) {
  63. L.debug("new set equals old -> no change to filter");
  64.  
  65. } else if (this.set.isEmpty()) {
  66. if (this.mode == Mode.EMPTY_MATCHES_ALL) {
  67. L.debug("empty set -> firing matchAll");
  68. this.fireMatchAll();
  69.  
  70. } else {
  71. L.debug("empty set -> firing matchNone");
  72. this.fireMatchNone();
  73. }
  74.  
  75. } else if (oldSet.isEmpty()) {
  76. L.debug("old set was empty, new set is not -> firing change");
  77. this.fireChanged(new SetMatcher<>(this.set, this.function));
  78.  
  79. } else if (oldSet.containsAll(this.set)) {
  80. L.debug("old set contains new set -> firing constrained");
  81. this.fireConstrained(new SetMatcher<>(this.set, this.function));
  82.  
  83. } else if (this.set.containsAll(oldSet)) {
  84. L.debug("new set contains old set -> firing relaxed");
  85. this.fireRelaxed(new SetMatcher<>(this.set, this.function));
  86. } else {
  87. L.debug("firing change");
  88. this.fireChanged(new SetMatcher<>(this.set, this.function));
  89. }
  90. }
  91.  
  92. // ~ Inner Classes ---------------------------------------------------------------------------------------------
  93.  
  94. private final static class SetMatcher<E, O> implements Matcher<E> {
  95.  
  96. // ~ Instance fields -------------------------------------------------------------------------------------------
  97.  
  98. private final ImmutableSet<O> matchSet;
  99. private final Function<E, O> fn;
  100.  
  101. // ~ Constructors ----------------------------------------------------------------------------------------------
  102.  
  103. private SetMatcher(final Set<O> matchSet, final Function<E, O> fn) {
  104. this.matchSet = ImmutableSet.copyOf(matchSet);
  105. this.fn = checkNotNull(fn);
  106. }
  107.  
  108. // ~ Methods ---------------------------------------------------------------------------------------------------
  109.  
  110. @Override
  111. public boolean matches(final E input) {
  112. return this.matchSet.contains(this.fn.apply(input));
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement