diff options
author | Walker Selby <git@walkerselby.com> | 2023-11-29 23:43:02 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-30 00:43:02 +0100 |
commit | 247d4d324dc9ce5960d8c94719707d232a89e3ad (patch) | |
tree | cbf6942c5875ed8fd8c24c54435aba5a514ce1b1 /src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt | |
parent | ed02a14a6d688ec9546d9ed06dddf412620bb246 (diff) | |
download | skyhanni-247d4d324dc9ce5960d8c94719707d232a89e3ad.tar.gz skyhanni-247d4d324dc9ce5960d8c94719707d232a89e3ad.tar.bz2 skyhanni-247d4d324dc9ce5960d8c94719707d232a89e3ad.zip |
Migrate Integer to Enums in Config #727
Co-authored-by: hannibal2 <24389977+hannibal002@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt new file mode 100644 index 000000000..99ed4b231 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/ConfigUtils.kt @@ -0,0 +1,63 @@ +package at.hannibal2.skyhanni.utils + +import at.hannibal2.skyhanni.config.HasLegacyId +import com.google.gson.JsonArray +import com.google.gson.JsonElement +import com.google.gson.JsonPrimitive + +object ConfigUtils { + + /** + * Migrates an Int ArrayList to an Enum ArrayList. + * The new enum class should implement HasLegacyId and have a getter for LegacyId + * + * @param element The JsonElement to migrate + * @param enumClass The enum class to migrate to + * @return The migrated JsonElement + */ + fun <T> migrateIntArrayListToEnumArrayList(element: JsonElement, enumClass: Class<T>): JsonElement + where T : Enum<T>, T : HasLegacyId { + require(element is JsonArray) { "Expected a JsonArray but got ${element.javaClass.simpleName}" } + + // An array of enum constants that are to be migrated + val migratedArray = element.mapNotNull { jsonElement -> + val index = jsonElement.asInt + getEnumConstantFromLegacyId(index, enumClass)?.name + }.map { JsonPrimitive(it) } + + // Return a JsonArray of the migrated enum constants + return JsonArray().apply { + migratedArray.forEach { add(it) } + } + } + + /** + * Gets an enum constant from a legacy id + * @param legacyId The legacy id to get the enum constant from + * @param enumClass The enum class to get the enum constant from + * @return The enum constant, or null if not found + */ + private fun <T> getEnumConstantFromLegacyId( + legacyId: Int, + enumClass: Class<T> + ): T? where T : Enum<T>, T : HasLegacyId { + for (enumConstant in enumClass.getEnumConstants()) { + if (enumConstant.legacyId == legacyId) return enumConstant + } + return null + } + + /** + * Migrates an Int to an Enum Constant. + * The new enum class should implement HasLegacyId and have a getter for LegacyId + * + * @param element The JsonElement to migrate + * @param enumClass The enum class to migrate to + * @return The migrated JsonElement + */ + fun <T> migrateIntToEnum(element: JsonElement, enumClass: Class<T>): JsonElement + where T : Enum<T>, T : HasLegacyId { + require(element is JsonPrimitive) { "Expected a JsonPrimitive but got ${element.javaClass.simpleName}" } + return JsonPrimitive(getEnumConstantFromLegacyId(element.asInt, enumClass)?.name) + } +} |