Skip to content

Commit c5b9a7b

Browse files
author
Adam Soos
committed
WS-2853: use map in enum forValue method
1 parent f1e31b7 commit c5b9a7b

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

model/src/main/java/com/basistech/rosette/apimodel/EventsNegationOption.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,35 @@
1616

1717
package com.basistech.rosette.apimodel;
1818

19-
import java.util.EnumSet;
19+
import java.util.Arrays;
20+
import java.util.Map;
21+
import java.util.function.Function;
22+
import java.util.stream.Collectors;
2023

2124
public enum EventsNegationOption {
2225
IGNORE("Ignore"),
2326
BOTH("Both"),
2427
ONLY_POSITIVE("Only positive"),
2528
ONLY_NEGATIVE("Only negative");
2629

30+
private static final Map<String, EventsNegationOption> STRING_KEYS;
31+
32+
static {
33+
STRING_KEYS = Arrays.stream(EventsNegationOption.values())
34+
.collect(Collectors.toMap(
35+
value -> value.toString().toLowerCase(),
36+
Function.identity()
37+
));
38+
}
39+
2740
private final String label;
2841

2942
EventsNegationOption(String label) {
3043
this.label = label;
3144
}
3245

3346
public static EventsNegationOption forValue(String value) {
34-
for (EventsNegationOption negationOption : EnumSet.allOf(EventsNegationOption.class)) {
35-
if (negationOption.toString().equalsIgnoreCase(value)) {
36-
return negationOption;
37-
}
38-
}
39-
throw new IllegalArgumentException("invalid events negation option: " + value);
47+
return STRING_KEYS.get(value.toLowerCase());
4048
}
4149

4250
@Override

0 commit comments

Comments
 (0)