Skip to content

Refactor: Merge the duplicate logic of PriorityComparator.of(...).#6220

Open
KSSJW wants to merge 1 commit into
HMCL-dev:mainfrom
KSSJW-Contribution:priority-comparator-refactor
Open

Refactor: Merge the duplicate logic of PriorityComparator.of(...).#6220
KSSJW wants to merge 1 commit into
HMCL-dev:mainfrom
KSSJW-Contribution:priority-comparator-refactor

Conversation

@KSSJW

@KSSJW KSSJW commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

合并PriorityComparator.of(...)的重复逻辑

变更

  • PriorityComparator.of(T... values))PriorityComparator.of(List<T> values, Comparator<? super T> fallback, boolean prioritizedFirst))逻辑有重复之处,且T... values存在安全隐患,故合并逻辑。

@KSSJW

KSSJW commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +36 to 38
public static <T extends Comparable<T>> PriorityComparator<T> of(List<T> values) {
return of(values, Comparator.naturalOrder(), true);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

为了提高代码的健壮性,建议在方法入口处对 values 进行非空校验。虽然在后续调用的重载方法中遍历 values 时也会抛出 NullPointerException,但显式地进行参数校验可以更早地发现问题,并提供更清晰的错误信息,符合防御性编程的最佳实践。

Suggested change
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);
}

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