Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1674: Make use of enhanced switch expression introduced in Java 14 #715

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,13 @@ static Span extract(int begin, int end, String beginTag) throws InvalidFormatExc

String type = beginTag.substring(2);

switch (type) {
case "PER":
type = "person";
break;
case "LOC":
type = "location";
break;
case "MISC":
type = "misc";
break;
case "ORG":
type = "organization";
break;
default:
throw new InvalidFormatException("Unknown type: " + type);
}
type = switch (type) {
case "PER" -> "person";
case "LOC" -> "location";
case "MISC" -> "misc";
case "ORG" -> "organization";
default -> throw new InvalidFormatException("Unknown type: " + type);
};

return new Span(begin, end, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,13 @@ private static Span extract(int begin, int end, String beginTag) throws InvalidF

String type = beginTag.substring(2);

switch (type) {
case "PER":
type = "person";
break;
case "LOC":
type = "location";
break;
case "GPE":
type = "gpe";
break;
case "ORG":
type = "organization";
break;
default:
throw new InvalidFormatException("Unknown type: " + type);
}
type = switch (type) {
case "PER" -> "person";
case "LOC" -> "location";
case "GPE" -> "gpe";
case "ORG" -> "organization";
default -> throw new InvalidFormatException("Unknown type: " + type);
};

return new Span(begin, end, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ protected ConlluLemmaSampleStreamFactory(Class<P> params) {
public ObjectStream<LemmaSample> create(String[] args) {
Parameters params = ArgumentParser.parse(args, Parameters.class);

ConlluTagset tagset;

switch (params.getTagset()) {
case "u":
tagset = ConlluTagset.U;
break;
case "x":
tagset = ConlluTagset.X;
break;
default:
throw new TerminateToolException(-1, "Unknown tagset parameter: " + params.getTagset());
}
ConlluTagset tagset = switch (params.getTagset()) {
case "u" -> ConlluTagset.U;
case "x" -> ConlluTagset.X;
default -> throw new TerminateToolException(-1, "Unknown tagset parameter: " + params.getTagset());
};

InputStreamFactory inFactory =
CmdLineUtil.createInputStreamFactory(params.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,11 @@ protected ConlluPOSSampleStreamFactory(Class<P> params) {
public ObjectStream<POSSample> create(String[] args) {
Parameters params = ArgumentParser.parse(args, Parameters.class);

ConlluTagset tagset;

switch (params.getTagset()) {
case "u":
tagset = ConlluTagset.U;
break;
case "x":
tagset = ConlluTagset.X;
break;
default:
throw new TerminateToolException(-1, "Unknown tagset parameter: " + params.getTagset());
}
ConlluTagset tagset = switch (params.getTagset()) {
case "u" -> ConlluTagset.U;
case "x" -> ConlluTagset.X;
default -> throw new TerminateToolException(-1, "Unknown tagset parameter: " + params.getTagset());
};

InputStreamFactory inFactory =
CmdLineUtil.createInputStreamFactory(params.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ public String getLemma() {
* @throws IllegalStateException Thrown if a non-supported {@link ConlluTagset} was specified.
*/
public String getPosTag(ConlluTagset tagset) {
switch (tagset) {
case U:
return uPosTag;
case X:
return xPosTag;
default:
throw new IllegalStateException("Unexpected tagset value: " + tagset);
}
return switch (tagset) {
case U -> uPosTag;
case X -> xPosTag;
default -> throw new IllegalStateException("Unexpected tagset value: " + tagset);
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,14 @@ public static DataIndexer getDataIndexer(TrainingParameters parameters, Map<Stri
reportMap = new HashMap<>();
}

DataIndexer indexer;
switch (indexerParam) {
case AbstractEventTrainer.DATA_INDEXER_ONE_PASS_VALUE:
indexer = new OnePassDataIndexer();
break;

case AbstractEventTrainer.DATA_INDEXER_TWO_PASS_VALUE:
indexer = new TwoPassDataIndexer();
break;

case AbstractEventTrainer.DATA_INDEXER_ONE_PASS_REAL_VALUE:
indexer = new OnePassRealValueDataIndexer();
break;

default:
DataIndexer indexer = switch (indexerParam) {
case AbstractEventTrainer.DATA_INDEXER_ONE_PASS_VALUE -> new OnePassDataIndexer();
case AbstractEventTrainer.DATA_INDEXER_TWO_PASS_VALUE -> new TwoPassDataIndexer();
case AbstractEventTrainer.DATA_INDEXER_ONE_PASS_REAL_VALUE -> new OnePassRealValueDataIndexer();
default ->
// if the user passes in a class name for the indexer, try to instantiate the class.
indexer = ExtensionLoader.instantiateExtension(DataIndexer.class, indexerParam);
}
ExtensionLoader.instantiateExtension(DataIndexer.class, indexerParam);
};

indexer.init(parameters, reportMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,11 @@ public char[] getResultBuffer() {
/* cons(i) is true <=> b[i] is a consonant. */

private boolean cons(int i) {
switch (b[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return false;
case 'y':
return (i == k0) || !cons(i - 1);
default:
return true;
}
return switch (b[i]) {
case 'a', 'e', 'i', 'o', 'u' -> false;
case 'y' -> (i == k0) || !cons(i - 1);
default -> true;
};
}

/* m() measures the number of consonant sequences between k0 and j. if c is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,13 @@ public TokenSample next() {
for (int ti = 0; ti < tokens.length; ti++) {
String token = tokens[ti];
String lastToken = ti - 1 >= 0 ? tokens[ti - 1] : "";
switch (token) {
case "-LRB-":
token = "(";
break;
case "-LCB-":
token = "{";
break;
case "-RRB-":
token = ")";
break;
case "-RCB-":
token = "}";
break;
}
token = switch (token) {
case "-LRB-" -> "(";
case "-LCB-" -> "{";
case "-RRB-" -> ")";
case "-RCB-" -> "}";
default -> token;
};
if (sb.length() != 0) {
if (!alphaNumeric.matcher(token).find() || token.startsWith("'") || token.equalsIgnoreCase("n't")) {
if ((token.equals("``") || token.equals("--") || token.equals("$") ||
Expand Down
Loading