Use enhanced switch expressions where feasible

Closes gh-28014
This commit is contained in:
a.yazychyan
2022-02-07 20:51:32 +03:00
committed by Sam Brannen
parent 42d114534b
commit c5c926726d
38 changed files with 291 additions and 493 deletions

View File

@@ -163,22 +163,13 @@ public final class TypePath {
int length = getLength();
StringBuilder result = new StringBuilder(length * 2);
for (int i = 0; i < length; ++i) {
switch (getStep(i)) {
case ARRAY_ELEMENT:
result.append('[');
break;
case INNER_TYPE:
result.append('.');
break;
case WILDCARD_BOUND:
result.append('*');
break;
case TYPE_ARGUMENT:
result.append(getStepArgument(i)).append(';');
break;
default:
throw new AssertionError();
}
switch (getStep(i)) {
case ARRAY_ELEMENT -> result.append('[');
case INNER_TYPE -> result.append('.');
case WILDCARD_BOUND -> result.append('*');
case TYPE_ARGUMENT -> result.append(getStepArgument(i)).append(';');
default -> throw new AssertionError();
}
}
return result.toString();
}

View File

@@ -95,19 +95,13 @@ abstract class AnnotationsScanner {
private static <C, R> R processClass(C context, Class<?> source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
switch (searchStrategy) {
case DIRECT:
return processElement(context, source, processor);
case INHERITED_ANNOTATIONS:
return processClassInheritedAnnotations(context, source, searchStrategy, processor);
case SUPERCLASS:
return processClassHierarchy(context, source, processor, false, false);
case TYPE_HIERARCHY:
return processClassHierarchy(context, source, processor, true, false);
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
return processClassHierarchy(context, source, processor, true, true);
}
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
return switch (searchStrategy) {
case DIRECT -> processElement(context, source, processor);
case INHERITED_ANNOTATIONS -> processClassInheritedAnnotations(context, source, searchStrategy, processor);
case SUPERCLASS -> processClassHierarchy(context, source, processor, false, false);
case TYPE_HIERARCHY -> processClassHierarchy(context, source, processor, true, false);
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processClassHierarchy(context, source, processor, true, true);
};
}
@Nullable
@@ -238,19 +232,14 @@ abstract class AnnotationsScanner {
private static <C, R> R processMethod(C context, Method source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> processor) {
switch (searchStrategy) {
case DIRECT:
case INHERITED_ANNOTATIONS:
return processMethodInheritedAnnotations(context, source, processor);
case SUPERCLASS:
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
processor, source, false);
case TYPE_HIERARCHY:
case TYPE_HIERARCHY_AND_ENCLOSING_CLASSES:
return processMethodHierarchy(context, new int[] {0}, source.getDeclaringClass(),
processor, source, true);
}
throw new IllegalStateException("Unsupported search strategy " + searchStrategy);
return switch (searchStrategy) {
case DIRECT, INHERITED_ANNOTATIONS -> processMethodInheritedAnnotations(context, source, processor);
case SUPERCLASS -> processMethodHierarchy(context, new int[]{0}, source.getDeclaringClass(),
processor, source, false);
case TYPE_HIERARCHY, TYPE_HIERARCHY_AND_ENCLOSING_CLASSES -> processMethodHierarchy(context, new int[]{0},
source.getDeclaringClass(),
processor, source, true);
};
}
@Nullable

View File

@@ -70,25 +70,23 @@ final class ProfilesParser {
continue;
}
switch (token) {
case "(":
case "(" -> {
Profiles contents = parseTokens(expression, tokens, Context.BRACKET);
if (context == Context.INVERT) {
return contents;
}
elements.add(contents);
break;
case "&":
}
case "&" -> {
assertWellFormed(expression, operator == null || operator == Operator.AND);
operator = Operator.AND;
break;
case "|":
}
case "|" -> {
assertWellFormed(expression, operator == null || operator == Operator.OR);
operator = Operator.OR;
break;
case "!":
elements.add(not(parseTokens(expression, tokens, Context.INVERT)));
break;
case ")":
}
case "!" -> elements.add(not(parseTokens(expression, tokens, Context.INVERT)));
case ")" -> {
Profiles merged = merge(expression, elements, operator);
if (context == Context.BRACKET) {
return merged;
@@ -96,13 +94,14 @@ final class ProfilesParser {
elements.clear();
elements.add(merged);
operator = null;
break;
default:
}
default -> {
Profiles value = equals(token);
if (context == Context.INVERT) {
return value;
}
elements.add(value);
}
}
}
return merge(expression, elements, operator);

View File

@@ -608,14 +608,11 @@ public abstract class DataBufferUtils {
private static NestedMatcher createMatcher(byte[] delimiter) {
Assert.isTrue(delimiter.length > 0, "Delimiter must not be empty");
switch (delimiter.length) {
case 1:
return (delimiter[0] == 10 ? SingleByteMatcher.NEWLINE_MATCHER : new SingleByteMatcher(delimiter));
case 2:
return new TwoByteMatcher(delimiter);
default:
return new KnuthMorrisPrattMatcher(delimiter);
}
return switch (delimiter.length) {
case 1 -> (delimiter[0] == 10 ? SingleByteMatcher.NEWLINE_MATCHER : new SingleByteMatcher(delimiter));
case 2 -> new TwoByteMatcher(delimiter);
default -> new KnuthMorrisPrattMatcher(delimiter);
};
}

View File

@@ -56,16 +56,12 @@ public class ListenableFutureCallbackRegistry<T> {
Assert.notNull(callback, "'callback' must not be null");
synchronized (this.mutex) {
switch (this.state) {
case NEW:
case NEW -> {
this.successCallbacks.add(callback);
this.failureCallbacks.add(callback);
break;
case SUCCESS:
notifySuccess(callback);
break;
case FAILURE:
notifyFailure(callback);
break;
}
case SUCCESS -> notifySuccess(callback);
case FAILURE -> notifyFailure(callback);
}
}
}
@@ -99,12 +95,8 @@ public class ListenableFutureCallbackRegistry<T> {
Assert.notNull(callback, "'callback' must not be null");
synchronized (this.mutex) {
switch (this.state) {
case NEW:
this.successCallbacks.add(callback);
break;
case SUCCESS:
notifySuccess(callback);
break;
case NEW -> this.successCallbacks.add(callback);
case SUCCESS -> notifySuccess(callback);
}
}
}
@@ -118,12 +110,8 @@ public class ListenableFutureCallbackRegistry<T> {
Assert.notNull(callback, "'callback' must not be null");
synchronized (this.mutex) {
switch (this.state) {
case NEW:
this.failureCallbacks.add(callback);
break;
case FAILURE:
notifyFailure(callback);
break;
case NEW -> this.failureCallbacks.add(callback);
case FAILURE -> notifyFailure(callback);
}
}
}