io.getunleash.strategy.constraints.StringConstraintOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
The newest version!
package io.getunleash.strategy.constraints;
import io.getunleash.Constraint;
import io.getunleash.UnleashContext;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
public class StringConstraintOperator implements ConstraintOperator {
private Locale comparisonLocale;
public StringConstraintOperator(Locale comparisonLocale) {
this.comparisonLocale = comparisonLocale;
}
@Override
public boolean evaluate(Constraint constraint, UnleashContext context) {
List values = constraint.getValues();
Optional contextValue = context.getByName(constraint.getContextName());
boolean caseInsensitive = constraint.isCaseInsensitive();
switch (constraint.getOperator()) {
case IN:
return isIn(values, contextValue, caseInsensitive);
case NOT_IN:
return !isIn(values, contextValue, caseInsensitive);
case STR_CONTAINS:
return contains(values, contextValue, caseInsensitive);
case STR_STARTS_WITH:
return startsWith(values, contextValue, caseInsensitive);
case STR_ENDS_WITH:
return endsWith(values, contextValue, caseInsensitive);
default:
return false;
}
}
private boolean endsWith(
List values, Optional contextValue, boolean caseInsensitive) {
return contextValue
.map(
c ->
values.stream()
.anyMatch(
v -> {
if (caseInsensitive) {
return c.toLowerCase(comparisonLocale)
.endsWith(
v.toLowerCase(
comparisonLocale));
} else {
return c.endsWith(v);
}
}))
.orElse(false);
}
private boolean startsWith(
List values, Optional contextValue, boolean caseInsensitive) {
return contextValue
.map(
actualContextValue ->
values.stream()
.anyMatch(
value -> {
if (caseInsensitive) {
return actualContextValue
.toLowerCase(comparisonLocale)
.startsWith(
value.toLowerCase(
comparisonLocale));
} else {
return actualContextValue.startsWith(value);
}
}))
.orElse(false);
}
private boolean contains(
List values, Optional contextValue, boolean caseInsensitive) {
return contextValue
.map(
c ->
values.stream()
.anyMatch(
v -> {
if (caseInsensitive) {
return c.toLowerCase(comparisonLocale)
.contains(
v.toLowerCase(
comparisonLocale));
} else {
return c.contains(v);
}
}))
.orElse(false);
}
private boolean isIn(List values, Optional value, boolean caseInsensitive) {
return value.map(
v ->
values.stream()
.anyMatch(
c -> {
if (caseInsensitive) {
return c.equalsIgnoreCase(v);
} else {
return c.equals(v);
}
}))
.orElse(false);
}
}