Refactor: Merge the duplicate logic of PriorityComparator.of(...).#6220
Refactor: Merge the duplicate logic of PriorityComparator.of(...).#6220KSSJW wants to merge 1 commit into
Conversation
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the PriorityComparator.of method to accept a List<T> instead of varargs, delegating the logic to an overloaded of method. The feedback suggests adding an explicit null check for the values parameter to ensure early failure and improve code robustness.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| public static <T extends Comparable<T>> PriorityComparator<T> of(List<T> values) { | ||
| return of(values, Comparator.naturalOrder(), true); | ||
| } |
There was a problem hiding this comment.
为了提高代码的健壮性,建议在方法入口处对 values 进行非空校验。虽然在后续调用的重载方法中遍历 values 时也会抛出 NullPointerException,但显式地进行参数校验可以更早地发现问题,并提供更清晰的错误信息,符合防御性编程的最佳实践。
| public static <T extends Comparable<T>> PriorityComparator<T> of(List<T> values) { | |
| return of(values, Comparator.naturalOrder(), true); | |
| } | |
| public static <T extends Comparable<T>> PriorityComparator<T> of(List<T> values) { | |
| Objects.requireNonNull(values, "values cannot be null"); | |
| return of(values, Comparator.naturalOrder(), true); | |
| } |
合并
PriorityComparator.of(...)的重复逻辑变更
PriorityComparator.of(T... values))与PriorityComparator.of(List<T> values, Comparator<? super T> fallback, boolean prioritizedFirst))逻辑有重复之处,且T... values存在安全隐患,故合并逻辑。