[New] search with contains() extended for matchAny or matchAll (default)

This commit is contained in:
Robert von Burg 2023-12-11 15:00:28 +01:00
parent 529c1d0e3b
commit 269bd83ded
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 33 additions and 12 deletions

View File

@ -63,6 +63,9 @@ public interface ExpressionBuilder {
default <T extends StrolchRootElement> SearchExpression<T> containsIgnoreCase(Object right) {
return element -> PredicatesSupport.containsIgnoreCase(right).matches(extract(element));
}
default <T extends StrolchRootElement> SearchExpression<T> containsIgnoreCaseMatchAny(Object right) {
return element -> PredicatesSupport.containsIgnoreCaseMatchAny(right).matches(extract(element));
}
default <T extends StrolchRootElement> SearchExpression<T> isIn(Object right) {
return element -> PredicatesSupport.isIn(right).matches(extract(element));

View File

@ -47,7 +47,11 @@ public class PredicatesSupport {
}
public static SearchPredicate contains(Object right) {
return new ContainsPredicate(right, false);
return new ContainsPredicate(right, false, false);
}
public static SearchPredicate containsMatchAny(Object right) {
return new ContainsPredicate(right, false, true);
}
public static SearchPredicate collectionContains(Object right) {
@ -55,7 +59,11 @@ public class PredicatesSupport {
}
public static SearchPredicate containsIgnoreCase(Object right) {
return new ContainsPredicate(right, true);
return new ContainsPredicate(right, true, false);
}
public static SearchPredicate containsIgnoreCaseMatchAny(Object right) {
return new ContainsPredicate(right, true, true);
}
public static SearchPredicate isIn(Object right) {

View File

@ -3,18 +3,20 @@ package li.strolch.search.predicates;
import li.strolch.utils.ObjectHelper;
/**
* Implements the contains predicate, delegating to {@link ObjectHelper#contains(Object, Object, boolean)}
* Implements the contains predicate, delegating to {@link ObjectHelper#contains(Object, Object, boolean, boolean)}
*/
public class ContainsPredicate extends AbstractSearchPredicate {
private final boolean ignoreCase;
private final boolean matchAny;
public ContainsPredicate(Object right, boolean ignoreCase) {
public ContainsPredicate(Object right, boolean ignoreCase, boolean matchAny) {
super(right);
this.ignoreCase = ignoreCase;
this.matchAny = matchAny;
}
@Override
public boolean matches(Object left) {
return ObjectHelper.contains(left, this.right, this.ignoreCase);
return ObjectHelper.contains(left, this.right, this.ignoreCase, !this.matchAny);
}
}

View File

@ -104,6 +104,10 @@ public class ObjectHelper {
}
public static boolean contains(Object left, Object right, boolean ignoreCase) {
return contains(left, right, ignoreCase, true);
}
public static boolean contains(Object left, Object right, boolean ignoreCase, boolean matchAll) {
if (left == null && right == null)
return true;
if (left == null)
@ -116,7 +120,7 @@ public class ObjectHelper {
if (right instanceof Collection<?> rightCollection) {
for (Object l : leftCollection) {
for (Object r : rightCollection) {
if (contains(l, r, ignoreCase))
if (contains(l, r, ignoreCase, matchAll))
return true;
}
}
@ -127,7 +131,7 @@ public class ObjectHelper {
if (right instanceof String[] rightArr) {
for (Object l : leftCollection) {
for (Object r : rightArr) {
if (contains(l, r, ignoreCase))
if (contains(l, r, ignoreCase, matchAll))
return true;
}
}
@ -136,7 +140,7 @@ public class ObjectHelper {
}
for (Object l : leftCollection) {
if (contains(l, right, ignoreCase))
if (contains(l, right, ignoreCase, matchAll))
return true;
}
@ -150,18 +154,22 @@ public class ObjectHelper {
if (ignoreCase) {
leftString = leftString.toLowerCase();
for (String s : rightArr) {
if (leftString.contains(s.toLowerCase()))
if (!matchAll && leftString.contains(s.toLowerCase()))
return true;
else if (matchAll && !leftString.contains(s.toLowerCase()))
return false;
}
} else {
for (String s : rightArr) {
if (leftString.contains(s))
if (!matchAll && leftString.contains(s))
return true;
else if (matchAll && !leftString.contains(s))
return false;
}
}
return false;
return matchAll;
}
if (right.getClass().isEnum())