Skip to content

Improve BinarySerializers Class#329

Open
RomanPilyushin wants to merge 1 commit intoactivej:masterfrom
RomanPilyushin:Improve-BinarySerializer-Class
Open

Improve BinarySerializers Class#329
RomanPilyushin wants to merge 1 commit intoactivej:masterfrom
RomanPilyushin:Improve-BinarySerializer-Class

Conversation

@RomanPilyushin
Copy link
Copy Markdown

Changes Made:

  1. Introduced Helper Classes to Reduce Redundancy:

    • SimpleBinarySerializer<T>:

      • Created a generic helper class SimpleBinarySerializer<T> that implements BinarySerializer<T>.
      • This class accepts functional interfaces Encoder<T> and Decoder<T> for its encode and decode methods.
      • Replaced repetitive serializer implementations for primitive types and strings (BYTE_SERIALIZER, INT_SERIALIZER, LONG_SERIALIZER, FLOAT_SERIALIZER, DOUBLE_SERIALIZER, UTF8_SERIALIZER, ISO_88591_SERIALIZER) with instances of SimpleBinarySerializer.
      • This change centralizes common encoding and decoding logic, reducing code duplication and enhancing maintainability.
    • SimpleNullableBinarySerializer<T>:

      • Introduced a similar helper class SimpleNullableBinarySerializer<T> for handling nullable types.
      • Used in the ofNullable method for special cases where the codec is UTF8_SERIALIZER or ISO_88591_SERIALIZER.
      • Simplifies the handling of nullable serialization for these specific cases, reducing redundant code.
  2. Utilized Method References for Simplicity:

    • Employed method references (e.g., BinaryOutput::writeInt, BinaryInput::readInt) when creating instances of SimpleBinarySerializer and SimpleNullableBinarySerializer.
    • This approach makes the code more concise and readable by directly linking the serializer methods to existing methods in BinaryOutput and BinaryInput.
  3. Optimized the Enum Serializer (ofEnum Method):

    • Cached the array of enum constants (E[] constants = enumType.getEnumConstants()) outside of the decode method.
    • This prevents the need to retrieve the enum constants on each decode operation, enhancing performance.
    • Added a bounds check for the ordinal value read from the input stream. If the ordinal is out of bounds, a CorruptedDataException is thrown.
    • This change improves error handling and ensures data integrity during deserialization.
  4. Refactored the ofNullable Method:

    • Streamlined the ofNullable method by using SimpleNullableBinarySerializer for the special cases of UTF8_SERIALIZER and ISO_88591_SERIALIZER.
    • Eliminated the need for anonymous inner classes, reducing code complexity.
    • This refactoring decreases code duplication and enhances clarity in how nullable serializers are created.

Summary of Benefits:

  • Reduced Code Duplication: By introducing helper classes and method references, repetitive code is minimized.
  • Enhanced Maintainability: Centralized common logic makes future updates easier and reduces the risk of inconsistencies.
  • Improved Readability: The code is now more concise and easier to understand.
  • Optimized Performance: Caching enum constants and removing redundant operations improve the efficiency of serialization and deserialization processes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant