-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract classes to top level packages
Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>
- Loading branch information
Artur Ciocanu
committed
Jul 25, 2024
1 parent
a126fe7
commit 9dbdadc
Showing
5 changed files
with
275 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...oot/sdk-spring-core/src/main/java/io/dapr/spring/data/repository/query/DaprPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.dapr.spring.data.repository.query; | ||
|
||
import org.springframework.beans.BeanWrapper; | ||
import org.springframework.data.mapping.PropertyPath; | ||
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
public class DaprPredicate implements Predicate<Object> { | ||
|
||
private final PropertyPath path; | ||
private final Function<Object, Boolean> check; | ||
private final Object value; | ||
|
||
public DaprPredicate(PropertyPath path, Object expected) { | ||
this(path, expected, (valueToCompare) -> ObjectUtils.nullSafeEquals(valueToCompare, expected)); | ||
} | ||
|
||
|
||
/** | ||
* Creates a new {@link DaprPredicate}. | ||
* | ||
* @param path The path to the property to compare. | ||
* @param value The value to compare. | ||
* @param check The function to check the value. | ||
*/ | ||
public DaprPredicate(PropertyPath path, Object value, Function<Object, Boolean> check) { | ||
this.path = path; | ||
this.check = check; | ||
this.value = value; | ||
} | ||
|
||
public PropertyPath getPath() { | ||
return path; | ||
} | ||
|
||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean test(Object o) { | ||
Object value = getValueByPath(o, path); | ||
return check.apply(value); | ||
} | ||
|
||
private Object getValueByPath(Object root, PropertyPath path) { | ||
Object currentValue = root; | ||
|
||
for (PropertyPath currentPath : path) { | ||
currentValue = wrap(currentValue).getPropertyValue(currentPath.getSegment()); | ||
|
||
if (currentValue == null) { | ||
break; | ||
} | ||
} | ||
|
||
return currentValue; | ||
} | ||
|
||
private BeanWrapper wrap(Object o) { | ||
return new DirectFieldAccessFallbackBeanWrapper(o); | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
...-spring-core/src/main/java/io/dapr/spring/data/repository/query/DaprPredicateBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright 2024 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.spring.data.repository.query; | ||
|
||
import org.springframework.data.repository.query.parser.Part; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.util.comparator.Comparators; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
class DaprPredicateBuilder { | ||
|
||
private final Part part; | ||
|
||
private DaprPredicateBuilder(Part part) { | ||
this.part = part; | ||
} | ||
|
||
static DaprPredicateBuilder propertyValueOf(Part part) { | ||
return new DaprPredicateBuilder(part); | ||
} | ||
|
||
Predicate<Object> isTrue() { | ||
return new DaprPredicate(part.getProperty(), true); | ||
} | ||
|
||
public Predicate<Object> isFalse() { | ||
return new DaprPredicate(part.getProperty(), false); | ||
} | ||
|
||
public Predicate<Object> isEqualTo(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (!ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) { | ||
if (o instanceof String s1 && value instanceof String s2) { | ||
return s1.equalsIgnoreCase(s2); | ||
} | ||
} | ||
|
||
return ObjectUtils.nullSafeEquals(o, value); | ||
}); | ||
} | ||
|
||
public Predicate<Object> isNull() { | ||
return new DaprPredicate(part.getProperty(), null, Objects::isNull); | ||
} | ||
|
||
public Predicate<Object> isNotNull() { | ||
return isNull().negate(); | ||
} | ||
|
||
public Predicate<Object> isLessThan(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) == -1); | ||
} | ||
|
||
public Predicate<Object> isLessThanEqual(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) <= 0); | ||
} | ||
|
||
public Predicate<Object> isGreaterThan(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) == 1); | ||
} | ||
|
||
public Predicate<Object> isGreaterThanEqual(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> Comparators.nullsHigh().compare(o, value) >= 0); | ||
} | ||
|
||
public Predicate<Object> matches(Pattern pattern) { | ||
return new DaprPredicate(part.getProperty(), null, o -> { | ||
if (o == null) { | ||
return false; | ||
} | ||
|
||
return pattern.matcher(o.toString()).find(); | ||
}); | ||
} | ||
|
||
public Predicate<Object> matches(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (o == null || value == null) { | ||
return ObjectUtils.nullSafeEquals(o, value); | ||
} | ||
|
||
if (value instanceof Pattern pattern) { | ||
return pattern.matcher(o.toString()).find(); | ||
} | ||
|
||
return o.toString().matches(value.toString()); | ||
}); | ||
} | ||
|
||
public Predicate<Object> in(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (value instanceof Collection<?> collection) { | ||
if (o instanceof Collection<?> subSet) { | ||
return collection.containsAll(subSet); | ||
} | ||
|
||
return collection.contains(o); | ||
} | ||
|
||
if (ObjectUtils.isArray(value)) { | ||
return ObjectUtils.containsElement(ObjectUtils.toObjectArray(value), value); | ||
} | ||
|
||
return false; | ||
}); | ||
} | ||
|
||
public Predicate<Object> contains(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (o == null) { | ||
return false; | ||
} | ||
|
||
if (o instanceof Collection<?> collection) { | ||
return collection.contains(value); | ||
} | ||
|
||
if (ObjectUtils.isArray(o)) { | ||
return ObjectUtils.containsElement(ObjectUtils.toObjectArray(o), value); | ||
} | ||
|
||
if (o instanceof Map<?, ?> map) { | ||
return map.containsValue(value); | ||
} | ||
|
||
if (value == null) { | ||
return false; | ||
} | ||
|
||
String s = o.toString(); | ||
|
||
if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) { | ||
return s.contains(value.toString()); | ||
} | ||
|
||
return s.toLowerCase().contains(value.toString().toLowerCase()); | ||
}); | ||
} | ||
|
||
public Predicate<Object> startsWith(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (!(o instanceof String s)) { | ||
return false; | ||
} | ||
|
||
if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) { | ||
return s.startsWith(value.toString()); | ||
} | ||
|
||
return s.toLowerCase().startsWith(value.toString().toLowerCase()); | ||
}); | ||
} | ||
|
||
public Predicate<Object> endsWith(Object value) { | ||
return new DaprPredicate(part.getProperty(), value, o -> { | ||
if (!(o instanceof String s)) { | ||
return false; | ||
} | ||
|
||
if (ObjectUtils.nullSafeEquals(Part.IgnoreCaseType.NEVER, part.shouldIgnoreCase())) { | ||
return s.endsWith(value.toString()); | ||
} | ||
|
||
return s.toLowerCase().endsWith(value.toString().toLowerCase()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.