Class VersionedSerializer.MultiType<BaseType>

  • Type Parameters:
    BaseType - The base type that all other types will derive from.
    Enclosing class:
    VersionedSerializer<T>

    public abstract static class VersionedSerializer.MultiType<BaseType>
    extends VersionedSerializer<BaseType>
    A VersionedDeserializer that serializes deserializes objects instantiating different types that inherit from a single base type. This is a meta-serializer in itself, as it composes various other Single-Type serializers into it. This should be used in those cases when we have a base (maybe abstract) type and multiple types inheriting from it that need serialization. A Serializer needs to be implemented for each sub-type and registered into this instance. Currently only VersionedSerializer.WithBuilder sub-serializers are supported. Example:
     
     class BaseType { ... }
    
     class SubType1 extends BaseType {
         static class SubType1Builder implements ObjectBuilder<SubType1> { ... }
         static class SubType1Serializer extends VersionedSerializer.WithBuilder<SubType1, SubType1Builder> { ... }
     }
    
     class SubType11 extends SubType1 {
         static class SubType11Builder implements ObjectBuilder<SubType11> { ... }
         static class SubType11Serializer extends VersionedSerializer.WithBuilder<SubType11, SubType11Builder> { ... }
     }
    
     class SubType2 extends BaseType {
         static class SubType2Builder implements ObjectBuilder<SubType2> { ... }
         static class SubType2Serializer extends VersionedSerializer.WithBuilder<SubType2, SubType2Builder> { ... }
     }
    
     class BaseTypeSerializer extends VersionedSerializer.MultiType<BaseType> {
        @Override
        protected void declareSerializers(Builder b) {
            // Declare sub-serializers here. IDs must be unique, non-changeable (during refactoring) and not necessarily
            // sequential or contiguous.
            b.serializer(SubType1.class, 0, new SubType1.SubType1Serializer())
             .serializer(SubType11.class, 10, new SubType11.SubType11Serializer())
             .serializer(SubType2.class, 1, new SubType2.SubType2Serializer());
        }
     }
     
    • Constructor Detail

      • MultiType

        public MultiType()
        Creates a new instance of the MultiType class.
    • Method Detail

      • declareSerializers

        protected abstract void declareSerializers​(VersionedSerializer.MultiType.Builder builder)
        When implemented in a derived class, this method will declare all supported serializers of subtypes of BaseType by using the serializer() method.
        Parameters:
        builder - A MultiType.Builder that can be used to declare serializers.
      • serialize

        public void serialize​(java.io.OutputStream stream,
                              BaseType o)
                       throws java.io.IOException
        Description copied from class: VersionedSerializer
        Serializes the given object to the given OutputStream.
        Specified by:
        serialize in class VersionedSerializer<BaseType>
        Parameters:
        stream - The OutputStream to serialize to.
        o - The object to serialize.
        Throws:
        java.io.IOException - If an IO Exception occurred.
      • deserialize

        public BaseType deserialize​(java.io.InputStream stream)
                             throws java.io.IOException
        Deserializes data from the given InputStream into an object of type BaseType. This will use one of the registered serializers to instantiate an instance of the correct type (derived from BaseType), as specified in declareSerializers().
        Parameters:
        stream - The InputStream to deserialize from.
        Returns:
        The deserialized instance.
        Throws:
        java.io.IOException - If an IO Exception occurred.
      • deserialize

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