You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Guillaume Nodet <gn...@apache.org> on 2025/06/03 21:06:28 UTC
Re: [PR] [MNG-8764] Sort injected lists by @Priority annotation [maven]
More verbose code. What’s the benefit?
------------------------
Guillaume Nodet
Le mar. 3 juin 2025 à 23:05, Pankraz76 (via GitHub) <gi...@apache.org> a
écrit :
>
> Pankraz76 commented on code in PR #2425:
> URL: https://github.com/apache/maven/pull/2425#discussion_r2124918459
>
>
> ##########
> impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java:
> ##########
> @@ -220,7 +220,13 @@ public <Q> Supplier<Q>
> doGetCompiledBinding(Dependency<Q> dep) {
> if (key.getRawType() == List.class) {
> Set<Binding<Object>> res2 =
> getBindings(key.getTypeParameter(0));
> if (res2 != null) {
> - List<Supplier<Object>> list =
> res2.stream().map(this::compile).collect(Collectors.toList());
> + // Sort bindings by priority (highest first) for
> deterministic ordering
> + List<Binding<Object>> sortedBindings = new
> ArrayList<>(res2);
> + Comparator<Binding<Object>> comparing =
> Comparator.comparing(Binding::getPriority);
> + sortedBindings.sort(comparing.reversed());
> +
> + List<Supplier<Object>> list =
> +
> sortedBindings.stream().map(this::compile).collect(Collectors.toList());
> //noinspection unchecked
> return () -> (Q) list(list, Supplier::get);
>
> Review Comment:
> considering this domain logic, might use OOP implementing `comparable`
> as its wont hurt nobody giving special attention and cohesion to core logic.
>
> <img width="1862" alt="image" src="
> https://github.com/user-attachments/assets/416d7025-ea50-4bca-9d00-afa3b1fc00a2"
> />
>
>
>
>
>
>
> ##########
> impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java:
> ##########
> @@ -220,7 +220,13 @@ public <Q> Supplier<Q>
> doGetCompiledBinding(Dependency<Q> dep) {
> if (key.getRawType() == List.class) {
> Set<Binding<Object>> res2 =
> getBindings(key.getTypeParameter(0));
> if (res2 != null) {
> - List<Supplier<Object>> list =
> res2.stream().map(this::compile).collect(Collectors.toList());
> + // Sort bindings by priority (highest first) for
> deterministic ordering
> + List<Binding<Object>> sortedBindings = new
> ArrayList<>(res2);
> + Comparator<Binding<Object>> comparing =
> Comparator.comparing(Binding::getPriority);
> + sortedBindings.sort(comparing.reversed());
> +
> + List<Supplier<Object>> list =
> +
> sortedBindings.stream().map(this::compile).collect(Collectors.toList());
>
> Review Comment:
> ```java
> List<Binding<Object>> sortedBindings = new ArrayList<>(res2);
> sortedBindings.sort(Comparator.reverseOrder());
> //noinspection unchecked
> return () -> (Q)
> list(sortedBindings.stream().map(this::compile).collect(Collectors.toList()),
> Supplier::get);
> ```
> ```java
> @Override
> public int compareTo(Binding<Object> objectBinding) {
> return Integer.compare(this.priority, objectBinding.priority);
> }
> ```
>
>
>
> --
> This is an automated message from the Apache Git Service.
> To respond to the message, please log on to GitHub and use the
> URL above to go to the specific comment.
>
> To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
>
> For queries about this service, please contact Infrastructure at:
> users@infra.apache.org
>
>