Java Enum#parseFrom

How the parse an enum from an constant?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("boxing")
public enum IndexStrategy {
DEFAULT(0), FASTINDEX(1), FINEGRAINED(2);
private final int value;
private IndexStrategy(int value) {
this.value = value;
}
public int getValue() {
return value;
}
private static final Map<Integer, IndexStrategy> intToEnum = new HashMap<Integer, IndexStrategy>();
static { // Initialize map from constant int to enum constant
for (IndexStrategy op : values())
intToEnum.put(op.getValue(), op);
}
// Returns Operation for int, or null if int is invalid
public static IndexStrategy fromInt(int value) {
return intToEnum.get(value);
}
}