Skip to content

Extract clamp methods to MathUtils#5826

Merged
Glavo merged 3 commits intoHMCL-dev:mainfrom
Glavo:math
Mar 22, 2026
Merged

Extract clamp methods to MathUtils#5826
Glavo merged 3 commits intoHMCL-dev:mainfrom
Glavo:math

Conversation

@Glavo
Copy link
Member

@Glavo Glavo commented Mar 22, 2026

  1. 调整 clamp 方法的参数顺序,与 Java 21 中 Math 类新添加的 clamp 方法保持一致;
  2. clamp 方法提取到 MathUtils 类中,避免未来我们反向移植补丁时因为参数顺序造成隐蔽的问题。

@Glavo Glavo requested a review from Copilot March 22, 2026 13:02
Copy link
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

This PR standardizes clamping utilities by moving them out of Lang into a dedicated MathUtils class and switching the parameter order to match Java 21’s Math.clamp, reducing the risk of subtle bugs during future backports.

Changes:

  • Added MathUtils with clamp(value, min, max) overloads for int and double.
  • Removed the old Lang.clamp(...) overloads.
  • Updated UI call sites to use MathUtils.clamp(...) with the new argument order.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
HMCLCore/src/main/java/org/jackhuang/hmcl/util/MathUtils.java Introduces the new centralized clamp helpers with Java 21-style parameter order.
HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java Removes the legacy clamp methods to avoid future accidental usage with the old argument order.
HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java Migrates background opacity clamping to MathUtils.clamp(value, min, max).
HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/FloatScrollBarSkin.java Migrates scrollbar value clamping to MathUtils.clamp(value, min, max).

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

Comment on lines +27 to +33
assert min <= max;
return Math.min(max, Math.max(value, min));
}

/// Clamp value to fit between min and max.
public static int clamp(int value, int min, int max) {
assert min <= max;
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

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

assert min <= max will be skipped when assertions are disabled (typical in production), so invalid ranges can silently produce incorrect results. If the intent is to align with Java 21 Math.clamp, consider doing an explicit runtime check and throwing IllegalArgumentException when min > max (also covers NaN for the double overload if you use !(min <= max)).

Suggested change
assert min <= max;
return Math.min(max, Math.max(value, min));
}
/// Clamp value to fit between min and max.
public static int clamp(int value, int min, int max) {
assert min <= max;
if (!(min <= max)) {
throw new IllegalArgumentException("min must be less than or equal to max");
}
return Math.min(max, Math.max(value, min));
}
/// Clamp value to fit between min and max.
public static int clamp(int value, int min, int max) {
if (min > max) {
throw new IllegalArgumentException("min must be less than or equal to max");
}

Copilot uses AI. Check for mistakes.
@Glavo Glavo merged commit c58d5a3 into HMCL-dev:main Mar 22, 2026
2 checks passed
@Glavo Glavo deleted the math branch March 22, 2026 13:27
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.

2 participants