Package org.incenp.linkml.core
Class EnumConverter
- java.lang.Object
-
- org.incenp.linkml.core.ScalarConverterBase
-
- org.incenp.linkml.core.EnumConverter
-
- All Implemented Interfaces:
IConverter
public class EnumConverter extends ScalarConverterBase
An object to convert simple LinkML enums (excluding so-called “dynamic enums”, which are not yet supported).Similarly to the
ObjectConverter, there should be one instance of this class for every type of enum that needs to be converted.This converter relies on the enum type providing (1) a
fromStringmethod that can be used to deserialise an enum value, and (2) atoString()method that can be used to serialise the value, in such a way that, ifvis a value from the enumEnum, thenEnum.fromString(v.toString()) == vis true.The Java code generator in LinkML-Py automatically produces such enum types, if the
--true-enumsoption is used.
-
-
Constructor Summary
Constructors Constructor Description EnumConverter(Class<?> targetEnum)Creates a new converter for the specified type of enum.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ObjectconvertImpl(Object raw, ConverterContext ctx)Performs the actual type conversion.Class<?>getType()Gets the type of object that this converter can convert a raw object into.Objectserialise(Object object, ConverterContext ctx)Converts a LinkML object into a raw object.-
Methods inherited from class org.incenp.linkml.core.ScalarConverterBase
convert, convertForSlot, serialiseForSlot, toList
-
-
-
-
Constructor Detail
-
EnumConverter
public EnumConverter(Class<?> targetEnum) throws LinkMLRuntimeException
Creates a new converter for the specified type of enum.- Parameters:
targetEnum- The enum into which to convert raw objects.- Throws:
LinkMLRuntimeException- If the target type is not a valid enum type (either because it is not a Java enum to begin with, or because it does not provide the expectedfromStringmethod.
-
-
Method Detail
-
getType
public Class<?> getType()
Description copied from interface:IConverterGets the type of object that this converter can convert a raw object into.
-
convertImpl
protected Object convertImpl(Object raw, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from class:ScalarConverterBasePerforms the actual type conversion.- Specified by:
convertImplin classScalarConverterBase- Parameters:
raw- The raw object to convert. This is guaranteed to be a non-null, non-list, non-dictionary object.- Returns:
- The converted value.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given value.
-
serialise
public Object serialise(Object object, ConverterContext ctx) throws LinkMLRuntimeException
Description copied from interface:IConverterConverts a LinkML object into a raw object.- Specified by:
serialisein interfaceIConverter- Overrides:
serialisein classScalarConverterBase- Parameters:
object- The LinkML object to convert.ctx- The global converter context.- Returns:
- The raw object that represents the original LinkML object.
- Throws:
LinkMLRuntimeException- If the converter cannot convert the given object.
-
-