Add GenericEnumConverter

This commit is contained in:
James Seibel
2022-06-30 20:37:00 -05:00
parent 50dc8efafc
commit 00118ea885
3 changed files with 58 additions and 4 deletions
@@ -3,7 +3,7 @@ package com.seibel.lod.core.api.external.apiObjects.wrapperInterfaces;
/**
* An interface for Distant Horizon's Config.
*
* @param <T>
* @param <T> The internal data type of this config.
* @author James Seibel
* @version 2022-6-13
*/
@@ -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<coreEnum extends Enum<coreEnum>, apiEnum extends Enum<apiEnum>> implements IConverter<coreEnum, apiEnum>
{
private final Class<coreEnum> apiCoreType;
private final Class<apiEnum> apiEnumClass;
public GenericEnumConverter(Class<coreEnum> newCoreEnumClass, Class<apiEnum> 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. <br>
* 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. <br> <br>
*
* Original source: https://stackoverflow.com/questions/25487619/java-generic-function-to-parse-enums-from-strings
*/
private static <E extends Enum<E>> E parseEnum(String str, Class<E> enumClass)
{
return Enum.valueOf(enumClass, str);
}
}
@@ -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 <T> The Enum type to parse
* @throws InvalidObjectException if no enum exists with the given enumName
*/
public static <T extends Enum<T>> T parseEnumIgnoreCase(Class<T> enumType, String enumName) throws InvalidObjectException
public static <T extends Enum<T>> T parseEnumIgnoreCase(String enumName, Class<T> 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. <Br><Br>
@@ -81,6 +83,8 @@ public class EnumUtil
return str.toString();
}
/** Returns true if both enums contain the same values. */
public static EnumComparisonResult compareEnumClassesByValues(Class<? extends Enum<?>> alphaEnum, Class<? extends Enum<?>> betaEnum)
{