Skip to content

Commit

Permalink
[MNG-8433] Use the switch expressions syntax (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHZM authored Dec 17, 2024
1 parent cd36684 commit 99777ac
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,19 @@ public int compareTo(Item item) {
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
Expand Down Expand Up @@ -191,26 +185,20 @@ public int compareTo(Item item) {
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
Expand Down Expand Up @@ -264,25 +252,16 @@ public int compareTo(Item item) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@ public Path getPath() {

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1461,15 +1461,13 @@ private boolean validateProfileId(
}

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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,23 @@ public static boolean isFamily(String family, String actualOsName) {
|| 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));
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ static class CycleInImportsResolver extends BaseModelResolver {
@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;
};
}
}

Expand Down Expand Up @@ -183,18 +181,12 @@ void testManagedDependencyBeforeImport() throws Exception {
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());
}
});

Expand Down Expand Up @@ -224,18 +216,12 @@ void testManagedDependencyAfterImport() throws Exception {
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());
}
});

Expand Down Expand Up @@ -265,22 +251,18 @@ void testManagedDependencyTwoImports() throws Exception {
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());
};
}
});

Expand Down Expand Up @@ -313,22 +295,19 @@ void testManagedDependencyDistance() throws Exception {
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());
};
}
});

Expand Down Expand Up @@ -363,22 +342,19 @@ void testManagedDependencyDistanceWithExplicit() throws Exception {
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());
};
}
});

Expand Down
Loading

0 comments on commit 99777ac

Please sign in to comment.