Add Predicates utility.

Original Pull Request: #2420
This commit is contained in:
Mark Paluch
2021-11-29 12:13:49 +01:00
committed by Christoph Strobl
parent b189240aaf
commit 0c80e9fa46
4 changed files with 64 additions and 3 deletions

View File

@@ -44,6 +44,7 @@ import org.springframework.core.convert.converter.GenericConverter.ConvertiblePa
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.Predicates;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -892,7 +893,7 @@ public class CustomConversions {
* @param userConverters must not be {@literal null} use {@link Collections#emptyList()} instead.
*/
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters) {
this(storeConversions, userConverters, it -> true);
this(storeConversions, userConverters, Predicates.isTrue());
}
/**

View File

@@ -38,6 +38,7 @@ import org.springframework.data.spel.spi.ExtensionIdAware;
import org.springframework.data.spel.spi.Function;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.Optionals;
import org.springframework.data.util.Predicates;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.MethodExecutor;
@@ -107,7 +108,7 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
*/
@Override
public StandardEvaluationContext getEvaluationContext(Object rootObject) {
return doGetEvaluationContext(rootObject, getExtensions(it -> true));
return doGetEvaluationContext(rootObject, getExtensions(Predicates.isTrue()));
}
/*

View File

@@ -27,6 +27,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.data.spel.spi.EvaluationContextExtension;
import org.springframework.data.spel.spi.ExtensionIdAware;
import org.springframework.data.spel.spi.ReactiveEvaluationContextExtension;
import org.springframework.data.util.Predicates;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -93,7 +94,7 @@ public class ReactiveExtensionAwareEvaluationContextProvider implements Reactive
*/
@Override
public Mono<StandardEvaluationContext> getEvaluationContextLater(Object rootObject) {
return getExtensions(it -> true) //
return getExtensions(Predicates.isTrue()) //
.map(it -> evaluationContextProvider.doGetEvaluationContext(rootObject, it));
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2021 the original author or 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
*
* https://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 org.springframework.data.util;
import java.util.function.Predicate;
import org.springframework.util.Assert;
/**
* Utility methods to work with {@link Predicate}s.
*
* @author Mark Paluch
* @since 2.7
*/
public interface Predicates {
/**
* A {@link Predicate} that yields always {@code true}.
*
* @return a {@link Predicate} that yields always {@code true}.
*/
static <T> Predicate<T> isTrue() {
return t -> true;
}
/**
* A {@link Predicate} that yields always {@code false}.
*
* @return a {@link Predicate} that yields always {@code false}.
*/
static <T> Predicate<T> isFalse() {
return t -> false;
}
/**
* Returns a {@link Predicate} that represents the logical negation of {@code predicate}.
*
* @return a {@link Predicate} that represents the logical negation of {@code predicate}.
*/
static <T> Predicate<T> negate(Predicate<T> predicate) {
Assert.notNull(predicate, "Predicate must not be null!");
return predicate.negate();
}
}