8267670: Update java.io, java.math, and java.text to use switch expressions

Reviewed-by: darcy, chegar, naoto, iris, dfuchs, lancea, vtewari
This commit is contained in:
Patrick Concannon 2021-06-01 10:14:56 +00:00
parent f5634fe39d
commit 4eb216824f
11 changed files with 161 additions and 349 deletions

View file

@ -4706,19 +4706,13 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
else { // half-way
assert cmpFracHalf == 0;
switch(roundingMode) {
case ROUND_HALF_DOWN:
return false;
return switch (roundingMode) {
case ROUND_HALF_DOWN -> false;
case ROUND_HALF_UP -> true;
case ROUND_HALF_EVEN -> oddQuot;
case ROUND_HALF_UP:
return true;
case ROUND_HALF_EVEN:
return oddQuot;
default:
throw new AssertionError("Unexpected rounding mode" + roundingMode);
}
default -> throw new AssertionError("Unexpected rounding mode" + roundingMode);
};
}
}
}

View file

@ -3791,14 +3791,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
*/
public int compareTo(BigInteger val) {
if (signum == val.signum) {
switch (signum) {
case 1:
return compareMagnitude(val);
case -1:
return val.compareMagnitude(this);
default:
return 0;
}
return switch (signum) {
case 1 -> compareMagnitude(val);
case -1 -> val.compareMagnitude(this);
default -> 0;
};
}
return signum > val.signum ? 1 : -1;
}

View file

@ -377,34 +377,16 @@ public enum RoundingMode {
* @throws IllegalArgumentException integer is out of range
*/
public static RoundingMode valueOf(int rm) {
switch(rm) {
case BigDecimal.ROUND_UP:
return UP;
case BigDecimal.ROUND_DOWN:
return DOWN;
case BigDecimal.ROUND_CEILING:
return CEILING;
case BigDecimal.ROUND_FLOOR:
return FLOOR;
case BigDecimal.ROUND_HALF_UP:
return HALF_UP;
case BigDecimal.ROUND_HALF_DOWN:
return HALF_DOWN;
case BigDecimal.ROUND_HALF_EVEN:
return HALF_EVEN;
case BigDecimal.ROUND_UNNECESSARY:
return UNNECESSARY;
default:
throw new IllegalArgumentException("argument out of range");
}
return switch (rm) {
case BigDecimal.ROUND_UP -> UP;
case BigDecimal.ROUND_DOWN -> DOWN;
case BigDecimal.ROUND_CEILING -> CEILING;
case BigDecimal.ROUND_FLOOR -> FLOOR;
case BigDecimal.ROUND_HALF_UP -> HALF_UP;
case BigDecimal.ROUND_HALF_DOWN -> HALF_DOWN;
case BigDecimal.ROUND_HALF_EVEN -> HALF_EVEN;
case BigDecimal.ROUND_UNNECESSARY -> UNNECESSARY;
default -> throw new IllegalArgumentException("argument out of range");
};
}
}