Skip to content

Commit

Permalink
Update Utils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed May 6, 2022
1 parent 80c9119 commit 4264ca0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/emu/grasscutter/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.nio.file.StandardCopyOption;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import emu.grasscutter.Config;
Expand Down Expand Up @@ -260,4 +262,41 @@ public static String readFromInputStream(InputStream stream) {
Grasscutter.getLogger().warn("Failed to read from input stream.");
} return stringBuilder.toString();
}

/**
* Switch properties from upper case to lower case?
*/
public static Map<String, Object> switchPropertiesUpperLowerCase(Map<String, Object> objMap, Class<?> cls) {
Map<String, Object> map = new HashMap<>(objMap.size());
for (String key : objMap.keySet()) {
try {
char c = key.charAt(0);
if (c >= 'a' && c <= 'z') {
try {
cls.getDeclaredField(key);
map.put(key, objMap.get(key));
} catch (NoSuchFieldException e) {
String s1 = String.valueOf(c).toUpperCase();
String after = key.length() > 1 ? s1 + key.substring(1) : s1;
cls.getDeclaredField(after);
map.put(after, objMap.get(key));
}
} else if (c >= 'A' && c <= 'Z') {
try {
cls.getDeclaredField(key);
map.put(key, objMap.get(key));
} catch (NoSuchFieldException e) {
String s1 = String.valueOf(c).toLowerCase();
String after = key.length() > 1 ? s1 + key.substring(1) : s1;
cls.getDeclaredField(after);
map.put(after, objMap.get(key));
}
}
} catch (NoSuchFieldException e) {
map.put(key, objMap.get(key));
}
}

return map;
}
}

0 comments on commit 4264ca0

Please sign in to comment.