Skip to content

Comments

[BUG] Fix allMin/allMax test compilation errors with Comparator.naturalOrder()#108

Open
akgitrepos wants to merge 1 commit intogoogle:masterfrom
akgitrepos:fix-allmin-allmax-compilation
Open

[BUG] Fix allMin/allMax test compilation errors with Comparator.naturalOrder()#108
akgitrepos wants to merge 1 commit intogoogle:masterfrom
akgitrepos:fix-allmin-allmax-compilation

Conversation

@akgitrepos
Copy link

@akgitrepos akgitrepos commented Feb 22, 2026

Summary

Fixes 6 test compilation errors in MoreCollectorsTest.java that prevent tests from compiling when using Comparator.naturalOrder() with the allMin/allMax collector methods.

Problem

Tests like testAllMax_empty, testAllMax_toOnlyElement, etc. fail with:

Unresolved compilation problem: 
The method allMax(Comparator<? super T>, Collector<? super T,?,R>) is not applicable 
for the arguments (Comparator<Comparable<? super Comparable<? super T>>>, 
Collector<Object,capture#65-of ?,ImmutableList<Object>>)

Java's type inference fails when combining:

  1. Comparator.naturalOrder() - returns Comparator<Comparable<? super T>>
  2. toImmutableList() - returns Collector<Object, ?, ImmutableList<Object>>

The wildcards ? super T in the method signature prevent Java from correctly inferring T.

Solution

1. Method signature change (MoreCollectors.java)

Changed downstream collector type from Collector<? super T, ?, R> to Collector<T, ?, R>:

  • This narrows the type parameter, helping Java's type inference work correctly
  • The change is backward compatible - existing code will continue to work

2. Test fixes (MoreCollectorsTest.java)

Added explicit type witnesses to ensure robust type inference:

  • Comparator.<Integer>naturalOrder()
  • onlyElement(identity())

Files Changed

  • mug/src/main/java/com/google/mu/util/stream/MoreCollectors.java (2 lines)
  • mug/src/test/java/com/google/mu/util/stream/MoreCollectorsTest.java (2 lines)

Testing

  • All 6 previously failing tests now pass
  • All mug module tests pass
  • All dependent modules pass

- Changed downstream collector type from Collector<? super T, ?, R> to
  Collector<T, ?, R> to improve type inference with Comparator.naturalOrder()
- Added explicit type witnesses in tests: Comparator.<Integer>naturalOrder()
  and onlyElement(identity()) to help Java's type inference

This fixes 6 test compilation errors:
- testAllMax_empty
- testAllMax_toOnlyElement
- testAllMax_multiple
- testAllMin_empty
- testAllMin_toOnlyElement
- testAllMin_multiple
@akgitrepos
Copy link
Author

@fluentfuture Hi, I have fixed test compilation issues on MoreCollectorsTest.java. Would appreciate a review. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant