Replace cascading if-else's with a case statement.

This commit is contained in:
Gary Gregory 2021-01-03 11:08:27 -05:00
parent 4892703095
commit e6e1682089
1 changed files with 50 additions and 47 deletions

View File

@ -217,61 +217,64 @@ public enum JavaVersion {
* corresponding constant of this enumeration class. This method is used * corresponding constant of this enumeration class. This method is used
* internally. * internally.
* *
* @param nom the Java version as string * @param versionStr the Java version as string
* @return the corresponding enumeration constant or <b>null</b> if the * @return the corresponding enumeration constant or <b>null</b> if the
* version is unknown * version is unknown
*/ */
static JavaVersion get(final String nom) { static JavaVersion get(final String versionStr) {
if (nom == null) { if (versionStr == null) {
return null; return null;
} else if ("0.9".equals(nom)) {
return JAVA_0_9;
} else if ("1.1".equals(nom)) {
return JAVA_1_1;
} else if ("1.2".equals(nom)) {
return JAVA_1_2;
} else if ("1.3".equals(nom)) {
return JAVA_1_3;
} else if ("1.4".equals(nom)) {
return JAVA_1_4;
} else if ("1.5".equals(nom)) {
return JAVA_1_5;
} else if ("1.6".equals(nom)) {
return JAVA_1_6;
} else if ("1.7".equals(nom)) {
return JAVA_1_7;
} else if ("1.8".equals(nom)) {
return JAVA_1_8;
} else if ("9".equals(nom)) {
return JAVA_9;
} else if ("10".equals(nom)) {
return JAVA_10;
} else if ("11".equals(nom)) {
return JAVA_11;
} else if ("12".equals(nom)) {
return JAVA_12;
} else if ("13".equals(nom)) {
return JAVA_13;
} else if ("14".equals(nom)) {
return JAVA_14;
} else if ("15".equals(nom)) {
return JAVA_15;
} else if ("16".equals(nom)) {
return JAVA_16;
} else if ("17".equals(nom)) {
return JAVA_17;
} }
final float v = toFloatVersion(nom); switch (versionStr) {
if ((v - 1.) < 1.) { // then we need to check decimals > .9 case "0.9":
final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(',')); return JAVA_0_9;
final int end = Math.max(nom.length(), nom.indexOf(',', firstComma)); case "1.1":
if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) { return JAVA_1_1;
case "1.2":
return JAVA_1_2;
case "1.3":
return JAVA_1_3;
case "1.4":
return JAVA_1_4;
case "1.5":
return JAVA_1_5;
case "1.6":
return JAVA_1_6;
case "1.7":
return JAVA_1_7;
case "1.8":
return JAVA_1_8;
case "9":
return JAVA_9;
case "10":
return JAVA_10;
case "11":
return JAVA_11;
case "12":
return JAVA_12;
case "13":
return JAVA_13;
case "14":
return JAVA_14;
case "15":
return JAVA_15;
case "16":
return JAVA_16;
case "17":
return JAVA_17;
default:
final float v = toFloatVersion(versionStr);
if ((v - 1.) < 1.) { // then we need to check decimals > .9
final int firstComma = Math.max(versionStr.indexOf('.'), versionStr.indexOf(','));
final int end = Math.max(versionStr.length(), versionStr.indexOf(',', firstComma));
if (Float.parseFloat(versionStr.substring(firstComma + 1, end)) > .9f) {
return JAVA_RECENT;
}
} else if (v > 10) {
return JAVA_RECENT; return JAVA_RECENT;
} }
} else if (v > 10) { return null;
return JAVA_RECENT;
} }
return null;
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------