Skip to content

Commit d0c070c

Browse files
committed
Clean up the code to make it cleaner
Signed-off-by: longtanfei <[email protected]>
1 parent 51fb9aa commit d0c070c

File tree

5 files changed

+8
-24
lines changed

5 files changed

+8
-24
lines changed

Diff for: spring-cloud-gateway-server/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@
212212
<artifactId>spring-cloud-test-support</artifactId>
213213
<scope>test</scope>
214214
</dependency>
215-
<dependency>
216-
<groupId>org.springframework.cloud</groupId>
217-
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
218-
<scope>test</scope>
219-
</dependency>
220215
<dependency>
221216
<groupId>io.projectreactor</groupId>
222217
<artifactId>reactor-test</artifactId>

Diff for: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/AsyncPredicate.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public String toString() {
7777

7878
@Override
7979
public void accept(Visitor visitor) {
80-
if (delegate instanceof GatewayPredicate) {
81-
GatewayPredicate gatewayPredicate = (GatewayPredicate) delegate;
80+
if (delegate instanceof GatewayPredicate gatewayPredicate) {
8281
gatewayPredicate.accept(visitor);
8382
}
8483
}

Diff for: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRoutePredicateFactory.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ public boolean test(ServerWebExchange exchange) {
5858
if (cookies == null) {
5959
return false;
6060
}
61-
for (HttpCookie cookie : cookies) {
62-
if (cookie.getValue().matches(config.regexp)) {
63-
return true;
64-
}
65-
}
66-
return false;
61+
return cookies.stream().anyMatch(cookie -> cookie.getValue().matches(config.regexp));
6762
}
6863

6964
@Override

Diff for: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/GatewayPredicate.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ default void accept(Visitor visitor) {
4747
static GatewayPredicate wrapIfNeeded(Predicate<? super ServerWebExchange> other) {
4848
GatewayPredicate right;
4949

50-
if (other instanceof GatewayPredicate) {
51-
right = (GatewayPredicate) other;
50+
if (other instanceof GatewayPredicate gatewayPredicate) {
51+
right = gatewayPredicate;
5252
}
5353
else {
5454
right = new GatewayPredicateWrapper(other);
@@ -72,8 +72,7 @@ public boolean test(ServerWebExchange exchange) {
7272

7373
@Override
7474
public void accept(Visitor visitor) {
75-
if (delegate instanceof GatewayPredicate) {
76-
GatewayPredicate gatewayPredicate = (GatewayPredicate) delegate;
75+
if (delegate instanceof GatewayPredicate gatewayPredicate) {
7776
gatewayPredicate.accept(visitor);
7877
}
7978
}

Diff for: spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/RouteDefinitionRouteLocator.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,10 @@ private AsyncPredicate<ServerWebExchange> combinePredicates(RouteDefinition rout
201201
// this is a very rare case, but possible, just match all
202202
return AsyncPredicate.from(exchange -> true);
203203
}
204-
AsyncPredicate<ServerWebExchange> predicate = lookup(routeDefinition, predicates.get(0));
205204

206-
for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
207-
AsyncPredicate<ServerWebExchange> found = lookup(routeDefinition, andPredicate);
208-
predicate = predicate.and(found);
209-
}
210-
211-
return predicate;
205+
return predicates.stream()
206+
.map(nextPredicate -> lookup(routeDefinition, nextPredicate))
207+
.reduce(AsyncPredicate.from(exchange -> true), AsyncPredicate::and);
212208
}
213209

214210
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)