Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static com.google.common.base.Preconditions.checkNotNull;
- import java.util.Set;
- import java.util.function.Function;
- import com.google.common.collect.ImmutableSet;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import ca.odell.glazedlists.matchers.AbstractMatcherEditor;
- import ca.odell.glazedlists.matchers.Matcher;
- public final class SetMatcherEditor<E, O> extends AbstractMatcherEditor<E> {
- // ~ Enumerations ----------------------------------------------------------------------------------------------
- public enum Mode {
- EMPTY_MATCHES_NONE, EMPTY_MATCHES_ALL
- }
- // ~ Static fields ---------------------------------------------------------------------------------------------
- private static final Logger L = LoggerFactory.getLogger(SetMatcherEditor.class);
- // ~ Static methods --------------------------------------------------------------------------------------------
- public static <E, O> SetMatcherEditor<E, O> create(final Function<E, O> fn) {
- return create(fn, Mode.EMPTY_MATCHES_NONE);
- }
- public static <E, O> SetMatcherEditor<E, O> create(final Function<E, O> fn, final Mode mode) {
- return new SetMatcherEditor<>(fn, mode);
- }
- // ~ Instance fields -------------------------------------------------------------------------------------------
- private final Function<E, O> function;
- private final Mode mode;
- private ImmutableSet<O> set;
- // ~ Constructors ----------------------------------------------------------------------------------------------
- private SetMatcherEditor(final Function<E, O> function, final Mode mode) {
- this.function = checkNotNull(function);
- this.mode = mode;
- this.set = ImmutableSet.of();
- if (this.mode == Mode.EMPTY_MATCHES_ALL) {
- this.fireMatchAll();
- } else {
- this.fireMatchNone();
- }
- }
- // ~ Methods ---------------------------------------------------------------------------------------------------
- public void setMatchSet(final Set<O> newSet) {
- ImmutableSet<O> oldSet = this.set;
- this.set = ImmutableSet.copyOf(newSet);
- if (oldSet.equals(this.set)) {
- L.debug("new set equals old -> no change to filter");
- } else if (this.set.isEmpty()) {
- if (this.mode == Mode.EMPTY_MATCHES_ALL) {
- L.debug("empty set -> firing matchAll");
- this.fireMatchAll();
- } else {
- L.debug("empty set -> firing matchNone");
- this.fireMatchNone();
- }
- } else if (oldSet.isEmpty()) {
- L.debug("old set was empty, new set is not -> firing change");
- this.fireChanged(new SetMatcher<>(this.set, this.function));
- } else if (oldSet.containsAll(this.set)) {
- L.debug("old set contains new set -> firing constrained");
- this.fireConstrained(new SetMatcher<>(this.set, this.function));
- } else if (this.set.containsAll(oldSet)) {
- L.debug("new set contains old set -> firing relaxed");
- this.fireRelaxed(new SetMatcher<>(this.set, this.function));
- } else {
- L.debug("firing change");
- this.fireChanged(new SetMatcher<>(this.set, this.function));
- }
- }
- // ~ Inner Classes ---------------------------------------------------------------------------------------------
- private final static class SetMatcher<E, O> implements Matcher<E> {
- // ~ Instance fields -------------------------------------------------------------------------------------------
- private final ImmutableSet<O> matchSet;
- private final Function<E, O> fn;
- // ~ Constructors ----------------------------------------------------------------------------------------------
- private SetMatcher(final Set<O> matchSet, final Function<E, O> fn) {
- this.matchSet = ImmutableSet.copyOf(matchSet);
- this.fn = checkNotNull(fn);
- }
- // ~ Methods ---------------------------------------------------------------------------------------------------
- @Override
- public boolean matches(final E input) {
- return this.matchSet.contains(this.fn.apply(input));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement