From 00118ea885f79751d24a1d17fbfdb4132792b325 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 30 Jun 2022 20:37:00 -0500 Subject: [PATCH] Add GenericEnumConverter --- .../wrapperInterfaces/IDhApiConfig.java | 2 +- .../objects/GenericEnumConverter.java | 50 +++++++++++++++++++ .../com/seibel/lod/core/util/EnumUtil.java | 10 ++-- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/seibel/lod/core/api/implementation/objects/GenericEnumConverter.java diff --git a/src/main/java/com/seibel/lod/core/api/external/apiObjects/wrapperInterfaces/IDhApiConfig.java b/src/main/java/com/seibel/lod/core/api/external/apiObjects/wrapperInterfaces/IDhApiConfig.java index 62f52f037..b21406c44 100644 --- a/src/main/java/com/seibel/lod/core/api/external/apiObjects/wrapperInterfaces/IDhApiConfig.java +++ b/src/main/java/com/seibel/lod/core/api/external/apiObjects/wrapperInterfaces/IDhApiConfig.java @@ -3,7 +3,7 @@ package com.seibel.lod.core.api.external.apiObjects.wrapperInterfaces; /** * An interface for Distant Horizon's Config. * - * @param + * @param The internal data type of this config. * @author James Seibel * @version 2022-6-13 */ diff --git a/src/main/java/com/seibel/lod/core/api/implementation/objects/GenericEnumConverter.java b/src/main/java/com/seibel/lod/core/api/implementation/objects/GenericEnumConverter.java new file mode 100644 index 000000000..c227071f3 --- /dev/null +++ b/src/main/java/com/seibel/lod/core/api/implementation/objects/GenericEnumConverter.java @@ -0,0 +1,50 @@ +package com.seibel.lod.core.api.implementation.objects; + +import com.seibel.lod.core.api.implementation.interfaces.IConverter; + +/** + * This assumes the two enums contain the same values. + * + * @author James Seibel + * @version 2022-6-30 + */ +public class GenericEnumConverter, apiEnum extends Enum> implements IConverter +{ + private final Class apiCoreType; + private final Class apiEnumClass; + + + public GenericEnumConverter(Class newCoreEnumClass, Class newApiEnumClass) + { + this.apiCoreType = newCoreEnumClass; + this.apiEnumClass = newApiEnumClass; + } + + + @Override + public coreEnum convertToCoreType(apiEnum apiObject) + { + return parseEnum(apiObject.name(), this.apiCoreType); + } + + @Override + public apiEnum convertToApiType(coreEnum coreObject) + { + return parseEnum(coreObject.name(), this.apiEnumClass); + } + + + /** + * Since this does require string conversions it isn't the fastest option, + * however it works and should be fast enough for most use cases.
+ * If speed becomes an issue this can always be replaced with either individual + * converters for each enum (using a switch statement) or a more exotic solution.

+ * + * Original source: https://stackoverflow.com/questions/25487619/java-generic-function-to-parse-enums-from-strings + */ + private static > E parseEnum(String str, Class enumClass) + { + return Enum.valueOf(enumClass, str); + } + +} diff --git a/src/main/java/com/seibel/lod/core/util/EnumUtil.java b/src/main/java/com/seibel/lod/core/util/EnumUtil.java index 5716c2316..514ab6a62 100644 --- a/src/main/java/com/seibel/lod/core/util/EnumUtil.java +++ b/src/main/java/com/seibel/lod/core/util/EnumUtil.java @@ -25,7 +25,7 @@ import java.io.InvalidObjectException; * Methods related to handling and using enums. * * @author James Seibel - * @version 2022-6-6 + * @version 2022-6-30 */ public class EnumUtil { @@ -33,12 +33,12 @@ public class EnumUtil * Attempts to find the enum value with the given name * (ignoring case). * - * @param enumType The class of the enum of parse * @param enumName the string value to parse + * @param enumType The class of the enum of parse * @param The Enum type to parse * @throws InvalidObjectException if no enum exists with the given enumName */ - public static > T parseEnumIgnoreCase(Class enumType, String enumName) throws InvalidObjectException + public static > T parseEnumIgnoreCase(String enumName, Class enumType) throws InvalidObjectException { // attempt to find an enum with enumName for (T enumValue : enumType.getEnumConstants()) @@ -53,6 +53,8 @@ public class EnumUtil throw new InvalidObjectException("No Enum of type [" + enumType.getSimpleName() + "] exists with the name [" + enumName + "]. Possible enum values are: [" + createEnumCsv(enumType) + "]" ); } + + /** * Returns a comma separated list of all possible enum values * for the given enumType.

@@ -81,6 +83,8 @@ public class EnumUtil return str.toString(); } + + /** Returns true if both enums contain the same values. */ public static EnumComparisonResult compareEnumClassesByValues(Class> alphaEnum, Class> betaEnum) {