[New] Added ValueSearch API for non-strolch object

This commit is contained in:
Robert von Burg 2019-01-14 14:33:06 +01:00
parent b297483280
commit 37fb641892
7 changed files with 163 additions and 9 deletions

View File

@ -5,8 +5,6 @@ import li.strolch.utils.collections.DateRange;
/**
* An interface to add search expressions to easily discover the possible search expressions
*
* @param <T>
*/
public interface ExpressionBuilder {
@ -52,8 +50,16 @@ public interface ExpressionBuilder {
return element -> PredicatesSupport.contains(right).matches(extract(element));
}
/**
* @deprecated use {@link #collectionContains(Object)} instead
*/
@Deprecated
default <T extends StrolchRootElement> SearchExpression<T> listContains(Object right) {
return element -> PredicatesSupport.listContains(right).matches(extract(element));
return element -> PredicatesSupport.collectionContains(right).matches(extract(element));
}
default <T extends StrolchRootElement> SearchExpression<T> collectionContains(Object right) {
return element -> PredicatesSupport.collectionContains(right).matches(extract(element));
}
default <T extends StrolchRootElement> SearchExpression<T> containsIgnoreCase(Object right) {

View File

@ -43,9 +43,9 @@ public class PredicatesSupport {
public static SearchPredicate contains(Object right) {
return new ContainsPredicate(right, false);
}
public static SearchPredicate listContains(Object right) {
return new ListContainsPredicate(right, false);
public static SearchPredicate collectionContains(Object right) {
return new CollectionContainsPredicate(right, false);
}
public static SearchPredicate containsIgnoreCase(Object right) {

View File

@ -48,7 +48,7 @@ public interface SearchPredicates {
}
default SearchPredicate listContains(Object right) {
return PredicatesSupport.listContains(right);
return PredicatesSupport.collectionContains(right);
}
default SearchPredicate isIn(Object right) {

View File

@ -0,0 +1,32 @@
package li.strolch.search;
import java.util.Collection;
import java.util.stream.Stream;
public class ValueSearch<T> implements SearchPredicates {
private ValueSearchExpression<T> expression;
public ValueSearch<T> where(ValueSearchExpression<T> expression) {
if (this.expression == null)
this.expression = expression;
else
this.expression = this.expression.and(expression);
return this;
}
/**
* Performs the actual search on the given input list
*
* @return the search result
*/
public SearchResult<T> search(Collection<T> input) {
Stream<T> stream = input.stream();
if (this.expression != null)
stream = stream.filter(e -> this.expression.matches(e));
return new SearchResult<>(stream);
}
}

View File

@ -0,0 +1,51 @@
package li.strolch.search;
/**
* Defines a search expression interface to perform where clauses on an object
*/
@FunctionalInterface
public interface ValueSearchExpression<T> {
/**
* See if this search expression matches the given element
*
* @param element
* the element to match
*
* @return true if the element is matched with this search expression
*/
boolean matches(T element);
/**
* Returns a new search expression where this search expression is ORed with the given search expression
*
* @param right
* the right hand side of the search expression
*
* @return the new search expression with an internal OR of the two search expressions
*/
default ValueSearchExpression<T> or(ValueSearchExpression<T> right) {
return element -> this.matches(element) || right.matches(element);
}
/**
* Returns a new search expression where this search expression is ANDed with the given search expression
*
* @param right
* the right hand side of the search expression
*
* @return the new search expression with an internal AND of the two search expressions
*/
default ValueSearchExpression<T> and(ValueSearchExpression<T> right) {
return element -> this.matches(element) && right.matches(element);
}
/**
* Negates this search expression
*
* @return a new search expression where this search expression is negated
*/
default ValueSearchExpression<T> not() {
return element -> !this.matches(element);
}
}

View File

@ -0,0 +1,65 @@
package li.strolch.search;
import java.util.Date;
import java.util.function.Function;
import li.strolch.utils.collections.DateRange;
public class ValueSearchExpressionBuilder {
public static <T> ValueSearchExpression<T> isEqualTo(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isEqualTo(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> isNotEqualTo(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isNotEqualTo(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> isEqualToIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isEqualToIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> isNotEqualToIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isNotEqualToIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> startsWith(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.startsWith(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> startsWithIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.startsWithIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> endsWith(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.endsWith(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> endsWithIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.endsWithIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> contains(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.contains(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> collectionContains(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.collectionContains(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> containsIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.containsIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> isIn(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isIn(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> isInIgnoreCase(Function<T, Object> extractor, Object right) {
return element -> PredicatesSupport.isInIgnoreCase(right).matches(extractor.apply(element));
}
public static <T> ValueSearchExpression<T> inRange(Function<T, Date> extractor, DateRange range) {
return element -> PredicatesSupport.inRange(range).matches(extractor.apply(element));
}
}

View File

@ -5,10 +5,10 @@ import li.strolch.utils.ObjectHelper;
/**
* Implements the contains predicate, delegating to {@link ObjectHelper#isIn(Object, Object, boolean)}
*/
public class ListContainsPredicate extends AbstractSearchPredicate {
public class CollectionContainsPredicate extends AbstractSearchPredicate {
private boolean ignoreCase;
public ListContainsPredicate(Object right, boolean ignoreCase) {
public CollectionContainsPredicate(Object right, boolean ignoreCase) {
super(right);
this.ignoreCase = ignoreCase;
}