Use new Java features (switch expressions, text blocks, new JDK methods)

Closes gh-29747
This commit is contained in:
Krzysztof Krason
2022-12-28 08:59:08 +01:00
committed by Sam Brannen
parent 48abd493fe
commit afb8a0d1b1
53 changed files with 498 additions and 552 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -78,11 +78,14 @@ public class StringDecoderBenchmark {
@Setup(Level.Trial)
public void setup() {
String eventTemplate = "id:$1\n" +
"event:some-event\n" +
":some-comment-$1-aa\n" +
":some-comment-$1-bb\n" +
"data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1\n\n";
String eventTemplate = """
id:$1
event:some-event
:some-comment-$1-aa
:some-comment-$1-bb
data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1
""";
int eventLength = String.format(eventTemplate, String.format("%05d", 1)).length();
int eventCount = this.totalSize / eventLength;

View File

@@ -152,42 +152,27 @@ class BasicJsonWriter {
private static String escape(CharSequence input) {
StringBuilder builder = new StringBuilder();
input.chars().forEach(c -> {
switch (c) {
case '"':
builder.append("\\\"");
break;
case '\\':
builder.append("\\\\");
break;
case '/':
builder.append("\\/");
break;
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
default:
if (c <= 0x1F) {
builder.append(String.format("\\u%04x", c));
}
else {
builder.append((char) c);
}
break;
}
});
input.chars().forEach(c -> builder.append(
switch (c) {
case '"' -> "\\\"";
case '\\' -> "\\\\";
case '/' -> "\\/";
case '\b' -> "\\b";
case '\f' -> "\\f";
case '\n' -> "\\n";
case '\r' -> "\\r";
case '\t' -> "\\t";
default -> {
if (c <= 0x1F) {
yield String.format("\\u%04x", c);
}
else {
yield (char) c;
}
}
}
)
);
return builder.toString();
}

View File

@@ -314,18 +314,14 @@ public abstract class MimeTypeUtils {
int i = 0;
while (i < mimeTypes.length()) {
switch (mimeTypes.charAt(i)) {
case '"':
inQuotes = !inQuotes;
break;
case ',':
case '"' -> inQuotes = !inQuotes;
case ',' -> {
if (!inQuotes) {
tokens.add(mimeTypes.substring(startIndex, i));
startIndex = i + 1;
}
break;
case '\\':
i++;
break;
}
case '\\' -> i++;
}
i++;
}

View File

@@ -96,18 +96,18 @@ public abstract class FutureAdapter<T, S> implements Future<T> {
@Nullable
final T adaptInternal(S adapteeResult) throws ExecutionException {
synchronized (this.mutex) {
switch (this.state) {
case SUCCESS:
return (T) this.result;
case FAILURE:
return switch (this.state) {
case SUCCESS -> (T) this.result;
case FAILURE -> {
Assert.state(this.result instanceof ExecutionException, "Failure without exception");
throw (ExecutionException) this.result;
case NEW:
}
case NEW -> {
try {
T adapted = adapt(adapteeResult);
this.result = adapted;
this.state = State.SUCCESS;
return adapted;
yield adapted;
}
catch (ExecutionException ex) {
this.result = ex;
@@ -120,9 +120,8 @@ public abstract class FutureAdapter<T, S> implements Future<T> {
this.state = State.FAILURE;
throw execEx;
}
default:
throw new IllegalStateException();
}
}
};
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,21 +65,19 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
switch (name) {
case NAMESPACES_FEATURE_NAME:
return this.namespacesFeature;
case NAMESPACE_PREFIXES_FEATURE_NAME:
return this.namespacePrefixesFeature;
case IS_STANDALONE_FEATURE_NAME:
return switch (name) {
case NAMESPACES_FEATURE_NAME -> this.namespacesFeature;
case NAMESPACE_PREFIXES_FEATURE_NAME -> this.namespacePrefixesFeature;
case IS_STANDALONE_FEATURE_NAME -> {
if (this.isStandalone != null) {
return this.isStandalone;
yield this.isStandalone;
}
else {
throw new SAXNotSupportedException("startDocument() callback not completed yet");
}
default:
return super.getFeature(name);
}
}
default -> super.getFeature(name);
};
}
@Override