jodd.util.InExRules Maven / Gradle / Ivy
Show all versions of redisson-all Show documentation
package jodd.util;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Simple ruling engine for includes and excludes rules.
*/
public class InExRules {
public static InExRules blacklist() {
return new InExRules<>(InExType.BLACKLIST, WILDCARD_STRING_MATCHER);
}
public static InExRules whitelist() {
return new InExRules<>(InExType.WHITELIST, WILDCARD_STRING_MATCHER);
}
public enum InExType {
WHITELIST,
BLACKLIST
}
public static final Function> WILDCARD_STRING_MATCHER = pattern -> new Predicate() {
final String p = pattern;
@Override
public boolean test(final String value) {
return Wildcard.match(value, p);
}
};
public InExRules(final InExType inExType, final Function> factory) {
this.inExType = inExType;
this.factory = factory;
}
private final InExType inExType;
private final Function
> factory;
private final List> includePatterns = new ArrayList<>();
private final List> excludePatterns = new ArrayList<>();
public void include(final P pattern) {
final Predicate matcher = factory.apply(pattern);
this.includePatterns.add(matcher);
}
public void exclude(final P pattern) {
final Predicate matcher = factory.apply(pattern);
this.excludePatterns.add(matcher);
}
public boolean match(final V value) {
boolean flag = inExType == InExType.BLACKLIST;
if (inExType == InExType.BLACKLIST) {
flag = processExcludes(value, flag);
flag = processIncludes(value, flag);
}
else {
flag = processIncludes(value, flag);
flag = processExcludes(value, flag);
}
return flag;
}
public boolean apply(final V value, boolean flag) {
if (inExType == InExType.BLACKLIST) {
flag = processExcludes(value, flag);
flag = processIncludes(value, flag);
}
else {
flag = processIncludes(value, flag);
flag = processExcludes(value, flag);
}
return flag;
}
protected boolean processIncludes(final V value, boolean include) {
if (includePatterns.isEmpty()) {
return include;
}
if (include) {
return include;
}
for (final Predicate includePredicate : includePatterns) {
if (includePredicate.test(value)) {
include = true;
break;
}
}
return include;
}
protected boolean processExcludes(final V value, boolean include) {
if (excludePatterns.isEmpty()) {
return include;
}
if (!include) {
return include;
}
for (final Predicate excludePredicate : excludePatterns) {
if (excludePredicate.test(value)) {
include = false;
break;
}
}
return include;
}
}