Interface EnumMapper<T extends Enum<T>>
-
- Type Parameters:
T- The enum type for which this mapper provides encoding/decoding logic.
public interface EnumMapper<T extends Enum<T>>An interface to implement custom serialization and deserialization of enum values used in network communication.This interface allows users to define a custom mapping between enum constants and their
byterepresentations sent over the wire, instead of relying on the default ordinal-based encoding. It can be used in conjunction with theorg.apache.ignite.internal.CustomMapperannotation to plug in user-defined mapping logic during generation of serialization code.Implementations must ensure that:
- Each enum constant maps to a unique
bytevalue. - The
bytecodes are stable and consistent across all nodes in the cluster. - The
decode(byte)method handles invalid or unknown codes appropriately (e.g., throws an exception or returns a default).
Example implementation:
public class MyColorMapper implements CustomMapper<Color> { public byte encode(Color color) { switch (color) { case null: return -1; case RED: return 0; case GREEN: return 1; case BLUE: return 2; default: throw new IllegalArgumentException("Unknown color: " + color); } } public Color decode(byte code) { switch (code) { case -1: return null; case 0: return Color.RED; case 1: return Color.GREEN; case 2: return Color.BLUE; default: throw new IllegalArgumentException("Unknown color code: " + code); } } }Note: This interface is used in Ignite's communication layer to enable evolution of enum types between different versions of the software.
- See Also:
CustomMapper,DefaultEnumMapper
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Tdecode(byte code)Decodes abytecode received from the network into the corresponding enum constant.byteencode(T val)Encodes given enum value into abyterepresentation to be sent over the network.
-
-
-
Method Detail
-
encode
byte encode(T val)
Encodes given enum value into abyterepresentation to be sent over the network.- Parameters:
val- The enum value to encode; may benulldepending on implementation contract.- Returns:
- The
byterepresentation of the enum value.
-
decode
T decode(byte code)
Decodes abytecode received from the network into the corresponding enum constant.- Parameters:
code- Thebyterepresentation of the enum value.- Returns:
- The decoded enum value.
- Throws:
IllegalArgumentException- if the code does not correspond to any valid enum constant.
-
-