Class VersionedSerializer.WithBuilder<TargetType,​ReaderType extends ObjectBuilder<TargetType>>

  • Type Parameters:
    TargetType - Type of the object to serialize from.
    ReaderType - Type of the Builder object, which must implement ObjectBuilder(of TargetType). This will be used for deserialization.
    Enclosing class:
    VersionedSerializer<T>

    public abstract static class VersionedSerializer.WithBuilder<TargetType,​ReaderType extends ObjectBuilder<TargetType>>
    extends VersionedSerializer<TargetType>
    A Single-Type VersionedDeserializer that deserializes into a "Builder" object. A Builder object is a shadow object that accumulates the deserialization changes and is able to create an object of TargetType when the build() method is invoked. This should be used in those cases when we do not have an instance of the target object available during deserialization, most likely because the object is immutable and needs to be created with its data. Example:
    
     class Attribute {
        private final Long value;
        private final Long lastUsed;
    
        // Attribute class is immutable; it has a builder that helps create new instances (this can be generated with Lombok).
        static class AttributeBuilder implements ObjectBuilder<Attribute> { ... }
     }
    
     class AttributeSerializer extends VersionedSerializer.WithBuilder<Attribute, Attribute.AttributeBuilder> {
        @Override
        protected byte getWriteVersion() { return 0; } // Version we're serializing at.
    
        @Override
        protected Attribute.AttributeBuilder newBuilder() { return Attribute.builder(); }
    
        @Override
        protected void declareVersions() {
            version(0).revision(0, this::write00, this::read00);
        }
    
        private void write00(Attribute source, RevisionDataOutput output) throws IOException { ... }
    
        private void read00(RevisionDataInput input, Attribute.AttributeBuilder target) throws IOException { ... }
     }
    
     
    • Constructor Detail

      • WithBuilder

        public WithBuilder()
    • Method Detail

      • newBuilder

        protected abstract ReaderType newBuilder()
        When implemented in a derived class, this method will return a new instance of the Builder each time it is invoked.
        Returns:
        A new instance of ReaderType.
      • deserialize

        public TargetType deserialize​(RevisionDataInput dataInput)
                               throws java.io.IOException
        Deserializes data from the given RevisionDataInput and creates a new object with the result. This overload is usually invoked for deserializing nested classes or collections.
        Parameters:
        dataInput - The RevisionDataInput to deserialize from.
        Returns:
        A new instance of TargetType with the deserialized data.
        Throws:
        java.io.IOException - If an IO Exception occurred.
      • deserialize

        public TargetType deserialize​(java.io.InputStream stream)
                               throws java.io.IOException
        Deserializes data from the given InputStream and creates a new object with the result.
        Parameters:
        stream - The InputStream to deserialize from.
        Returns:
        A new instance of TargetType with the deserialized data.
        Throws:
        java.io.IOException - If an IO Exception occurred.
      • deserialize

        public TargetType deserialize​(byte[] data)
                               throws java.io.IOException
        Deserializes data from the given byte array and creates a new object with the result.
        Parameters:
        data - The byte array to deserialize from.
        Returns:
        A new instance of TargetType with the deserialized data.
        Throws:
        java.io.IOException - If an IO Exception occurred.
      • deserialize

        public TargetType deserialize​(BufferView data)
                               throws java.io.IOException
        Deserializes data from the given ArrayView and creates a new object with the result.
        Parameters:
        data - The BufferView to deserialize from.
        Returns:
        A new instance of TargetType with the deserialized data.
        Throws:
        java.io.IOException - If an IO Exception occurred.