Class DefaultEnumMapper
- java.lang.Object
-
- org.apache.ignite.plugin.extensions.communication.mappers.DefaultEnumMapper
-
public final class DefaultEnumMapper extends Object
A default enum mapper that uses enum ordinal values to perform serialization and deserialization.This mapper encodes an enum constant into a
byteby taking itsEnum.ordinal(), and decodes abytevalue back into the corresponding enum constant using array indexing. The encoding returns-1fornullinputs, and decoding returnsnullfor negative codes. If a provided code is out of range (greater than or equal to the number of enum constants), anIllegalArgumentExceptionis thrown.This class assumes that:
- Enums have no more than 127 constants (to fit in a positive
byte). - The order of enum constants (i.e., their ordinals) is stable and not subject to change.
Example usage:
public enum Color { RED, GREEN, BLUE; } Color[] values = Color.values(); byte code = DefaultEnumMapper.INSTANCE.encode(Color.RED); // Returns 0 Color color = DefaultEnumMapper.INSTANCE.decode(values, code); // Returns Color.REDNote: This class is thread-safe and uses a singleton pattern. Use
INSTANCEto access the shared instance.- See Also:
Enum.ordinal()
- Enums have no more than 127 constants (to fit in a positive
-
-
Field Summary
Fields Modifier and Type Field Description static DefaultEnumMapperINSTANCE
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Enum<T>>
Tdecode(T[] vals, byte enumCode)Decodes abytecode into the corresponding enum constant.<T extends Enum<T>>
byteencode(T enumVal)Encodes the given enum value into abyteusing its ordinal.
-
-
-
Field Detail
-
INSTANCE
public static final DefaultEnumMapper INSTANCE
-
-
Method Detail
-
encode
public <T extends Enum<T>> byte encode(T enumVal)
Encodes the given enum value into abyteusing its ordinal.- Type Parameters:
T- The enum type.- Parameters:
enumVal- The enum value to encode; may benull.- Returns:
- The
byterepresentation of the enum value (non-negative) or-1if the value isnull.
-
decode
public <T extends Enum<T>> T decode(T[] vals, byte enumCode)
Decodes abytecode into the corresponding enum constant.- Type Parameters:
T- The enum type.- Parameters:
vals- Array of all possible values of the enum type. Must not benull.enumCode- Thebyterepresentation of the enum value.- Returns:
- The corresponding enum constant, or
nullifenumCodeis negative. - Throws:
IllegalArgumentException- ifenumCodeis out of range (i.e., greater than or equal to the length ofvalsarray).
-
-