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 TfromByteArray(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 TfromMessage(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 MtoMessage(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:PersistCodecGiven an instance of S, encode it to a byte-array representation using the encoding rules associated with this codec.- Specified by:
toByteArrayin interfacePersistCodec<S,T extends S,M extends com.google.protobuf.Message>- Parameters:
data- the instance to encode- Returns:
- an encoded form, or
nullifdatawasnull - Throws:
EncodeException- if the data could not be encoded
-
fromByteArray
public final T fromByteArray(byte[] data) throws DecodeException
Description copied from interface:PersistCodecGiven 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:
fromByteArrayin 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
nullifdatawasnull - Throws:
DecodeException- if the data could not be decoded
-
toMessage
public abstract M toMessage(S data)
Description copied from interface:PersistCodecGiven an instance of S, encode it as a protobuf message.
-
fromMessage
public abstract T fromMessage(M message)
Description copied from interface:PersistCodecGiven a protobuf message returned fromPersistCodec.toMessage(S), decode and build an instance of T.- Specified by:
fromMessagein interfacePersistCodec<S,T extends S,M extends com.google.protobuf.Message>- Parameters:
message- the message instance to decode, ornullifmessagewasnull- Returns:
- a decoded instance of T.
-
-