[New] Added isIn() as new search predicate

This commit is contained in:
Robert von Burg 2018-04-19 18:11:31 +02:00
parent 164fc0ac80
commit 7deafa533d
5 changed files with 85 additions and 8 deletions

View File

@ -47,6 +47,14 @@ public interface ExpressionBuilder<T extends StrolchRootElement> {
return element -> PredicatesSupport.containsIgnoreCase(right).matches(extract(element));
}
default SearchExpression<T> isIn(Object right) {
return element -> PredicatesSupport.isIn(right).matches(extract(element));
}
default SearchExpression<T> isInIgnoreCase(Object right) {
return element -> PredicatesSupport.isInIgnoreCase(right).matches(extract(element));
}
default SearchExpression<T> inRange(DateRange range) {
return element -> PredicatesSupport.inRange(range).matches(extract(element));
}

View File

@ -47,6 +47,14 @@ public class PredicatesSupport {
return left -> ObjectHelper.contains(left, right, true);
}
public static SearchPredicate isIn(Object right) {
return left -> ObjectHelper.isIn(left, right, false);
}
public static SearchPredicate isInIgnoreCase(Object right) {
return left -> ObjectHelper.isIn(left, right, true);
}
public static SearchPredicate inRange(DateRange range) {
return left -> range.contains((Date) left);
}

View File

@ -44,6 +44,22 @@ public interface SearchPredicates {
return PredicatesSupport.containsIgnoreCase(right);
}
default SearchPredicate isIn(Object right) {
return PredicatesSupport.isIn(right);
}
default SearchPredicate isIn(Object... right) {
return PredicatesSupport.isIn(right);
}
default SearchPredicate isInIgnoreCase(Object right) {
return PredicatesSupport.isInIgnoreCase(right);
}
default SearchPredicate isInIgnoreCase(Object... right) {
return PredicatesSupport.isInIgnoreCase(right);
}
default SearchPredicate inRange(DateRange range) {
return PredicatesSupport.inRange(range);
}

View File

@ -365,6 +365,10 @@ public class StrolchSearchTest {
.and(param(BAG_ID, PARAM_STRING_ID, isNotEqualTo("dfgdfg")))
.and(param(BAG_ID, PARAM_STRING_ID, isNotEqualToIgnoreCase("dfgdfg")))
.and(param(BAG_ID, PARAM_STRING_ID, contains("rol")))
.and(param(BAG_ID, PARAM_STRING_ID, isIn("Strolch")))
.and(param(BAG_ID, PARAM_STRING_ID, isIn("Strolch", "sdf")))
.and(param(BAG_ID, PARAM_STRING_ID, isInIgnoreCase("strolch")))
.and(param(BAG_ID, PARAM_STRING_ID, isInIgnoreCase("strolch", "dfgdfg")))
.and(param(BAG_ID, PARAM_STRING_ID, contains(new String[] { "Str", "rol" })))
.and(param(BAG_ID, PARAM_STRING_ID, containsIgnoreCase("ROL")))
.and(param(BAG_ID, PARAM_STRING_ID, containsIgnoreCase(new String[] { "STR", "ROL" })))
@ -375,12 +379,19 @@ public class StrolchSearchTest {
.and(param(BAG_ID, PARAM_BOOLEAN_ID, isEqualTo(true)))
.and(param(BAG_ID, PARAM_DATE_ID, isEqualTo(new Date(1354295525628L))))
.and(param(BAG_ID, PARAM_INTEGER_ID, isEqualTo(77)))
.and(param(BAG_ID, PARAM_INTEGER_ID, isEqualTo(77))) //
.and(param(BAG_ID, PARAM_INTEGER_ID, isIn(77))) //
.and(param(BAG_ID, PARAM_INTEGER_ID, isIn(77, 88))) //
.and(param(BAG_ID, PARAM_INTEGER_ID, isIn(asList(77, 88)))) //
.and(param(BAG_ID, PARAM_LIST_FLOAT_ID, isEqualTo(asList(6.0D, 11.0D, 16.0D))))
.and(param(BAG_ID, PARAM_LIST_FLOAT_ID, contains(singletonList(6.0D))))
.and(param(BAG_ID, PARAM_LIST_FLOAT_ID, contains(asList(6.0D, 11.0D))))
.and(param(BAG_ID, PARAM_LIST_INTEGER_ID, isEqualTo(asList(5, 10, 15))))
.and(param(BAG_ID, PARAM_LIST_INTEGER_ID, contains(asList(5, 10))))
.and(param(BAG_ID, PARAM_LIST_LONG_ID, isEqualTo(asList(7L, 12L, 17L))))
.and(param(BAG_ID, PARAM_LIST_STRING_ID, isEqualTo(asList("Hello", "World"))))

View File

@ -56,8 +56,7 @@ public class ObjectHelper {
Collection<?> collection = (Collection) left;
if (right instanceof Collection)
return collection.containsAll((Collection) right);
else
return collection.contains(right);
return collection.contains(right);
}
if (left instanceof String) {
@ -83,8 +82,9 @@ public class ObjectHelper {
return true;
}
}
} else if (right instanceof String) {
if (right instanceof String) {
String subStr = (String) right;
if (ignoreCase)
@ -97,6 +97,42 @@ public class ObjectHelper {
throw new IllegalArgumentException("Unhandled type combination " + left.getClass() + " / " + right.getClass());
}
public static boolean isIn(Object left, Object right, boolean ignoreCase) {
if (left == null && right == null)
return true;
if (left == null)
return false;
if (right == null)
return false;
if (right instanceof Collection) {
Collection<?> collection = (Collection) right;
for (Object o : collection) {
if (equals(left, o, ignoreCase))
return true;
}
return false;
}
if (right instanceof Object[]) {
Object[] arr = (Object[]) right;
for (Object o : arr) {
if (equals(left, o, ignoreCase))
return true;
}
return false;
}
if (right instanceof String || right instanceof Number) {
return equals(left, right, ignoreCase);
}
throw new IllegalArgumentException("Unhandled type combination " + left.getClass() + " / " + right.getClass());
}
public static boolean startsWith(Object left, Object right, boolean ignoreCase) {
if (left == null && right == null)
return true;
@ -111,8 +147,7 @@ public class ObjectHelper {
if (ignoreCase)
return str.toLowerCase().startsWith(subStr.toLowerCase());
else
return str.startsWith(subStr);
return str.startsWith(subStr);
}
throw new IllegalArgumentException("Unhandled type combination " + left.getClass() + " / " + right.getClass());
@ -132,8 +167,7 @@ public class ObjectHelper {
if (ignoreCase)
return str.toLowerCase().endsWith(subStr.toLowerCase());
else
return str.endsWith(subStr);
return str.endsWith(subStr);
}
throw new IllegalArgumentException("Unhandled type combination " + left.getClass() + " / " + right.getClass());