Class GPBCodec<S,​T extends S,​M extends com.google.protobuf.Message>

  • Type Parameters:
    S - the least-specific type this codec will accept when encoding
    T - the type of instances this codec will build when decoding
    M - the generated protobuf message class
    All Implemented Interfaces:
    PersistCodec<S,​T,​M>

    public abstract class GPBCodec<S,​T extends S,​M extends com.google.protobuf.Message>
    extends Object
    implements PersistCodec<S,​T,​M>
    The base PersistCodec implementation used by subclasses.
    • Constructor Detail

      • GPBCodec

        protected GPBCodec​(M instance)
        Subclasses use this constructor to provide a prototype message instance, indicating the message type they expect to handle. The prototype will be used to build messages when decoding from byte arrays.
        Parameters:
        instance - any instance of M (e.g. M.getDefaultInstance())
    • Method Detail

      • toByteArray

        public final byte[] toByteArray​(S data)
                                 throws EncodeException
        Description copied from interface: PersistCodec
        Given an instance of S, encode it to a byte-array representation using the encoding rules associated with this codec.
        Specified by:
        toByteArray in interface PersistCodec<S,​T extends S,​M extends com.google.protobuf.Message>
        Parameters:
        data - the instance to encode
        Returns:
        an encoded form, or null if data was null
        Throws:
        EncodeException - if the data could not be encoded
      • toMessage

        public abstract M toMessage​(S data)
        Description copied from interface: PersistCodec
        Given an instance of S, encode it as a protobuf message.
        Specified by:
        toMessage in interface PersistCodec<S,​T extends S,​M extends com.google.protobuf.Message>
        Parameters:
        data - the data object to encode
        Returns:
        a message instance representing the data object, or null if data was null
      • fromMessage

        public abstract T fromMessage​(M message)
        Description copied from interface: PersistCodec
        Given a protobuf message returned from PersistCodec.toMessage(S), decode and build an instance of T.
        Specified by:
        fromMessage in interface PersistCodec<S,​T extends S,​M extends com.google.protobuf.Message>
        Parameters:
        message - the message instance to decode, or null if message was null
        Returns:
        a decoded instance of T.