Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void generate(
/**
* JSON Codec for $modelClass model object. Generated based on protobuf schema.
*/
public final$staticModifier class $codecClass implements JsonCodec<$modelClass> {
public final$staticModifier class $codecClass extends JsonCodec<$modelClass> {

/**
* Empty constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void generate(
final String modelClassName = lookupHelper.getUnqualifiedClassForMessage(FileType.MODEL, msgDef);
final String schemaClassName = lookupHelper.getUnqualifiedClassForMessage(FileType.SCHEMA, msgDef);
final String codecClassName = lookupHelper.getUnqualifiedClassForMessage(FileType.CODEC, msgDef);
final String codecPackage = lookupHelper.getPackage(FileType.CODEC, msgDef);

final List<Field> fields = new ArrayList<>();
writer.addImport(lookupHelper.getPackage(FileType.MODEL, msgDef) + ".*");
Expand Down Expand Up @@ -91,7 +90,7 @@ public void generate(
/**
* Protobuf Codec for $modelClass model object. Generated based on protobuf schema.
*/
public final$staticModifier class $codecClass implements Codec<$modelClass> {
public final$staticModifier class $codecClass extends Codec<$modelClass> {
/**
* An initial capacity for the ArrayList where unknown fields are collected.
* To optimize parsing unknown fields, we store the max value we've seen so far.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*
* @param <T> The type of object to serialize and deserialize
*/
public interface Codec<T> {
@SuppressWarnings("unused")
public abstract class Codec<T> {

/**
* The default maximum size of a repeated or length-encoded field (Bytes, String, Message, etc.).
* The size should not be increased beyond the current limit because of the safety concerns.
* An application can override this limit when calling the `Codec.parse()` method for a specific
* protobuf model type if that model is allowed to contain larger fields.
*/
int DEFAULT_MAX_SIZE = 2 * 1024 * 1024;
public static final int DEFAULT_MAX_SIZE = 2 * 1024 * 1024;

/**
* The default maximum depth of nested messages before the `parse()` method would error out.
Expand All @@ -33,7 +34,7 @@ public interface Codec<T> {
* Applications can always override the maxDepth by supplying an argument to the main `Codec.parse()` method.
* The default depth should not be increased beyond the current limit because of the safety concerns.
*/
int DEFAULT_MAX_DEPTH = 512;
public static final int DEFAULT_MAX_DEPTH = 512;

/**
* Parses an object from the {@link ReadableSequentialData} and returns it.
Expand Down Expand Up @@ -66,7 +67,7 @@ public interface Codec<T> {
* @throws ParseException If parsing fails
*/
@NonNull
T parse(
public abstract T parse(
@NonNull ReadableSequentialData input,
boolean strictMode,
boolean parseUnknownFields,
Expand Down Expand Up @@ -97,7 +98,7 @@ T parse(
* @throws ParseException If parsing fails
*/
@NonNull
default T parse(@NonNull ReadableSequentialData input, boolean strictMode, boolean parseUnknownFields, int maxDepth)
public final T parse(@NonNull ReadableSequentialData input, boolean strictMode, boolean parseUnknownFields, int maxDepth)
throws ParseException {
return parse(input, strictMode, parseUnknownFields, maxDepth, DEFAULT_MAX_SIZE);
}
Expand All @@ -120,7 +121,7 @@ default T parse(@NonNull ReadableSequentialData input, boolean strictMode, boole
* @throws ParseException If parsing fails
*/
@NonNull
default T parse(@NonNull ReadableSequentialData input, final boolean strictMode, final int maxDepth)
public final T parse(@NonNull ReadableSequentialData input, final boolean strictMode, final int maxDepth)
throws ParseException {
return parse(input, strictMode, false, maxDepth);
}
Expand All @@ -144,7 +145,7 @@ default T parse(@NonNull ReadableSequentialData input, final boolean strictMode,
* @throws ParseException If parsing fails
*/
@NonNull
default T parse(@NonNull Bytes bytes, final boolean strictMode, final int maxDepth) throws ParseException {
public final T parse(@NonNull Bytes bytes, final boolean strictMode, final int maxDepth) throws ParseException {
return parse(bytes.toReadableSequentialData(), strictMode, maxDepth);
}

Expand All @@ -156,7 +157,7 @@ default T parse(@NonNull Bytes bytes, final boolean strictMode, final int maxDep
* @throws ParseException If parsing fails
*/
@NonNull
default T parse(@NonNull ReadableSequentialData input) throws ParseException {
public final T parse(@NonNull ReadableSequentialData input) throws ParseException {
return parse(input, false, DEFAULT_MAX_DEPTH);
}

Expand All @@ -168,7 +169,7 @@ default T parse(@NonNull ReadableSequentialData input) throws ParseException {
* @throws ParseException If parsing fails
*/
@NonNull
default T parse(@NonNull Bytes bytes) throws ParseException {
public final T parse(@NonNull Bytes bytes) throws ParseException {
return parse(bytes.toReadableSequentialData());
}

Expand All @@ -184,7 +185,7 @@ default T parse(@NonNull Bytes bytes) throws ParseException {
* @throws ParseException If parsing fails
*/
@NonNull
default T parseStrict(@NonNull ReadableSequentialData input) throws ParseException {
public final T parseStrict(@NonNull ReadableSequentialData input) throws ParseException {
return parse(input, true, DEFAULT_MAX_DEPTH);
}

Expand All @@ -200,7 +201,7 @@ default T parseStrict(@NonNull ReadableSequentialData input) throws ParseExcepti
* @throws ParseException If parsing fails
*/
@NonNull
default T parseStrict(@NonNull Bytes bytes) throws ParseException {
public final T parseStrict(@NonNull Bytes bytes) throws ParseException {
return parseStrict(bytes.toReadableSequentialData());
}

Expand All @@ -211,7 +212,7 @@ default T parseStrict(@NonNull Bytes bytes) throws ParseException {
* @param output The {@link WritableSequentialData} to write to.
* @throws IOException If the {@link WritableSequentialData} cannot be written to.
*/
void write(@NonNull T item, @NonNull WritableSequentialData output) throws IOException;
public abstract void write(@NonNull T item, @NonNull WritableSequentialData output) throws IOException;

/**
* Writes an item to the given byte array, this is a performance focused method. In non-performance centric use
Expand All @@ -224,7 +225,7 @@ default T parseStrict(@NonNull Bytes bytes) throws ParseException {
* @throws UncheckedIOException If the there is a problem writing to the output array.
* @throws IndexOutOfBoundsException If the output array is not large enough to hold the entire item.
*/
default int write(@NonNull T item, @NonNull byte[] output, final int startOffset) {
public int write(@NonNull T item, @NonNull byte[] output, final int startOffset) {
final BufferedData bufferedData = BufferedData.wrap(output, startOffset, output.length - startOffset);
try {
write(item, bufferedData);
Expand All @@ -243,15 +244,15 @@ default int write(@NonNull T item, @NonNull byte[] output, final int startOffset
* @return The length of the data item in the input
* @throws ParseException If parsing fails
*/
int measure(@NonNull ReadableSequentialData input) throws ParseException;
public abstract int measure(@NonNull ReadableSequentialData input) throws ParseException;

/**
* Compute number of bytes that would be written when calling {@code write()} method.
*
* @param item The input model data to measure write bytes for
* @return The length in bytes that would be written
*/
int measureRecord(T item);
public abstract int measureRecord(T item);

/**
* Compares the given item with the bytes in the input, and returns false if it determines that
Expand All @@ -265,7 +266,7 @@ default int write(@NonNull T item, @NonNull byte[] output, final int startOffset
* @return true if the bytes represent the item, false otherwise.
* @throws ParseException If parsing fails
*/
boolean fastEquals(@NonNull T item, @NonNull ReadableSequentialData input) throws ParseException;
public abstract boolean fastEquals(@NonNull T item, @NonNull ReadableSequentialData input) throws ParseException;

/**
* Converts a Record into a Bytes object
Expand All @@ -275,7 +276,7 @@ default int write(@NonNull T item, @NonNull byte[] output, final int startOffset
* @throws RuntimeException wrapping an IOException If it is impossible
* to write to the {@link WritableStreamingData}
*/
default Bytes toBytes(@NonNull T item) {
public final Bytes toBytes(@NonNull T item) {
// it is cheaper performance wise to measure the size of the object first than grow a buffer as needed
final byte[] bytes = new byte[measureRecord(item)];
final BufferedData bufferedData = BufferedData.wrap(bytes);
Expand All @@ -292,5 +293,5 @@ default Bytes toBytes(@NonNull T item) {
*
* @return The default value for the model class
*/
T getDefaultInstance();
public abstract T getDefaultInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*
* @param <T> The type of object to serialize and deserialize
*/
public interface JsonCodec<T> extends Codec<T> {
public abstract class JsonCodec<T> extends Codec<T> {

/** {@inheritDoc} */
default @NonNull T parse(
public @NonNull T parse(
@NonNull ReadableSequentialData input,
final boolean strictMode,
final boolean parseUnknownFields,
Expand Down Expand Up @@ -51,7 +51,7 @@ public interface JsonCodec<T> extends Codec<T> {
* @throws ParseException If parsing fails
*/
@NonNull
T parse(@Nullable final JSONParser.ObjContext root, final boolean strictMode, final int maxDepth, final int maxSize)
public abstract T parse(@Nullable final JSONParser.ObjContext root, final boolean strictMode, final int maxDepth, final int maxSize)
throws ParseException;

/**
Expand All @@ -61,7 +61,7 @@ T parse(@Nullable final JSONParser.ObjContext root, final boolean strictMode, fi
* @param output The {@link WritableSequentialData} to write to.
* @throws IOException If the {@link WritableSequentialData} cannot be written to.
*/
default void write(@NonNull T item, @NonNull WritableSequentialData output) throws IOException {
public void write(@NonNull T item, @NonNull WritableSequentialData output) throws IOException {
output.writeUTF8(toJSON(item));
}

Expand All @@ -70,7 +70,7 @@ default void write(@NonNull T item, @NonNull WritableSequentialData output) thro
*
* @param item The item to convert. Must not be null.
*/
default String toJSON(@NonNull T item) {
public String toJSON(@NonNull T item) {
return toJSON(item, "", false);
}

Expand All @@ -82,7 +82,7 @@ default String toJSON(@NonNull T item) {
* @param inline When true the output will start with indent end with a new line otherwise
* it will just be the object "{...}"
*/
String toJSON(@NonNull T item, String indent, boolean inline);
public abstract String toJSON(@NonNull T item, String indent, boolean inline);

/**
* Reads from this data input the length of the data within the input. The implementation may
Expand All @@ -95,7 +95,7 @@ default String toJSON(@NonNull T item) {
* @return The length of the data item in the input
* @throws ParseException If parsing fails
*/
default int measure(@NonNull ReadableSequentialData input) throws ParseException {
public int measure(@NonNull ReadableSequentialData input) throws ParseException {
final long startPosition = input.position();
parse(input);
return (int) (input.position() - startPosition);
Expand All @@ -109,7 +109,7 @@ default int measure(@NonNull ReadableSequentialData input) throws ParseException
* @param item The input model data to measure write bytes for
* @return The length in bytes that would be written
*/
default int measureRecord(T item) {
public int measureRecord(T item) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
WritableStreamingData out = new WritableStreamingData(bout);
try {
Expand All @@ -134,12 +134,17 @@ default int measureRecord(T item) {
* @return true if the bytes represent the item, false otherwise.
* @throws ParseException If parsing fails
*/
default boolean fastEquals(@NonNull T item, @NonNull ReadableSequentialData input) throws ParseException {
public boolean fastEquals(@NonNull T item, @NonNull ReadableSequentialData input) throws ParseException {
return Objects.equals(item, parse(input));
}

/**
* Get the default value for the model class.
*
* @return The default value for the model class
*/
@Override
default T getDefaultInstance() {
public T getDefaultInstance() {
return null;
}
}
Loading
Loading