Skip to content

Commit 6c73574

Browse files
committed
perf: align small enum with current impl
1 parent afcedec commit 6c73574

1 file changed

Lines changed: 101 additions & 41 deletions

File tree

perf/jmh/src/main/java/org/openapitools/perf/SmallEnumLookupJmhBenchmark.java

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.HashMap;
1414
import java.util.Locale;
1515
import java.util.Map;
16-
import java.util.Optional;
1716
import java.util.Random;
1817
import java.util.concurrent.TimeUnit;
1918
import java.util.function.Function;
@@ -66,13 +65,19 @@ public String getValue() {
6665
}
6766

6867
public static Tiny1 fromValue(String value) {
69-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
70-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
68+
Tiny1 result = BY_VALUE_CS.get(value);
69+
if (result != null) {
70+
return result;
71+
}
72+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
7173
}
7274

7375
public static Tiny1 fromValueCaseInsensitive(String value) {
74-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
75-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
76+
Tiny1 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
77+
if (result != null) {
78+
return result;
79+
}
80+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
7681
}
7782

7883
public static Tiny1 fromValueLinear(String value) {
@@ -124,13 +129,19 @@ public String getValue() {
124129
}
125130

126131
public static Tiny2 fromValue(String value) {
127-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
128-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
132+
Tiny2 result = BY_VALUE_CS.get(value);
133+
if (result != null) {
134+
return result;
135+
}
136+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
129137
}
130138

131139
public static Tiny2 fromValueCaseInsensitive(String value) {
132-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
133-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
140+
Tiny2 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
141+
if (result != null) {
142+
return result;
143+
}
144+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
134145
}
135146

136147
public static Tiny2 fromValueLinear(String value) {
@@ -183,13 +194,19 @@ public String getValue() {
183194
}
184195

185196
public static Tiny3 fromValue(String value) {
186-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
187-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
197+
Tiny3 result = BY_VALUE_CS.get(value);
198+
if (result != null) {
199+
return result;
200+
}
201+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
188202
}
189203

190204
public static Tiny3 fromValueCaseInsensitive(String value) {
191-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
192-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
205+
Tiny3 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
206+
if (result != null) {
207+
return result;
208+
}
209+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
193210
}
194211

195212
public static Tiny3 fromValueLinear(String value) {
@@ -243,13 +260,19 @@ public String getValue() {
243260
}
244261

245262
public static Tiny4 fromValue(String value) {
246-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
247-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
263+
Tiny4 result = BY_VALUE_CS.get(value);
264+
if (result != null) {
265+
return result;
266+
}
267+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
248268
}
249269

250270
public static Tiny4 fromValueCaseInsensitive(String value) {
251-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
252-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
271+
Tiny4 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
272+
if (result != null) {
273+
return result;
274+
}
275+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
253276
}
254277

255278
public static Tiny4 fromValueLinear(String value) {
@@ -304,13 +327,19 @@ public String getValue() {
304327
}
305328

306329
public static Tiny5 fromValue(String value) {
307-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
308-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
330+
Tiny5 result = BY_VALUE_CS.get(value);
331+
if (result != null) {
332+
return result;
333+
}
334+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
309335
}
310336

311337
public static Tiny5 fromValueCaseInsensitive(String value) {
312-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
313-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
338+
Tiny5 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
339+
if (result != null) {
340+
return result;
341+
}
342+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
314343
}
315344

316345
public static Tiny5 fromValueLinear(String value) {
@@ -366,13 +395,19 @@ public String getValue() {
366395
}
367396

368397
public static Tiny6 fromValue(String value) {
369-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
370-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
398+
Tiny6 result = BY_VALUE_CS.get(value);
399+
if (result != null) {
400+
return result;
401+
}
402+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
371403
}
372404

373405
public static Tiny6 fromValueCaseInsensitive(String value) {
374-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
375-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
406+
Tiny6 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
407+
if (result != null) {
408+
return result;
409+
}
410+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
376411
}
377412

378413
public static Tiny6 fromValueLinear(String value) {
@@ -429,13 +464,19 @@ public String getValue() {
429464
}
430465

431466
public static Tiny7 fromValue(String value) {
432-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
433-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
467+
Tiny7 result = BY_VALUE_CS.get(value);
468+
if (result != null) {
469+
return result;
470+
}
471+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
434472
}
435473

436474
public static Tiny7 fromValueCaseInsensitive(String value) {
437-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
438-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
475+
Tiny7 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
476+
if (result != null) {
477+
return result;
478+
}
479+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
439480
}
440481

441482
public static Tiny7 fromValueLinear(String value) {
@@ -493,13 +534,19 @@ public String getValue() {
493534
}
494535

495536
public static Tiny8 fromValue(String value) {
496-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
497-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
537+
Tiny8 result = BY_VALUE_CS.get(value);
538+
if (result != null) {
539+
return result;
540+
}
541+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
498542
}
499543

500544
public static Tiny8 fromValueCaseInsensitive(String value) {
501-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
502-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
545+
Tiny8 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
546+
if (result != null) {
547+
return result;
548+
}
549+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
503550
}
504551

505552
public static Tiny8 fromValueLinear(String value) {
@@ -558,13 +605,19 @@ public String getValue() {
558605
}
559606

560607
public static Tiny9 fromValue(String value) {
561-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
562-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
608+
Tiny9 result = BY_VALUE_CS.get(value);
609+
if (result != null) {
610+
return result;
611+
}
612+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
563613
}
564614

565615
public static Tiny9 fromValueCaseInsensitive(String value) {
566-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
567-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
616+
Tiny9 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
617+
if (result != null) {
618+
return result;
619+
}
620+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
568621
}
569622

570623
public static Tiny9 fromValueLinear(String value) {
@@ -624,13 +677,19 @@ public String getValue() {
624677
}
625678

626679
public static Tiny10 fromValue(String value) {
627-
return Optional.ofNullable(value).map(v -> BY_VALUE_CS.get(v))
628-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
680+
Tiny10 result = BY_VALUE_CS.get(value);
681+
if (result != null) {
682+
return result;
683+
}
684+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
629685
}
630686

631687
public static Tiny10 fromValueCaseInsensitive(String value) {
632-
return Optional.ofNullable(value).map(v -> BY_VALUE_CI.get(v.toLowerCase(Locale.ROOT)))
633-
.orElseThrow(() -> new IllegalArgumentException("Unexpected value '" + value + "'"));
688+
Tiny10 result = BY_VALUE_CI.get(value.toLowerCase(Locale.ROOT));
689+
if (result != null) {
690+
return result;
691+
}
692+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
634693
}
635694

636695
public static Tiny10 fromValueLinear(String value) {
@@ -696,6 +755,7 @@ private LookupFunctions(
696755
Function<String, Object> linear,
697756
Function<String, Object> linearCaseInsensitive) {
698757
this.values = values;
758+
// Methods
699759
this.hashMap = hashMap;
700760
this.hashMapCaseInsensitive = hashMapCaseInsensitive;
701761
this.linear = linear;

0 commit comments

Comments
 (0)