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

@ -623,12 +623,12 @@ loop: while (true) {
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
case 0, 1, 2, 3, 4, 5, 6, 7 -> {
/* 0xxxxxxx*/
count++;
chararr[chararr_count++]=(char)c;
break;
case 12: case 13:
}
case 12, 13 -> {
/* 110x xxxx 10xx xxxx*/
count += 2;
if (count > utflen)
@ -640,8 +640,8 @@ loop: while (true) {
"malformed input around byte " + count);
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
}
case 14 -> {
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
@ -655,8 +655,8 @@ loop: while (true) {
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
}
default ->
/* 10xx xxxx, 1111 xxxx */
throw new UTFDataFormatException(
"malformed input around byte " + count);

@ -1778,21 +1778,13 @@ public class ObjectInputStream
int oldHandle = passHandle;
try {
byte tc = bin.peekByte();
switch (tc) {
case TC_NULL:
return (String) readNull();
case TC_REFERENCE:
return (String) readHandle(false);
case TC_STRING:
case TC_LONGSTRING:
return readString(false);
default:
throw new StreamCorruptedException(
return switch (tc) {
case TC_NULL -> (String) readNull();
case TC_REFERENCE -> (String) readHandle(false);
case TC_STRING, TC_LONGSTRING -> readString(false);
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
};
} finally {
passHandle = oldHandle;
}
@ -1872,27 +1864,20 @@ public class ObjectInputStream
throws IOException
{
byte tc = bin.peekByte();
ObjectStreamClass descriptor;
switch (tc) {
case TC_NULL:
descriptor = (ObjectStreamClass) readNull();
break;
case TC_REFERENCE:
descriptor = (ObjectStreamClass) readHandle(unshared);
return switch (tc) {
case TC_NULL -> (ObjectStreamClass) readNull();
case TC_PROXYCLASSDESC -> readProxyDesc(unshared);
case TC_CLASSDESC -> readNonProxyDesc(unshared);
case TC_REFERENCE -> {
var d = (ObjectStreamClass) readHandle(unshared);
// Should only reference initialized class descriptors
descriptor.checkInitialized();
break;
case TC_PROXYCLASSDESC:
descriptor = readProxyDesc(unshared);
break;
case TC_CLASSDESC:
descriptor = readNonProxyDesc(unshared);
break;
default:
throw new StreamCorruptedException(
d.checkInitialized();
yield d;
}
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
return descriptor;
};
}
private boolean isCustomSubclass() {
@ -2052,21 +2037,13 @@ public class ObjectInputStream
* assigned handle.
*/
private String readString(boolean unshared) throws IOException {
String str;
byte tc = bin.readByte();
switch (tc) {
case TC_STRING:
str = bin.readUTF();
break;
case TC_LONGSTRING:
str = bin.readLongUTF();
break;
default:
throw new StreamCorruptedException(
String str = switch (tc) {
case TC_STRING -> bin.readUTF();
case TC_LONGSTRING -> bin.readLongUTF();
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
};
passHandle = handles.assign(unshared ? unsharedMarker : str);
handles.finish(passHandle);
return str;
@ -3680,28 +3657,17 @@ public class ObjectInputStream
int b1, b2, b3;
b1 = buf[pos++] & 0xFF;
switch (b1 >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: // 1 byte format: 0xxxxxxx
case 0, 1, 2, 3, 4, 5, 6, 7 -> // 1 byte format: 0xxxxxxx
cbuf[cpos++] = (char) b1;
break;
case 12:
case 13: // 2 byte format: 110xxxxx 10xxxxxx
case 12, 13 -> { // 2 byte format: 110xxxxx 10xxxxxx
b2 = buf[pos++];
if ((b2 & 0xC0) != 0x80) {
throw new UTFDataFormatException();
}
cbuf[cpos++] = (char) (((b1 & 0x1F) << 6) |
((b2 & 0x3F) << 0));
break;
case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
}
case 14 -> { // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
b3 = buf[pos + 1];
b2 = buf[pos + 0];
pos += 2;
@ -3711,10 +3677,8 @@ public class ObjectInputStream
cbuf[cpos++] = (char) (((b1 & 0x0F) << 12) |
((b2 & 0x3F) << 6) |
((b3 & 0x3F) << 0));
break;
default: // 10xx xxxx, 1111 xxxx
throw new UTFDataFormatException();
}
default -> throw new UTFDataFormatException(); // 10xx xxxx, 1111 xxxx
}
}
} catch (ArrayIndexOutOfBoundsException ex) {
@ -3748,19 +3712,11 @@ public class ObjectInputStream
int b1, b2, b3;
b1 = readByte() & 0xFF;
switch (b1 >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: // 1 byte format: 0xxxxxxx
case 0, 1, 2, 3, 4, 5, 6, 7 -> { // 1 byte format: 0xxxxxxx
sbuf.append((char) b1);
return 1;
case 12:
case 13: // 2 byte format: 110xxxxx 10xxxxxx
}
case 12, 13 -> { // 2 byte format: 110xxxxx 10xxxxxx
if (utflen < 2) {
throw new UTFDataFormatException();
}
@ -3771,8 +3727,8 @@ public class ObjectInputStream
sbuf.append((char) (((b1 & 0x1F) << 6) |
((b2 & 0x3F) << 0)));
return 2;
case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
}
case 14 -> { // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
if (utflen < 3) {
if (utflen == 2) {
readByte(); // consume remaining byte
@ -3785,12 +3741,11 @@ public class ObjectInputStream
throw new UTFDataFormatException();
}
sbuf.append((char) (((b1 & 0x0F) << 12) |
((b2 & 0x3F) << 6) |
((b2 & 0x3F) << 6) |
((b3 & 0x3F) << 0)));
return 3;
default: // 10xx xxxx, 1111 xxxx
throw new UTFDataFormatException();
}
default -> throw new UTFDataFormatException(); // 10xx xxxx, 1111 xxxx
}
}

@ -1493,39 +1493,26 @@ public class ObjectStreamClass implements Serializable {
for (int i = 0; i < fields.length; i++) {
ObjectStreamField f = fields[i];
switch (f.getTypeCode()) {
case 'Z':
case 'B':
f.setOffset(primDataSize++);
break;
case 'C':
case 'S':
case 'Z', 'B' -> f.setOffset(primDataSize++);
case 'C', 'S' -> {
f.setOffset(primDataSize);
primDataSize += 2;
break;
case 'I':
case 'F':
}
case 'I', 'F' -> {
f.setOffset(primDataSize);
primDataSize += 4;
break;
case 'J':
case 'D':
}
case 'J', 'D' -> {
f.setOffset(primDataSize);
primDataSize += 8;
break;
case '[':
case 'L':
}
case '[', 'L' -> {
f.setOffset(numObjFields++);
if (firstObjIndex == -1) {
firstObjIndex = i;
}
break;
default:
throw new InternalError();
}
default -> throw new InternalError();
}
}
if (firstObjIndex != -1 &&
@ -2124,40 +2111,15 @@ public class ObjectStreamClass implements Serializable {
long key = readKeys[i];
int off = offsets[i];
switch (typeCodes[i]) {
case 'Z':
Bits.putBoolean(buf, off, unsafe.getBoolean(obj, key));
break;
case 'B':
buf[off] = unsafe.getByte(obj, key);
break;
case 'C':
Bits.putChar(buf, off, unsafe.getChar(obj, key));
break;
case 'S':
Bits.putShort(buf, off, unsafe.getShort(obj, key));
break;
case 'I':
Bits.putInt(buf, off, unsafe.getInt(obj, key));
break;
case 'F':
Bits.putFloat(buf, off, unsafe.getFloat(obj, key));
break;
case 'J':
Bits.putLong(buf, off, unsafe.getLong(obj, key));
break;
case 'D':
Bits.putDouble(buf, off, unsafe.getDouble(obj, key));
break;
default:
throw new InternalError();
case 'Z' -> Bits.putBoolean(buf, off, unsafe.getBoolean(obj, key));
case 'B' -> buf[off] = unsafe.getByte(obj, key);
case 'C' -> Bits.putChar(buf, off, unsafe.getChar(obj, key));
case 'S' -> Bits.putShort(buf, off, unsafe.getShort(obj, key));
case 'I' -> Bits.putInt(buf, off, unsafe.getInt(obj, key));
case 'F' -> Bits.putFloat(buf, off, unsafe.getFloat(obj, key));
case 'J' -> Bits.putLong(buf, off, unsafe.getLong(obj, key));
case 'D' -> Bits.putDouble(buf, off, unsafe.getDouble(obj, key));
default -> throw new InternalError();
}
}
}
@ -2178,40 +2140,15 @@ public class ObjectStreamClass implements Serializable {
}
int off = offsets[i];
switch (typeCodes[i]) {
case 'Z':
unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
break;
case 'B':
unsafe.putByte(obj, key, buf[off]);
break;
case 'C':
unsafe.putChar(obj, key, Bits.getChar(buf, off));
break;
case 'S':
unsafe.putShort(obj, key, Bits.getShort(buf, off));
break;
case 'I':
unsafe.putInt(obj, key, Bits.getInt(buf, off));
break;
case 'F':
unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
break;
case 'J':
unsafe.putLong(obj, key, Bits.getLong(buf, off));
break;
case 'D':
unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
break;
default:
throw new InternalError();
case 'Z' -> unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
case 'B' -> unsafe.putByte(obj, key, buf[off]);
case 'C' -> unsafe.putChar(obj, key, Bits.getChar(buf, off));
case 'S' -> unsafe.putShort(obj, key, Bits.getShort(buf, off));
case 'I' -> unsafe.putInt(obj, key, Bits.getInt(buf, off));
case 'F' -> unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
case 'J' -> unsafe.putLong(obj, key, Bits.getLong(buf, off));
case 'D' -> unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
default -> throw new InternalError();
}
}
}
@ -2230,15 +2167,10 @@ public class ObjectStreamClass implements Serializable {
* in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
*/
for (int i = numPrimFields; i < fields.length; i++) {
switch (typeCodes[i]) {
case 'L':
case '[':
vals[offsets[i]] = unsafe.getReference(obj, readKeys[i]);
break;
default:
throw new InternalError();
}
vals[offsets[i]] = switch (typeCodes[i]) {
case 'L', '[' -> unsafe.getReference(obj, readKeys[i]);
default -> throw new InternalError();
};
}
}
@ -2272,8 +2204,7 @@ public class ObjectStreamClass implements Serializable {
continue; // discard value
}
switch (typeCodes[i]) {
case 'L':
case '[':
case 'L', '[' -> {
Object val = vals[offsets[i]];
if (val != null &&
!types[i - numPrimFields].isInstance(val))
@ -2289,10 +2220,8 @@ public class ObjectStreamClass implements Serializable {
}
if (!dryRun)
unsafe.putReference(obj, key, val);
break;
default:
throw new InternalError();
}
default -> throw new InternalError();
}
}
}

@ -110,19 +110,18 @@ public class ObjectStreamField
this.unshared = unshared;
this.field = null;
switch (signature.charAt(0)) {
case 'Z': type = Boolean.TYPE; break;
case 'B': type = Byte.TYPE; break;
case 'C': type = Character.TYPE; break;
case 'S': type = Short.TYPE; break;
case 'I': type = Integer.TYPE; break;
case 'J': type = Long.TYPE; break;
case 'F': type = Float.TYPE; break;
case 'D': type = Double.TYPE; break;
case 'L':
case '[': type = Object.class; break;
default: throw new IllegalArgumentException("illegal signature");
}
type = switch (signature.charAt(0)) {
case 'Z' -> Boolean.TYPE;
case 'B' -> Byte.TYPE;
case 'C' -> Character.TYPE;
case 'S' -> Short.TYPE;
case 'I' -> Integer.TYPE;
case 'J' -> Long.TYPE;
case 'F' -> Float.TYPE;
case 'D' -> Double.TYPE;
case 'L', '[' -> Object.class;
default -> throw new IllegalArgumentException("illegal signature");
};
}
/**

@ -658,29 +658,16 @@ public class StreamTokenizer {
} else
d = c2;
} else {
switch (c) {
case 'a':
c = 0x7;
break;
case 'b':
c = '\b';
break;
case 'f':
c = 0xC;
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = 0xB;
break;
}
c = switch (c) {
case 'a' -> 0x7;
case 'b' -> '\b';
case 'f' -> 0xC;
case 'n' -> '\n';
case 'r' -> '\r';
case 't' -> '\t';
case 'v' -> 0xB;
default -> c;
};
d = read();
}
} else {
@ -791,43 +778,28 @@ public class StreamTokenizer {
* @see java.io.StreamTokenizer#ttype
*/
public String toString() {
String ret;
switch (ttype) {
case TT_EOF:
ret = "EOF";
break;
case TT_EOL:
ret = "EOL";
break;
case TT_WORD:
ret = sval;
break;
case TT_NUMBER:
ret = "n=" + nval;
break;
case TT_NOTHING:
ret = "NOTHING";
break;
default: {
String ret = switch (ttype) {
case TT_EOF -> "EOF";
case TT_EOL -> "EOL";
case TT_WORD -> sval;
case TT_NUMBER -> "n=" + nval;
case TT_NOTHING -> "NOTHING";
default -> {
/*
* ttype is the first character of either a quoted string or
* is an ordinary character. ttype can definitely not be less
* than 0, since those are reserved values used in the previous
* case statements
*/
if (ttype < 256 &&
((ctype[ttype] & CT_QUOTE) != 0)) {
ret = sval;
break;
if (ttype < 256 && ((ctype[ttype] & CT_QUOTE) != 0)) {
yield sval;
}
char s[] = new char[3];
s[0] = s[2] = '\'';
s[1] = (char) ttype;
ret = new String(s);
break;
yield new String(s);
}
}
};
return "Token[" + ret + "], line " + LINENO;
}

@ -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);
};
}
}
}

@ -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;
}

@ -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");
};
}
}

@ -560,22 +560,13 @@ public abstract class BreakIterator implements Cloneable
private static BreakIterator createBreakInstance(LocaleProviderAdapter adapter, Locale locale, int type) {
BreakIteratorProvider breakIteratorProvider = adapter.getBreakIteratorProvider();
BreakIterator iterator = null;
switch (type) {
case CHARACTER_INDEX:
iterator = breakIteratorProvider.getCharacterInstance(locale);
break;
case WORD_INDEX:
iterator = breakIteratorProvider.getWordInstance(locale);
break;
case LINE_INDEX:
iterator = breakIteratorProvider.getLineInstance(locale);
break;
case SENTENCE_INDEX:
iterator = breakIteratorProvider.getSentenceInstance(locale);
break;
}
return iterator;
return switch (type) {
case CHARACTER_INDEX -> breakIteratorProvider.getCharacterInstance(locale);
case WORD_INDEX -> breakIteratorProvider.getWordInstance(locale);
case LINE_INDEX -> breakIteratorProvider.getLineInstance(locale);
case SENTENCE_INDEX -> breakIteratorProvider.getSentenceInstance(locale);
default -> null;
};
}
/**

@ -972,25 +972,14 @@ public abstract class NumberFormat extends Format {
Locale locale, Style formatStyle,
int choice) {
NumberFormatProvider provider = adapter.getNumberFormatProvider();
NumberFormat numberFormat = null;
switch (choice) {
case NUMBERSTYLE:
numberFormat = provider.getNumberInstance(locale);
break;
case PERCENTSTYLE:
numberFormat = provider.getPercentInstance(locale);
break;
case CURRENCYSTYLE:
numberFormat = provider.getCurrencyInstance(locale);
break;
case INTEGERSTYLE:
numberFormat = provider.getIntegerInstance(locale);
break;
case COMPACTSTYLE:
numberFormat = provider.getCompactNumberInstance(locale, formatStyle);
break;
}
return numberFormat;
return switch (choice) {
case NUMBERSTYLE -> provider.getNumberInstance(locale);
case PERCENTSTYLE -> provider.getPercentInstance(locale);
case CURRENCYSTYLE -> provider.getCurrencyInstance(locale);
case INTEGERSTYLE -> provider.getIntegerInstance(locale);
case COMPACTSTYLE -> provider.getCompactNumberInstance(locale, formatStyle);
default -> null;
};
}
/**

@ -130,14 +130,18 @@ class PatternEntry {
if (showWhiteSpace)
toAddTo.append(' ');
}
switch (strength) {
case Collator.IDENTICAL: toAddTo.append('='); break;
case Collator.TERTIARY: toAddTo.append(','); break;
case Collator.SECONDARY: toAddTo.append(';'); break;
case Collator.PRIMARY: toAddTo.append('<'); break;
case RESET: toAddTo.append('&'); break;
case UNSET: toAddTo.append('?'); break;
}
var c = switch (strength) {
case Collator.IDENTICAL -> '=';
case Collator.TERTIARY -> ',';
case Collator.SECONDARY -> ';';
case Collator.PRIMARY -> '<';
case RESET -> '&';
case UNSET -> '?';
default -> throw new IllegalStateException("Unexpected value: " + strength);
};
toAddTo.append(c);
if (showWhiteSpace)
toAddTo.append(' ');
appendQuoted(chars,toAddTo);