Skip to content

Commit 0c80e9f

Browse files
mp911dechristophstrobl
authored andcommitted
Add Predicates utility.
Original Pull Request: #2420
1 parent b189240 commit 0c80e9f

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

src/main/java/org/springframework/data/convert/CustomConversions.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.core.convert.support.GenericConversionService;
4545
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
4646
import org.springframework.data.mapping.model.SimpleTypeHolder;
47+
import org.springframework.data.util.Predicates;
4748
import org.springframework.data.util.Streamable;
4849
import org.springframework.lang.Nullable;
4950
import org.springframework.util.Assert;
@@ -892,7 +893,7 @@ protected static class ConverterConfiguration {
892893
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
893894
*/
894895
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters) {
895-
this(storeConversions, userConverters, it -> true);
896+
this(storeConversions, userConverters, Predicates.isTrue());
896897
}
897898

898899
/**

src/main/java/org/springframework/data/spel/ExtensionAwareEvaluationContextProvider.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.spel.spi.Function;
3939
import org.springframework.data.util.Lazy;
4040
import org.springframework.data.util.Optionals;
41+
import org.springframework.data.util.Predicates;
4142
import org.springframework.expression.AccessException;
4243
import org.springframework.expression.EvaluationContext;
4344
import org.springframework.expression.MethodExecutor;
@@ -107,7 +108,7 @@ public ExtensionAwareEvaluationContextProvider(
107108
*/
108109
@Override
109110
public StandardEvaluationContext getEvaluationContext(Object rootObject) {
110-
return doGetEvaluationContext(rootObject, getExtensions(it -> true));
111+
return doGetEvaluationContext(rootObject, getExtensions(Predicates.isTrue()));
111112
}
112113

113114
/*

src/main/java/org/springframework/data/spel/ReactiveExtensionAwareEvaluationContextProvider.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.spel.spi.EvaluationContextExtension;
2828
import org.springframework.data.spel.spi.ExtensionIdAware;
2929
import org.springframework.data.spel.spi.ReactiveEvaluationContextExtension;
30+
import org.springframework.data.util.Predicates;
3031
import org.springframework.data.util.ReflectionUtils;
3132
import org.springframework.expression.EvaluationContext;
3233
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -93,7 +94,7 @@ public EvaluationContext getEvaluationContext(Object rootObject, ExpressionDepen
9394
*/
9495
@Override
9596
public Mono<StandardEvaluationContext> getEvaluationContextLater(Object rootObject) {
96-
return getExtensions(it -> true) //
97+
return getExtensions(Predicates.isTrue()) //
9798
.map(it -> evaluationContextProvider.doGetEvaluationContext(rootObject, it));
9899
}
99100

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.util;
17+
18+
import java.util.function.Predicate;
19+
20+
import org.springframework.util.Assert;
21+
22+
/**
23+
* Utility methods to work with {@link Predicate}s.
24+
*
25+
* @author Mark Paluch
26+
* @since 2.7
27+
*/
28+
public interface Predicates {
29+
30+
/**
31+
* A {@link Predicate} that yields always {@code true}.
32+
*
33+
* @return a {@link Predicate} that yields always {@code true}.
34+
*/
35+
static <T> Predicate<T> isTrue() {
36+
return t -> true;
37+
}
38+
39+
/**
40+
* A {@link Predicate} that yields always {@code false}.
41+
*
42+
* @return a {@link Predicate} that yields always {@code false}.
43+
*/
44+
static <T> Predicate<T> isFalse() {
45+
return t -> false;
46+
}
47+
48+
/**
49+
* Returns a {@link Predicate} that represents the logical negation of {@code predicate}.
50+
*
51+
* @return a {@link Predicate} that represents the logical negation of {@code predicate}.
52+
*/
53+
static <T> Predicate<T> negate(Predicate<T> predicate) {
54+
55+
Assert.notNull(predicate, "Predicate must not be null!");
56+
return predicate.negate();
57+
}
58+
}

0 commit comments

Comments
 (0)