Class GPBCodec<S,T extends S,M extends com.google.protobuf.Message>
- java.lang.Object
-
- GPBCodec<S,T,M>
-
- Type Parameters:
S
- the least-specific type this codec will accept when encodingT
- the type of instances this codec will build when decodingM
- 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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description T
fromByteArray(byte[] data)
Given an encoded representation generated byPersistCodec.toByteArray(S)
, decode and build an instance of T (always exactly this type, regardless of the actual type previously passed toPersistCodec.toByteArray(S)
)abstract T
fromMessage(M message)
Given a protobuf message returned fromPersistCodec.toMessage(S)
, decode and build an instance of T.byte[]
toByteArray(S data)
Given an instance of S, encode it to a byte-array representation using the encoding rules associated with this codec.abstract M
toMessage(S data)
Given an instance of S, encode it as a protobuf message.
-
-
-
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 interfacePersistCodec<S,T extends S,M extends com.google.protobuf.Message>
- Parameters:
data
- the instance to encode- Returns:
- an encoded form, or
null
ifdata
wasnull
- Throws:
EncodeException
- if the data could not be encoded
-
fromByteArray
public final T fromByteArray(byte[] data) throws DecodeException
Description copied from interface:PersistCodec
Given an encoded representation generated byPersistCodec.toByteArray(S)
, decode and build an instance of T (always exactly this type, regardless of the actual type previously passed toPersistCodec.toByteArray(S)
)- Specified by:
fromByteArray
in interfacePersistCodec<S,T extends S,M extends com.google.protobuf.Message>
- Parameters:
data
- the encoded form to decode- Returns:
- a decoded instance of T, or
null
ifdata
wasnull
- Throws:
DecodeException
- if the data could not be decoded
-
toMessage
public abstract M toMessage(S data)
Description copied from interface:PersistCodec
Given an instance of S, encode it as a protobuf message.
-
fromMessage
public abstract T fromMessage(M message)
Description copied from interface:PersistCodec
Given a protobuf message returned fromPersistCodec.toMessage(S)
, decode and build an instance of T.- Specified by:
fromMessage
in interfacePersistCodec<S,T extends S,M extends com.google.protobuf.Message>
- Parameters:
message
- the message instance to decode, ornull
ifmessage
wasnull
- Returns:
- a decoded instance of T.
-
-