Skip to content

refactor: use functional streams for host and path predicate accumulation#4128

Merged
ryanjbaxter merged 2 commits intospring-cloud:mainfrom
Yuri-Costa09:refactor/functional-host-predicate
Mar 30, 2026
Merged

refactor: use functional streams for host and path predicate accumulation#4128
ryanjbaxter merged 2 commits intospring-cloud:mainfrom
Yuri-Costa09:refactor/functional-host-predicate

Conversation

@Yuri-Costa09
Copy link
Copy Markdown
Contributor

@Yuri-Costa09 Yuri-Costa09 commented Mar 30, 2026

This pull request refactors the way multiple host and path patterns are handled in GatewayRequestPredicates. The main improvement is replacing manual loops with more concise and functional Java Stream API usage, making the code cleaner and easier to read.

Changes

  • Updated the host(String... patterns) method to use Java Streams for combining multiple host predicates with or, replacing the previous manual loop.
  • Updated the path(String... patterns) method to use Java Streams for combining multiple path predicates with or, replacing the previous manual loop.

Documentation improvements:

  • Added Javadoc comments for the overloaded host(String pattern) and host(String... patterns) methods to clarify their purpose and usage. Following the convention of other existing methods.

Motivation

This PR refactors the host predicate accumulation to use a functional style with Java Streams, resolving a legacy "TODO" and aligning the implementation with other predicates like path.

Key benefits:

  • Readability & Modernization: Replaces imperative for loops with declarative Streams, reducing cognitive load and removing manual index management ("magic numbers").

  • Immutability & Safety: Leverages the Streams API to ensure immutable operations during the predicate tree construction, which is a best practice in reactive environments like Spring Cloud Gateway.

  • Future-Proofing: While the current number of patterns is usually small, this functional approach allows for easier evolution (such as parallelization) without changing the core logic.

  • Documentation: Added Javadocs to the host methods to improve the developer experience for both users and future contributors, maintaining consistency across the RequestPredicates class.

All tests passed, Build Successful.

Signed-off-by: Yuri-Costa09 <yurircostawork@gmail.com>
… format code following codebase rules.

Signed-off-by: Yuri-Costa09 <yurircostawork@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors GatewayRequestPredicates to build composite host/path predicates using Java Streams, and adds Javadoc to clarify usage of the host overloads.

Changes:

  • Refactored host(String... patterns) to combine multiple host predicates via Stream.reduce(RequestPredicate::or).
  • Refactored path(String... patterns) to combine multiple path predicates via Stream.reduce(RequestPredicate::or).
  • Added Javadoc for host(String pattern) and host(String... patterns) to align with the documentation style of other predicates in the class.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ryanjbaxter ryanjbaxter merged commit 2380487 into spring-cloud:main Mar 30, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this to Done in 2025.1.2 Mar 30, 2026
@ryanjbaxter ryanjbaxter added this to the 5.0.2 milestone Mar 30, 2026
@Yuri-Costa09
Copy link
Copy Markdown
Contributor Author

Thank you for your time! @ryanjbaxter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants