Skip to content
Merged
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 @@ -50,9 +50,16 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
}
};

private final Map<String, T> nameToConstant = new HashMap<>();
private final Map<String, T> stringToConstant = new HashMap<>();
private final Map<T, String> constantToName = new HashMap<>();
/**
* Taken from Java 19 method {@link HashMap.newHashMap}, using default load factor {@code 0.75F}.
*/
private static int calculateHashMapCapacity(int numMappings) {
return (int) Math.ceil(numMappings / 0.75F);
}

private final Map<String, T> nameToConstant;
private final Map<String, T> stringToConstant;
private final Map<T, String> constantToName;

private EnumTypeAdapter(Class<T> classOfT) {
try {
Expand All @@ -71,6 +78,12 @@ private EnumTypeAdapter(Class<T> classOfT) {
// one declared field which is not an enum constant, namely the implicit $VALUES array
fields = Arrays.copyOf(fields, constantCount);

int hashMapCapacity = calculateHashMapCapacity(constantCount);
nameToConstant = new HashMap<>(hashMapCapacity);
stringToConstant = new HashMap<>(hashMapCapacity);
// Don't use `EnumMap` here; that can break when using ProGuard or R8
constantToName = new HashMap<>(hashMapCapacity);

AccessibleObject.setAccessible(fields, true);

for (Field constantField : fields) {
Expand Down