Skip to content

Commit

Permalink
Avoid boxing in Doubles.constrainToRange() and Floats.constrainToRang…
Browse files Browse the repository at this point in the history
…e().

Fixes #3984

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=325802979
  • Loading branch information
kluever authored and cgdecker committed Aug 12, 2020
1 parent 081c486 commit aa18c74
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
10 changes: 8 additions & 2 deletions android/guava/src/com/google/common/primitives/Doubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.base.Strings.lenientFormat;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;

Expand Down Expand Up @@ -253,8 +254,13 @@ public static double max(double... array) {
*/
@Beta
public static double constrainToRange(double value, double min, double max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return Math.min(Math.max(value, min), max);
// avoid auto-boxing by not using Preconditions.checkArgument(); see Guava issue 3984
// Reject NaN by testing for the good case (min <= max) instead of the bad (min > max).
if (min <= max) {
return Math.min(Math.max(value, min), max);
}
throw new IllegalArgumentException(
lenientFormat("min (%s) must be less than or equal to max (%s)", min, max));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions android/guava/src/com/google/common/primitives/Floats.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.base.Strings.lenientFormat;
import static java.lang.Float.NEGATIVE_INFINITY;
import static java.lang.Float.POSITIVE_INFINITY;

Expand Down Expand Up @@ -250,8 +251,13 @@ public static float max(float... array) {
*/
@Beta
public static float constrainToRange(float value, float min, float max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return Math.min(Math.max(value, min), max);
// avoid auto-boxing by not using Preconditions.checkArgument(); see Guava issue 3984
// Reject NaN by testing for the good case (min <= max) instead of the bad (min > max).
if (min <= max) {
return Math.min(Math.max(value, min), max);
}
throw new IllegalArgumentException(
lenientFormat("min (%s) must be less than or equal to max (%s)", min, max));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions guava/src/com/google/common/primitives/Doubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.base.Strings.lenientFormat;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;

Expand Down Expand Up @@ -255,8 +256,13 @@ public static double max(double... array) {
*/
@Beta
public static double constrainToRange(double value, double min, double max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return Math.min(Math.max(value, min), max);
// avoid auto-boxing by not using Preconditions.checkArgument(); see Guava issue 3984
// Reject NaN by testing for the good case (min <= max) instead of the bad (min > max).
if (min <= max) {
return Math.min(Math.max(value, min), max);
}
throw new IllegalArgumentException(
lenientFormat("min (%s) must be less than or equal to max (%s)", min, max));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions guava/src/com/google/common/primitives/Floats.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.base.Strings.lenientFormat;
import static java.lang.Float.NEGATIVE_INFINITY;
import static java.lang.Float.POSITIVE_INFINITY;

Expand Down Expand Up @@ -250,8 +251,13 @@ public static float max(float... array) {
*/
@Beta
public static float constrainToRange(float value, float min, float max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return Math.min(Math.max(value, min), max);
// avoid auto-boxing by not using Preconditions.checkArgument(); see Guava issue 3984
// Reject NaN by testing for the good case (min <= max) instead of the bad (min > max).
if (min <= max) {
return Math.min(Math.max(value, min), max);
}
throw new IllegalArgumentException(
lenientFormat("min (%s) must be less than or equal to max (%s)", min, max));
}

/**
Expand Down

0 comments on commit aa18c74

Please sign in to comment.