Skip to content

Commit 08c36bb

Browse files
IGNITE-26728 Add documentation for new interfaces and annotation types
1 parent 5b81d51 commit 08c36bb

File tree

4 files changed

+126
-32
lines changed

4 files changed

+126
-32
lines changed

modules/codegen2/src/main/java/org/apache/ignite/internal/CustomMapper.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,16 @@
2424

2525
/**
2626
* Annotation used to specify a custom mapping strategy for an enum type during code generation.
27-
* It allows associating a custom mapper class that defines how enum constants are serialized,
28-
* deserialized, or otherwise processed externally (e.g., to integer codes, strings, or other representations).
27+
* It allows associating a custom mapper class that defines how enum constants are serialized and deserialized.
2928
*
30-
* <p>The class specified by {@link #value()} must implement the appropriate mapping interface
31-
* expected by the code generation framework, typically providing methods to convert between
32-
* enum constants and their external representations.</p>
29+
* <p>It is used in conjunction with {@link Order} annotation and used to mark fields of enum type
30+
* that should be serialized and deserialized using custom logic.</p>
3331
*
34-
* <p>Example usage:</p>
35-
* <pre>{@code
36-
* @CustomEnumMapper(className = "com.example.MyCustomColorMapper")
37-
* public enum Color {
38-
* RED, GREEN, BLUE;
39-
* }
40-
* }</pre>
41-
*
42-
* <p>In this example, {@code MyCustomColorMapper} is responsible for defining the mapping logic
43-
* between the {@code Color} enum and its external form (e.g., integers or strings).</p>
32+
* <p>The class specified by {@link #value()} must implement {@code org.apache.ignite.plugin.extensions.communication.mappers.CustomMapper}
33+
* interface to provide methods for converting enum values to and from their external representations.</p>
4434
*
4535
* @see #value()
36+
* @see org.apache.ignite.internal.Order
4637
*/
4738
@Retention(RetentionPolicy.CLASS)
4839
@Target(ElementType.FIELD)

modules/core/src/main/java/org/apache/ignite/plugin/extensions/communication/mappers/CustomMapper.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,69 @@
1717

1818
package org.apache.ignite.plugin.extensions.communication.mappers;
1919

20-
/** */
20+
/**
21+
* An interface to implement custom serialization and deserialization of enum values used in network communication.
22+
*
23+
* <p>This interface allows users to define a custom mapping between enum constants and their {@code byte}
24+
* representations sent over the wire, instead of relying on the default ordinal-based encoding.
25+
* It can be used in conjunction with the {@code org.apache.ignite.internal.CustomMapper} annotation
26+
* to plug in user-defined mapping logic during generation of serialization code.</p>
27+
*
28+
* <p>Implementations must ensure that:
29+
* <ul>
30+
* <li>Each enum constant maps to a unique {@code byte} value.</li>
31+
* <li>The {@code byte} codes are stable and consistent across all nodes in the cluster.</li>
32+
* <li>The {@link #decode(byte)} method handles invalid or unknown codes appropriately (e.g., throws an exception or returns a default).</li>
33+
* </ul>
34+
* </p>
35+
*
36+
* <p>Example implementation:</p>
37+
* <pre>{@code
38+
* public class MyColorMapper implements CustomMapper<Color> {
39+
* public byte encode(Color color) {
40+
* switch (color) {
41+
* case null: return -1;
42+
* case RED: return 0;
43+
* case GREEN: return 1;
44+
* case BLUE: return 2;
45+
* default: throw new IllegalArgumentException("Unknown color: " + color);
46+
* }
47+
* }
48+
*
49+
* public Color decode(byte code) {
50+
* switch (code) {
51+
* case -1: return null;
52+
* case 0: return Color.RED;
53+
* case 1: return Color.GREEN;
54+
* case 2: return Color.BLUE;
55+
* default: throw new IllegalArgumentException("Unknown color code: " + code);
56+
* }
57+
* }
58+
* }
59+
* }</pre>
60+
*
61+
* <p><strong>Note:</strong> This interface is used in Ignite's communication layer
62+
* to enable evolution of enum types between different versions of the software.</p>
63+
*
64+
* @param <T> The enum type for which this mapper provides encoding/decoding logic.
65+
* @see org.apache.ignite.internal.CustomMapper
66+
* @see DefaultEnumMapper
67+
*/
2168
public interface CustomMapper<T extends Enum<T>> {
2269
/**
23-
* Encodes enum value into {@code byte} representation that will be sent over the network.
70+
* Encodes given enum value into a {@code byte} representation to be sent over the network.
2471
*
25-
* @param val Value to encode.
26-
* @return {@code byte} representation of the enum value.
72+
* @param val The enum value to encode; may be {@code null} depending on implementation contract.
73+
* @return The {@code byte} representation of the enum value.
2774
*/
2875
public byte encode(T val);
2976

3077
/**
31-
* Decodes enum value from {@code byte} representation received from the network.
78+
* Decodes a {@code byte} code received from the network into the corresponding enum constant.
3279
*
33-
* @param code {@code byte} representation of the enum value.
34-
* @return Decoded enum value.
80+
* @param code The {@code byte} representation of the enum value.
81+
* @return The decoded enum value.
82+
* @throws IllegalArgumentException if the code does not correspond to any valid enum constant.
3583
*/
3684
public T decode(byte code);
3785
}

modules/core/src/main/java/org/apache/ignite/plugin/extensions/communication/mappers/DefaultEnumMapper.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,38 @@
1717

1818
package org.apache.ignite.plugin.extensions.communication.mappers;
1919

20-
/** */
20+
/**
21+
* A default enum mapper that uses enum ordinal values to perform serialization and deserialization.
22+
*
23+
* <p>This mapper encodes an enum constant into a {@code byte} by taking its {@link Enum#ordinal()},
24+
* and decodes a {@code byte} value back into the corresponding enum constant using array indexing.
25+
* The encoding returns {@code -1} for {@code null} inputs, and decoding returns {@code null} for
26+
* negative codes. If a provided code is out of range (greater than or equal to the number of enum
27+
* constants), an {@link IllegalArgumentException} is thrown.</p>
28+
*
29+
* <p>This class assumes that:
30+
* <ul>
31+
* <li>Enums have no more than 127 constants (to fit in a positive {@code byte}).</li>
32+
* <li>The order of enum constants (i.e., their ordinals) is stable and not subject to change.</li>
33+
* </ul>
34+
* </p>
35+
*
36+
* <p>Example usage:</p>
37+
* <pre>{@code
38+
* public enum Color {
39+
* RED, GREEN, BLUE;
40+
* }
41+
*
42+
* Color[] values = Color.values();
43+
* byte code = DefaultEnumMapper.INSTANCE.encode(Color.RED); // Returns 0
44+
* Color color = DefaultEnumMapper.INSTANCE.decode(values, code); // Returns Color.RED
45+
* }</pre>
46+
*
47+
* <p><strong>Note:</strong> This class is thread-safe and uses a singleton pattern.
48+
* Use {@link #INSTANCE} to access the shared instance.</p>
49+
*
50+
* @see Enum#ordinal()
51+
*/
2152
public class DefaultEnumMapper {
2253
/** */
2354
public static final DefaultEnumMapper INSTANCE = new DefaultEnumMapper();
@@ -26,9 +57,11 @@ public class DefaultEnumMapper {
2657
private DefaultEnumMapper() {}
2758

2859
/**
29-
* @param <T> Enum type.
30-
* @param enumVal Enum value to encode.
31-
* @return {@code byte} representation of the enum value (non-negative) or {@code -1} if {@code null} value was provided.
60+
* Encodes the given enum value into a {@code byte} using its ordinal.
61+
*
62+
* @param <T> The enum type.
63+
* @param enumVal The enum value to encode; may be {@code null}.
64+
* @return The {@code byte} representation of the enum value (non-negative) or {@code -1} if the value is {@code null}.
3265
*/
3366
public <T extends Enum<T>> byte encode(T enumVal) {
3467
if (enumVal == null)
@@ -38,11 +71,14 @@ public <T extends Enum<T>> byte encode(T enumVal) {
3871
}
3972

4073
/**
41-
* @param <T> Enum type.
42-
* @param vals Array of all possible values of the enum type. Should not be null.
43-
* @param enumCode {@code byte} representation of enum value.
44-
* @return Enum value or {@code null} if negative enumCode was provided.
45-
* @throws IllegalArgumentException if enumCode is out of range.
74+
* Decodes a {@code byte} code into the corresponding enum constant.
75+
*
76+
* @param <T> The enum type.
77+
* @param vals Array of all possible values of the enum type. Must not be {@code null}.
78+
* @param enumCode The {@code byte} representation of the enum value.
79+
* @return The corresponding enum constant, or {@code null} if {@code enumCode} is negative.
80+
* @throws IllegalArgumentException if {@code enumCode} is out of range
81+
* (i.e., greater than or equal to the length of {@code vals} array).
4682
*/
4783
public <T extends Enum<T>> T decode(T[] vals, byte enumCode) {
4884
if (enumCode < 0)

modules/core/src/test/java/org/apache/ignite/internal/managers/communication/DefaultEnumMapperTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package org.apache.ignite.internal.managers.communication;
219

320
import org.apache.ignite.plugin.extensions.communication.mappers.DefaultEnumMapper;
@@ -8,7 +25,9 @@
825
import static org.junit.Assert.assertEquals;
926
import static org.junit.Assert.assertNull;
1027

11-
/** */
28+
/**
29+
* Verifies behavior of {@link DefaultEnumMapper} on a single enum type.
30+
*/
1231
public class DefaultEnumMapperTest {
1332
/** */
1433
private final TransactionIsolation[] txIsolationVals = TransactionIsolation.values();

0 commit comments

Comments
 (0)