Skip to content

Instantly share code, notes, and snippets.

@fab1an
Created August 30, 2016 11:40
Show Gist options
  • Save fab1an/008828eed6f9906693c4da8997ee67ec to your computer and use it in GitHub Desktop.
Save fab1an/008828eed6f9906693c4da8997ee67ec to your computer and use it in GitHub Desktop.
Generic SetMatcherEditor for GlazedLists
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));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment