This is an automated email from the ASF dual-hosted git repository. cstamas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push: new 99777ace02 [MNG-8433] Use the switch expressions syntax (#1983) 99777ace02 is described below commit 99777ace0226034f82299898e598a7f977d525ef Author: Jermaine Hua <crazy...@apache.org> AuthorDate: Tue Dec 17 17:56:12 2024 +0800 [MNG-8433] Use the switch expressions syntax (#1983) Signed-off-by: crazyhzm <crazy...@apache.org> --- https://issues.apache.org/jira/browse/MNG-8433 --- .../artifact/versioning/ComparableVersion.java | 79 +++++-------- .../repository/metadata/MetadataBridge.java | 13 +-- .../model/validation/DefaultModelValidator.java | 16 ++- .../src/main/java/org/apache/maven/utils/Os.java | 47 +++----- .../model/building/DefaultModelBuilderTest.java | 130 +++++++++------------ .../internal/ArtifactDescriptorUtils.java | 17 ++- .../UserPropertiesArtifactRelocationSource.java | 20 +--- .../cling/logging/impl/Log4j2Configuration.java | 20 +--- .../cling/logging/impl/LogbackConfiguration.java | 20 +--- .../logging/impl/MavenSimpleConfiguration.java | 20 +--- .../maven/project/harness/Xpp3DomNodeIterator.java | 13 +-- .../internal/impl/model/DefaultModelValidator.java | 16 ++- .../maven/internal/impl/model/profile/Os.java | 47 +++----- .../impl/resolver/ArtifactDescriptorUtils.java | 17 ++- .../UserPropertiesArtifactRelocationSource.java | 20 +--- .../org/apache/maven/slf4j/MavenBaseLogger.java | 22 ++-- .../org/apache/maven/slf4j/MavenSimpleLogger.java | 20 ++-- .../java/org/apache/maven/slf4j/OutputChoice.java | 17 +-- .../maven/internal/xml/ImmutableCollections.java | 33 +++--- 19 files changed, 219 insertions(+), 368 deletions(-) diff --git a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java index e806eabb38..77cc25b500 100644 --- a/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java +++ b/compat/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java @@ -119,25 +119,19 @@ public class ComparableVersion implements Comparable<ComparableVersion> { return (value == 0) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } - switch (item.getType()) { - case INT_ITEM: + return switch (item.getType()) { + case INT_ITEM -> { int itemValue = ((IntItem) item).value; - return Integer.compare(value, itemValue); - case LONG_ITEM: - case BIGINTEGER_ITEM: - return -1; - - case STRING_ITEM: - return 1; - case COMBINATION_ITEM: - return 1; // 1.1 > 1-sp + yield Integer.compare(value, itemValue); + } + case LONG_ITEM, BIGINTEGER_ITEM -> -1; + case STRING_ITEM -> 1; + case COMBINATION_ITEM -> 1; // 1.1 > 1-sp - case LIST_ITEM: - return 1; // 1.1 > 1-1 + case LIST_ITEM -> 1; // 1.1 > 1-1 - default: - throw new IllegalStateException("invalid item: " + item.getClass()); - } + default -> throw new IllegalStateException("invalid item: " + item.getClass()); + }; } @Override @@ -191,26 +185,20 @@ public class ComparableVersion implements Comparable<ComparableVersion> { return (value == 0) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } - switch (item.getType()) { - case INT_ITEM: - return 1; - case LONG_ITEM: + return switch (item.getType()) { + case INT_ITEM -> 1; + case LONG_ITEM -> { long itemValue = ((LongItem) item).value; - return Long.compare(value, itemValue); - case BIGINTEGER_ITEM: - return -1; - - case STRING_ITEM: - return 1; - case COMBINATION_ITEM: - return 1; // 1.1 > 1-sp + yield Long.compare(value, itemValue); + } + case BIGINTEGER_ITEM -> -1; + case STRING_ITEM -> 1; + case COMBINATION_ITEM -> 1; // 1.1 > 1-sp - case LIST_ITEM: - return 1; // 1.1 > 1-1 + case LIST_ITEM -> 1; // 1.1 > 1-1 - default: - throw new IllegalStateException("invalid item: " + item.getClass()); - } + default -> throw new IllegalStateException("invalid item: " + item.getClass()); + }; } @Override @@ -264,25 +252,16 @@ public class ComparableVersion implements Comparable<ComparableVersion> { return BigInteger.ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } - switch (item.getType()) { - case INT_ITEM: - case LONG_ITEM: - return 1; + return switch (item.getType()) { + case INT_ITEM, LONG_ITEM -> 1; + case BIGINTEGER_ITEM -> value.compareTo(((BigIntegerItem) item).value); + case STRING_ITEM -> 1; + case COMBINATION_ITEM -> 1; // 1.1 > 1-sp - case BIGINTEGER_ITEM: - return value.compareTo(((BigIntegerItem) item).value); - - case STRING_ITEM: - return 1; - case COMBINATION_ITEM: - return 1; // 1.1 > 1-sp + case LIST_ITEM -> 1; // 1.1 > 1-1 - case LIST_ITEM: - return 1; // 1.1 > 1-1 - - default: - throw new IllegalStateException("invalid item: " + item.getClass()); - } + default -> throw new IllegalStateException("invalid item: " + item.getClass()); + }; } @Override diff --git a/compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java b/compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java index 5199cc2051..8c31f36069 100644 --- a/compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java +++ b/compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java @@ -101,14 +101,11 @@ public final class MetadataBridge extends AbstractMetadata implements MergeableM public Nature getNature() { if (metadata instanceof RepositoryMetadata) { - switch (((RepositoryMetadata) metadata).getNature()) { - case RepositoryMetadata.RELEASE_OR_SNAPSHOT: - return Nature.RELEASE_OR_SNAPSHOT; - case RepositoryMetadata.SNAPSHOT: - return Nature.SNAPSHOT; - default: - return Nature.RELEASE; - } + return switch (((RepositoryMetadata) metadata).getNature()) { + case RepositoryMetadata.RELEASE_OR_SNAPSHOT -> Nature.RELEASE_OR_SNAPSHOT; + case RepositoryMetadata.SNAPSHOT -> Nature.SNAPSHOT; + default -> Nature.RELEASE; + }; } else { return Nature.RELEASE; } diff --git a/compat/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/compat/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index 20652a9dac..8c7128dbb3 100644 --- a/compat/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/compat/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -1461,15 +1461,13 @@ public class DefaultModelValidator implements ModelValidator { } private boolean isValidProfileId(String id) { - switch (id.charAt(0)) { // avoid first character that has special CLI meaning in "mvn -P xxx" - case '+': // activate - case '-': // deactivate - case '!': // deactivate - case '?': // optional - return false; - default: - } - return true; + return switch (id.charAt(0)) { // avoid first character that has special CLI meaning in "mvn -P xxx" + // +: activate + // -, !: deactivate + // ?: optional + case '+', '-', '!', '?' -> false; + default -> true; + }; } @SuppressWarnings("checkstyle:parameternumber") diff --git a/compat/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java b/compat/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java index 1a801b8539..f085eb32b4 100644 --- a/compat/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java +++ b/compat/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java @@ -174,36 +174,23 @@ public class Os { || actualOsName.contains("ce")); isNT = !is9x; } - switch (family) { - case FAMILY_WINDOWS: - return isWindows; - case FAMILY_WIN9X: - return isWindows && is9x; - case FAMILY_NT: - return isWindows && isNT; - case FAMILY_OS2: - return actualOsName.contains(FAMILY_OS2); - case FAMILY_NETWARE: - return actualOsName.contains(FAMILY_NETWARE); - case FAMILY_DOS: - return File.pathSeparatorChar == ';' && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows; - case FAMILY_MAC: - return actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN); - case FAMILY_TANDEM: - return actualOsName.contains("nonstop_kernel"); - case FAMILY_UNIX: - return File.pathSeparatorChar == ':' - && !isFamily(FAMILY_OPENVMS, actualOsName) - && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x")); - case FAMILY_ZOS: - return actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390); - case FAMILY_OS400: - return actualOsName.contains(FAMILY_OS400); - case FAMILY_OPENVMS: - return actualOsName.contains(FAMILY_OPENVMS); - default: - return actualOsName.contains(family.toLowerCase(Locale.US)); - } + return switch (family) { + case FAMILY_WINDOWS -> isWindows; + case FAMILY_WIN9X -> isWindows && is9x; + case FAMILY_NT -> isWindows && isNT; + case FAMILY_OS2 -> actualOsName.contains(FAMILY_OS2); + case FAMILY_NETWARE -> actualOsName.contains(FAMILY_NETWARE); + case FAMILY_DOS -> File.pathSeparatorChar == ';' && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows; + case FAMILY_MAC -> actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN); + case FAMILY_TANDEM -> actualOsName.contains("nonstop_kernel"); + case FAMILY_UNIX -> File.pathSeparatorChar == ':' + && !isFamily(FAMILY_OPENVMS, actualOsName) + && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x")); + case FAMILY_ZOS -> actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390); + case FAMILY_OS400 -> actualOsName.contains(FAMILY_OS400); + case FAMILY_OPENVMS -> actualOsName.contains(FAMILY_OPENVMS); + default -> actualOsName.contains(family.toLowerCase(Locale.US)); + }; } /** diff --git a/compat/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java b/compat/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java index dbcc9dd57b..332791b0ac 100644 --- a/compat/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java +++ b/compat/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java @@ -95,13 +95,11 @@ class DefaultModelBuilderTest { @Override public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case BASE1_ID: - return new StringModelSource(BASE1); - case BASE2_ID: - return new StringModelSource(BASE2); - } - return null; + return switch (dependency.getManagementKey()) { + case BASE1_ID -> new StringModelSource(BASE1); + case BASE2_ID -> new StringModelSource(BASE2); + default -> null; + }; } } @@ -183,18 +181,12 @@ class DefaultModelBuilderTest { request.setModelResolver(new BaseModelResolver() { public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case "test:import:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/import.xml") - .getFile())); - default: - throw new UnresolvableModelException( - "Cannot resolve", - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion()); + if (dependency.getManagementKey().equals("test:import:pom")) { + return new FileModelSource(new File( + getClass().getResource("/poms/depmgmt/import.xml").getFile())); } + throw new UnresolvableModelException( + "Cannot resolve", dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); } }); @@ -224,18 +216,12 @@ class DefaultModelBuilderTest { request.setModelResolver(new BaseModelResolver() { public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case "test:import:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/import.xml") - .getFile())); - default: - throw new UnresolvableModelException( - "Cannot resolve", - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion()); + if (dependency.getManagementKey().equals("test:import:pom")) { + return new FileModelSource(new File( + getClass().getResource("/poms/depmgmt/import.xml").getFile())); } + throw new UnresolvableModelException( + "Cannot resolve", dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); } }); @@ -265,22 +251,18 @@ class DefaultModelBuilderTest { request.setModelResolver(new BaseModelResolver() { public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case "test:import:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/import.xml") - .getFile())); - case "test:other:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/other-import.xml") - .getFile())); - default: - throw new UnresolvableModelException( - "Cannot resolve", - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion()); - } + return switch (dependency.getManagementKey()) { + case "test:import:pom" -> new FileModelSource(new File( + getClass().getResource("/poms/depmgmt/import.xml").getFile())); + case "test:other:pom" -> new FileModelSource(new File(getClass() + .getResource("/poms/depmgmt/other-import.xml") + .getFile())); + default -> throw new UnresolvableModelException( + "Cannot resolve", + dependency.getGroupId(), + dependency.getArtifactId(), + dependency.getVersion()); + }; } }); @@ -313,22 +295,19 @@ class DefaultModelBuilderTest { request.setModelResolver(new BaseModelResolver() { public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case "org.junit:bom:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/junit-" + dependency.getVersion() + ".xml") - .getFile())); - case "test:other:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/distant-import.xml") - .getFile())); - default: - throw new UnresolvableModelException( - "Cannot resolve", - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion()); - } + return switch (dependency.getManagementKey()) { + case "org.junit:bom:pom" -> new FileModelSource(new File(getClass() + .getResource("/poms/depmgmt/junit-" + dependency.getVersion() + ".xml") + .getFile())); + case "test:other:pom" -> new FileModelSource(new File(getClass() + .getResource("/poms/depmgmt/distant-import.xml") + .getFile())); + default -> throw new UnresolvableModelException( + "Cannot resolve", + dependency.getGroupId(), + dependency.getArtifactId(), + dependency.getVersion()); + }; } }); @@ -363,22 +342,19 @@ class DefaultModelBuilderTest { request.setModelResolver(new BaseModelResolver() { public ModelSource resolveModel(org.apache.maven.model.Dependency dependency) throws UnresolvableModelException { - switch (dependency.getManagementKey()) { - case "org.junit:bom:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/junit-" + dependency.getVersion() + ".xml") - .getFile())); - case "test:other:pom": - return new FileModelSource(new File(getClass() - .getResource("/poms/depmgmt/distant-import.xml") - .getFile())); - default: - throw new UnresolvableModelException( - "Cannot resolve", - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion()); - } + return switch (dependency.getManagementKey()) { + case "org.junit:bom:pom" -> new FileModelSource(new File(getClass() + .getResource("/poms/depmgmt/junit-" + dependency.getVersion() + ".xml") + .getFile())); + case "test:other:pom" -> new FileModelSource(new File(getClass() + .getResource("/poms/depmgmt/distant-import.xml") + .getFile())); + default -> throw new UnresolvableModelException( + "Cannot resolve", + dependency.getGroupId(), + dependency.getArtifactId(), + dependency.getVersion()); + }; } }); diff --git a/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java b/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java index 74b3b7ff34..821db5de14 100644 --- a/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java +++ b/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java @@ -82,15 +82,12 @@ public class ArtifactDescriptorUtils { } public static String toRepositoryChecksumPolicy(final String artifactRepositoryPolicy) { - switch (artifactRepositoryPolicy) { - case RepositoryPolicy.CHECKSUM_POLICY_FAIL: - return RepositoryPolicy.CHECKSUM_POLICY_FAIL; - case RepositoryPolicy.CHECKSUM_POLICY_IGNORE: - return RepositoryPolicy.CHECKSUM_POLICY_IGNORE; - case RepositoryPolicy.CHECKSUM_POLICY_WARN: - return RepositoryPolicy.CHECKSUM_POLICY_WARN; - default: - throw new IllegalArgumentException("unknown repository checksum policy: " + artifactRepositoryPolicy); - } + return switch (artifactRepositoryPolicy) { + case RepositoryPolicy.CHECKSUM_POLICY_FAIL -> RepositoryPolicy.CHECKSUM_POLICY_FAIL; + case RepositoryPolicy.CHECKSUM_POLICY_IGNORE -> RepositoryPolicy.CHECKSUM_POLICY_IGNORE; + case RepositoryPolicy.CHECKSUM_POLICY_WARN -> RepositoryPolicy.CHECKSUM_POLICY_WARN; + default -> throw new IllegalArgumentException( + "unknown repository checksum policy: " + artifactRepositoryPolicy); + }; } } diff --git a/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java b/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java index 2079f35da5..37a7c3b416 100644 --- a/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java +++ b/compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java @@ -198,20 +198,12 @@ public final class UserPropertiesArtifactRelocationSource implements MavenArtifa private static Artifact parseArtifact(String coords) { Artifact s; String[] parts = coords.split(":"); - switch (parts.length) { - case 3: - s = new DefaultArtifact(parts[0], parts[1], "*", "*", parts[2]); - break; - case 4: - s = new DefaultArtifact(parts[0], parts[1], "*", parts[2], parts[3]); - break; - case 5: - s = new DefaultArtifact(parts[0], parts[1], parts[2], parts[3], parts[4]); - break; - default: - throw new IllegalArgumentException("Bad artifact coordinates " + coords - + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>"); - } + s = switch (parts.length) { + case 3 -> new DefaultArtifact(parts[0], parts[1], "*", "*", parts[2]); + case 4 -> new DefaultArtifact(parts[0], parts[1], "*", parts[2], parts[3]); + case 5 -> new DefaultArtifact(parts[0], parts[1], parts[2], parts[3], parts[4]); + default -> throw new IllegalArgumentException("Bad artifact coordinates " + coords + + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>");}; return s; } } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java index 1f02e57750..bbd487fa5f 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java @@ -28,20 +28,12 @@ import org.apache.maven.cling.logging.BaseSlf4jConfiguration; public class Log4j2Configuration extends BaseSlf4jConfiguration { @Override public void setRootLoggerLevel(Level level) { - String value; - switch (level) { - case DEBUG: - value = "debug"; - break; - - case INFO: - value = "info"; - break; - - default: - value = "error"; - break; - } + String value = + switch (level) { + case DEBUG -> "debug"; + case INFO -> "info"; + default -> "error"; + }; System.setProperty("maven.logging.root.level", value); } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java index 4481486218..67ee429d82 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java @@ -30,20 +30,12 @@ import org.slf4j.LoggerFactory; public class LogbackConfiguration extends BaseSlf4jConfiguration { @Override public void setRootLoggerLevel(Level level) { - ch.qos.logback.classic.Level value; - switch (level) { - case DEBUG: - value = ch.qos.logback.classic.Level.DEBUG; - break; - - case INFO: - value = ch.qos.logback.classic.Level.INFO; - break; - - default: - value = ch.qos.logback.classic.Level.ERROR; - break; - } + ch.qos.logback.classic.Level value = + switch (level) { + case DEBUG -> ch.qos.logback.classic.Level.DEBUG; + case INFO -> ch.qos.logback.classic.Level.INFO; + default -> ch.qos.logback.classic.Level.ERROR; + }; ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(value); } diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java index 5147cd8864..e7fb25b39d 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/MavenSimpleConfiguration.java @@ -31,20 +31,12 @@ import org.slf4j.LoggerFactory; public class MavenSimpleConfiguration extends BaseSlf4jConfiguration { @Override public void setRootLoggerLevel(Level level) { - String value; - switch (level) { - case DEBUG: - value = "debug"; - break; - - case INFO: - value = "info"; - break; - - default: - value = "error"; - break; - } + String value = + switch (level) { + case DEBUG -> "debug"; + case INFO -> "info"; + default -> "error"; + }; System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", value); } diff --git a/impl/maven-core/src/test/java/org/apache/maven/project/harness/Xpp3DomNodeIterator.java b/impl/maven-core/src/test/java/org/apache/maven/project/harness/Xpp3DomNodeIterator.java index 3f506a3bcf..0651d70ad2 100644 --- a/impl/maven-core/src/test/java/org/apache/maven/project/harness/Xpp3DomNodeIterator.java +++ b/impl/maven-core/src/test/java/org/apache/maven/project/harness/Xpp3DomNodeIterator.java @@ -122,14 +122,11 @@ class Xpp3DomNodeIterator implements NodeIterator { return false; } if (test instanceof NodeTypeTest) { - switch (((NodeTypeTest) test).getNodeType()) { - case Compiler.NODE_TYPE_NODE: - return true; - case Compiler.NODE_TYPE_TEXT: - return node.getValue() != null; - default: - return false; - } + return switch (((NodeTypeTest) test).getNodeType()) { + case Compiler.NODE_TYPE_NODE -> true; + case Compiler.NODE_TYPE_TEXT -> node.getValue() != null; + default -> false; + }; } return false; } diff --git a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java index e67fd88717..278b6984d4 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java @@ -1534,15 +1534,13 @@ public class DefaultModelValidator implements ModelValidator { } private boolean isValidProfileId(String id) { - switch (id.charAt(0)) { // avoid first character that has special CLI meaning in "mvn -P xxx" - case '+': // activate - case '-': // deactivate - case '!': // deactivate - case '?': // optional - return false; - default: - } - return true; + return switch (id.charAt(0)) { // avoid first character that has special CLI meaning in "mvn -P xxx" + // +: activate + // -, !: deactivate + // ?: optional + case '+', '-', '!', '?' -> false; + default -> true; + }; } @SuppressWarnings("checkstyle:parameternumber") diff --git a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/profile/Os.java b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/profile/Os.java index 9f65ea6107..8ca920d293 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/profile/Os.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/model/profile/Os.java @@ -175,36 +175,23 @@ public class Os { || actualOsName.contains("ce")); isNT = !is9x; } - switch (family) { - case FAMILY_WINDOWS: - return isWindows; - case FAMILY_WIN9X: - return isWindows && is9x; - case FAMILY_NT: - return isWindows && isNT; - case FAMILY_OS2: - return actualOsName.contains(FAMILY_OS2); - case FAMILY_NETWARE: - return actualOsName.contains(FAMILY_NETWARE); - case FAMILY_DOS: - return PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows; - case FAMILY_MAC: - return actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN); - case FAMILY_TANDEM: - return actualOsName.contains("nonstop_kernel"); - case FAMILY_UNIX: - return PATH_SEP.equals(":") - && !isFamily(FAMILY_OPENVMS, actualOsName) - && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x")); - case FAMILY_ZOS: - return actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390); - case FAMILY_OS400: - return actualOsName.contains(FAMILY_OS400); - case FAMILY_OPENVMS: - return actualOsName.contains(FAMILY_OPENVMS); - default: - return actualOsName.contains(family.toLowerCase(Locale.US)); - } + return switch (family) { + case FAMILY_WINDOWS -> isWindows; + case FAMILY_WIN9X -> isWindows && is9x; + case FAMILY_NT -> isWindows && isNT; + case FAMILY_OS2 -> actualOsName.contains(FAMILY_OS2); + case FAMILY_NETWARE -> actualOsName.contains(FAMILY_NETWARE); + case FAMILY_DOS -> PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows; + case FAMILY_MAC -> actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN); + case FAMILY_TANDEM -> actualOsName.contains("nonstop_kernel"); + case FAMILY_UNIX -> PATH_SEP.equals(":") + && !isFamily(FAMILY_OPENVMS, actualOsName) + && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x")); + case FAMILY_ZOS -> actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390); + case FAMILY_OS400 -> actualOsName.contains(FAMILY_OS400); + case FAMILY_OPENVMS -> actualOsName.contains(FAMILY_OPENVMS); + default -> actualOsName.contains(family.toLowerCase(Locale.US)); + }; } /** diff --git a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/ArtifactDescriptorUtils.java b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/ArtifactDescriptorUtils.java index bda516d62d..6edf21eb74 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/ArtifactDescriptorUtils.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/ArtifactDescriptorUtils.java @@ -80,15 +80,12 @@ public class ArtifactDescriptorUtils { } public static String toRepositoryChecksumPolicy(final String artifactRepositoryPolicy) { - switch (artifactRepositoryPolicy) { - case RepositoryPolicy.CHECKSUM_POLICY_FAIL: - return RepositoryPolicy.CHECKSUM_POLICY_FAIL; - case RepositoryPolicy.CHECKSUM_POLICY_IGNORE: - return RepositoryPolicy.CHECKSUM_POLICY_IGNORE; - case RepositoryPolicy.CHECKSUM_POLICY_WARN: - return RepositoryPolicy.CHECKSUM_POLICY_WARN; - default: - throw new IllegalArgumentException("unknown repository checksum policy: " + artifactRepositoryPolicy); - } + return switch (artifactRepositoryPolicy) { + case RepositoryPolicy.CHECKSUM_POLICY_FAIL -> RepositoryPolicy.CHECKSUM_POLICY_FAIL; + case RepositoryPolicy.CHECKSUM_POLICY_IGNORE -> RepositoryPolicy.CHECKSUM_POLICY_IGNORE; + case RepositoryPolicy.CHECKSUM_POLICY_WARN -> RepositoryPolicy.CHECKSUM_POLICY_WARN; + default -> throw new IllegalArgumentException( + "unknown repository checksum policy: " + artifactRepositoryPolicy); + }; } } diff --git a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/relocation/UserPropertiesArtifactRelocationSource.java b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/relocation/UserPropertiesArtifactRelocationSource.java index 7e25ba3257..c3cc346445 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/relocation/UserPropertiesArtifactRelocationSource.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/internal/impl/resolver/relocation/UserPropertiesArtifactRelocationSource.java @@ -194,20 +194,12 @@ public final class UserPropertiesArtifactRelocationSource implements MavenArtifa private static Artifact parseArtifact(String coords) { Artifact s; String[] parts = coords.split(":"); - switch (parts.length) { - case 3: - s = new DefaultArtifact(parts[0], parts[1], "*", "*", parts[2]); - break; - case 4: - s = new DefaultArtifact(parts[0], parts[1], "*", parts[2], parts[3]); - break; - case 5: - s = new DefaultArtifact(parts[0], parts[1], parts[2], parts[3], parts[4]); - break; - default: - throw new IllegalArgumentException("Bad artifact coordinates " + coords - + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>"); - } + s = switch (parts.length) { + case 3 -> new DefaultArtifact(parts[0], parts[1], "*", "*", parts[2]); + case 4 -> new DefaultArtifact(parts[0], parts[1], "*", parts[2], parts[3]); + case 5 -> new DefaultArtifact(parts[0], parts[1], parts[2], parts[3], parts[4]); + default -> throw new IllegalArgumentException("Bad artifact coordinates " + coords + + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>");}; return s; } } diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java index 19fc45a665..f24c17b32f 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java @@ -442,20 +442,14 @@ public class MavenBaseLogger extends LegacyAbstractLogger { } protected String renderLevel(int levelInt) { - switch (levelInt) { - case LOG_LEVEL_TRACE: - return "TRACE"; - case LOG_LEVEL_DEBUG: - return ("DEBUG"); - case LOG_LEVEL_INFO: - return "INFO"; - case LOG_LEVEL_WARN: - return "WARN"; - case LOG_LEVEL_ERROR: - return "ERROR"; - default: - throw new IllegalStateException("Unrecognized level [" + levelInt + "]"); - } + return switch (levelInt) { + case LOG_LEVEL_TRACE -> "TRACE"; + case LOG_LEVEL_DEBUG -> ("DEBUG"); + case LOG_LEVEL_INFO -> "INFO"; + case LOG_LEVEL_WARN -> "WARN"; + case LOG_LEVEL_ERROR -> "ERROR"; + default -> throw new IllegalStateException("Unrecognized level [" + levelInt + "]"); + }; } public void log(LoggingEvent event) { diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java index e8b1fa26de..17927d9ec6 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java @@ -58,19 +58,13 @@ public class MavenSimpleLogger extends MavenBaseLogger { warnRenderedLevel = builder().warning("WARNING").build(); errorRenderedLevel = builder().error("ERROR").build(); } - switch (level) { - case LOG_LEVEL_TRACE: - return traceRenderedLevel; - case LOG_LEVEL_DEBUG: - return debugRenderedLevel; - case LOG_LEVEL_INFO: - return infoRenderedLevel; - case LOG_LEVEL_WARN: - return warnRenderedLevel; - case LOG_LEVEL_ERROR: - default: - return errorRenderedLevel; - } + return switch (level) { + case LOG_LEVEL_TRACE -> traceRenderedLevel; + case LOG_LEVEL_DEBUG -> debugRenderedLevel; + case LOG_LEVEL_INFO -> infoRenderedLevel; + case LOG_LEVEL_WARN -> warnRenderedLevel; + default -> errorRenderedLevel; + }; } protected void write(StringBuilder buf, Throwable t) { diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/OutputChoice.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/OutputChoice.java index fa491b596d..8d9764cc58 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/OutputChoice.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/OutputChoice.java @@ -59,17 +59,10 @@ class OutputChoice { } PrintStream getTargetPrintStream() { - switch (outputChoiceType) { - case SYS_OUT: - return System.out; - case SYS_ERR: - return System.err; - case CACHED_SYS_ERR: - case CACHED_SYS_OUT: - case FILE: - return targetPrintStream; - default: - throw new IllegalArgumentException(); - } + return switch (outputChoiceType) { + case SYS_OUT -> System.out; + case SYS_ERR -> System.err; + case CACHED_SYS_ERR, CACHED_SYS_OUT, FILE -> targetPrintStream; + }; } } diff --git a/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java b/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java index 4daeeb51c6..dc6651da94 100644 --- a/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java +++ b/impl/maven-xml/src/main/java/org/apache/maven/internal/xml/ImmutableCollections.java @@ -84,17 +84,15 @@ class ImmutableCollections { } else if (collection instanceof AbstractImmutableList) { return (List<E>) collection; } else { - switch (collection.size()) { - case 0: - return emptyList(); - case 1: - return singletonList(collection.iterator().next()); - case 2: + return switch (collection.size()) { + case 0 -> emptyList(); + case 1 -> singletonList(collection.iterator().next()); + case 2 -> { Iterator<E> it = collection.iterator(); - return new List2<>(it.next(), it.next()); - default: - return new ListN<>(collection); - } + yield new List2<>(it.next(), it.next()); + } + default -> new ListN<>(collection); + }; } } @@ -113,15 +111,14 @@ class ImmutableCollections { } else if (map instanceof AbstractImmutableMap) { return map; } else { - switch (map.size()) { - case 0: - return emptyMap(); - case 1: + return switch (map.size()) { + case 0 -> emptyMap(); + case 1 -> { Map.Entry<K, V> entry = map.entrySet().iterator().next(); - return singletonMap(entry.getKey(), entry.getValue()); - default: - return new MapN<>(map); - } + yield singletonMap(entry.getKey(), entry.getValue()); + } + default -> new MapN<>(map); + }; } }