From 7deafa533d02eb0a090cf4e2003aceed39055fd3 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 19 Apr 2018 18:11:31 +0200 Subject: [PATCH] [New] Added isIn() as new search predicate --- .../li/strolch/search/ExpressionBuilder.java | 8 ++++ .../li/strolch/search/PredicatesSupport.java | 8 ++++ .../li/strolch/search/SearchPredicates.java | 16 +++++++ .../li/strolch/search/StrolchSearchTest.java | 13 ++++- .../java/li/strolch/utils/ObjectHelper.java | 48 ++++++++++++++++--- 5 files changed, 85 insertions(+), 8 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/search/ExpressionBuilder.java b/li.strolch.agent/src/main/java/li/strolch/search/ExpressionBuilder.java index 1dc3e689e..2fe18a3f4 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/ExpressionBuilder.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/ExpressionBuilder.java @@ -47,6 +47,14 @@ public interface ExpressionBuilder { return element -> PredicatesSupport.containsIgnoreCase(right).matches(extract(element)); } + default SearchExpression isIn(Object right) { + return element -> PredicatesSupport.isIn(right).matches(extract(element)); + } + + default SearchExpression isInIgnoreCase(Object right) { + return element -> PredicatesSupport.isInIgnoreCase(right).matches(extract(element)); + } + default SearchExpression inRange(DateRange range) { return element -> PredicatesSupport.inRange(range).matches(extract(element)); } diff --git a/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java b/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java index 014a733dd..00393cf65 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/PredicatesSupport.java @@ -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); } diff --git a/li.strolch.agent/src/main/java/li/strolch/search/SearchPredicates.java b/li.strolch.agent/src/main/java/li/strolch/search/SearchPredicates.java index 22bb0ba33..ab914baeb 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/SearchPredicates.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/SearchPredicates.java @@ -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); } diff --git a/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java b/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java index e6d72d18b..091420734 100644 --- a/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java +++ b/li.strolch.agent/src/test/java/li/strolch/search/StrolchSearchTest.java @@ -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")))) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/ObjectHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/ObjectHelper.java index e98d4c62b..c855cf6a6 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/ObjectHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/ObjectHelper.java @@ -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());