Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,17 @@ public int hashCode() {
}

/**
* Convert long to int for a resource value safely. This method assumes
* resource value is positive.
* Convert long to int for a resource value safely. Negative values are
* clamped to 0; values exceeding Integer.MAX_VALUE are clamped to
* Integer.MAX_VALUE.
*
* @param value long resource value
* @return int resource value
*/
protected static int castToIntSafely(long value) {
if (value < 0) {
return 0;
}
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ void testCastToIntSafely() {
Resource.castToIntSafely(Long.MAX_VALUE),
"Cast to Integer.MAX_VALUE if the long is greater than "
+ "Integer.MAX_VALUE");

assertEquals(0, Resource.castToIntSafely(-1),
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.

keeping with assertEquals as it's already in use...FYI for new test suites we like AssertJ as it's more informative and cherrypicks easily from junit5 branches (3.5+) and Junit4 (3.4.x)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the information!

"Cast to 0 if the long is negative");
assertEquals(0, Resource.castToIntSafely(Long.MIN_VALUE),
"Cast to 0 if the long is negative");
}

@Test
Expand Down
Loading