Skip to content

Commit 4889453

Browse files
authored
Refactor if-else chains to switch in benchmark methods (#2756)
1 parent 3741287 commit 4889453

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

metrics/src/main/java/com/google/gson/metrics/BagOfPrimitivesDeserializationBenchmark.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ public void timeBagOfPrimitivesStreaming(int reps) throws IOException {
6464
String stringValue = null;
6565
while (jr.hasNext()) {
6666
String name = jr.nextName();
67-
if (name.equals("longValue")) {
68-
longValue = jr.nextLong();
69-
} else if (name.equals("intValue")) {
70-
intValue = jr.nextInt();
71-
} else if (name.equals("booleanValue")) {
72-
booleanValue = jr.nextBoolean();
73-
} else if (name.equals("stringValue")) {
74-
stringValue = jr.nextString();
75-
} else {
76-
throw new IOException("Unexpected name: " + name);
67+
switch (name) {
68+
case "longValue":
69+
longValue = jr.nextLong();
70+
break;
71+
case "intValue":
72+
intValue = jr.nextInt();
73+
break;
74+
case "booleanValue":
75+
booleanValue = jr.nextBoolean();
76+
break;
77+
case "stringValue":
78+
stringValue = jr.nextString();
79+
break;
80+
default:
81+
throw new IOException("Unexpected name: " + name);
7782
}
7883
}
7984
jr.endObject();

metrics/src/main/java/com/google/gson/metrics/CollectionsDeserializationBenchmark.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,21 @@ public void timeCollectionsStreaming(int reps) throws IOException {
7676
String stringValue = null;
7777
while (jr.hasNext()) {
7878
String name = jr.nextName();
79-
if (name.equals("longValue")) {
80-
longValue = jr.nextLong();
81-
} else if (name.equals("intValue")) {
82-
intValue = jr.nextInt();
83-
} else if (name.equals("booleanValue")) {
84-
booleanValue = jr.nextBoolean();
85-
} else if (name.equals("stringValue")) {
86-
stringValue = jr.nextString();
87-
} else {
88-
throw new IOException("Unexpected name: " + name);
79+
switch (name) {
80+
case "longValue":
81+
longValue = jr.nextLong();
82+
break;
83+
case "intValue":
84+
intValue = jr.nextInt();
85+
break;
86+
case "booleanValue":
87+
booleanValue = jr.nextBoolean();
88+
break;
89+
case "stringValue":
90+
stringValue = jr.nextString();
91+
break;
92+
default:
93+
throw new IOException("Unexpected name: " + name);
8994
}
9095
}
9196
jr.endObject();

0 commit comments

Comments
 (0)