Clean up the code to make it cleaner

Signed-off-by: longtanfei <lucky8987@163.com>
This commit is contained in:
lucky8987
2025-03-26 23:07:02 +08:00
committed by longtanfei
parent 51fb9aa36c
commit d0c070c37d
5 changed files with 8 additions and 24 deletions

View File

@@ -212,11 +212,6 @@
<artifactId>spring-cloud-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>

View File

@@ -77,8 +77,7 @@ public interface AsyncPredicate<T> extends Function<T, Publisher<Boolean>>, HasC
@Override
public void accept(Visitor visitor) {
if (delegate instanceof GatewayPredicate) {
GatewayPredicate gatewayPredicate = (GatewayPredicate) delegate;
if (delegate instanceof GatewayPredicate gatewayPredicate) {
gatewayPredicate.accept(visitor);
}
}

View File

@@ -58,12 +58,7 @@ public class CookieRoutePredicateFactory extends AbstractRoutePredicateFactory<C
if (cookies == null) {
return false;
}
for (HttpCookie cookie : cookies) {
if (cookie.getValue().matches(config.regexp)) {
return true;
}
}
return false;
return cookies.stream().anyMatch(cookie -> cookie.getValue().matches(config.regexp));
}
@Override

View File

@@ -47,8 +47,8 @@ public interface GatewayPredicate extends Predicate<ServerWebExchange>, HasConfi
static GatewayPredicate wrapIfNeeded(Predicate<? super ServerWebExchange> other) {
GatewayPredicate right;
if (other instanceof GatewayPredicate) {
right = (GatewayPredicate) other;
if (other instanceof GatewayPredicate gatewayPredicate) {
right = gatewayPredicate;
}
else {
right = new GatewayPredicateWrapper(other);
@@ -72,8 +72,7 @@ public interface GatewayPredicate extends Predicate<ServerWebExchange>, HasConfi
@Override
public void accept(Visitor visitor) {
if (delegate instanceof GatewayPredicate) {
GatewayPredicate gatewayPredicate = (GatewayPredicate) delegate;
if (delegate instanceof GatewayPredicate gatewayPredicate) {
gatewayPredicate.accept(visitor);
}
}

View File

@@ -201,14 +201,10 @@ public class RouteDefinitionRouteLocator implements RouteLocator {
// this is a very rare case, but possible, just match all
return AsyncPredicate.from(exchange -> true);
}
AsyncPredicate<ServerWebExchange> predicate = lookup(routeDefinition, predicates.get(0));
for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
AsyncPredicate<ServerWebExchange> found = lookup(routeDefinition, andPredicate);
predicate = predicate.and(found);
}
return predicate;
return predicates.stream()
.map(nextPredicate -> lookup(routeDefinition, nextPredicate))
.reduce(AsyncPredicate.from(exchange -> true), AsyncPredicate::and);
}
@SuppressWarnings("unchecked")