Skip to content

Commit

Permalink
#222 improve generics of struct decoder lambda-helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lorban committed Dec 8, 2016
1 parent 14f3b26 commit 27f0c0a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* @author Ludovic Orban
*/
public interface StructArrayEncoderFunction<T> {
public interface StructArrayEncoderFunction<T, P extends PrimitiveEncodingSupport<?>> {

void encode(PrimitiveEncodingSupport<PrimitiveEncodingSupport> encoder, T t);
void encode(P encoder, T t);

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public StructEncoder<P> byteBuffer(String name, ByteBuffer value) {
return this;
}

public StructEncoder<P> struct(String name, StructEncoderFunction function) {
public StructEncoder<P> struct(String name, StructEncoderFunction<StructEncoder<StructEncoder<P>>> function) {
StructField field = fieldSearcher.findField(name, StructField.class, null);
List<DataHolder> values = new ArrayList<DataHolder>();
data.add(new StructDataHolder(values, field.index()));
Expand Down Expand Up @@ -226,17 +226,17 @@ public StructArrayEncoder<StructEncoder<P>> structs(String name) {
return new StructArrayEncoder<StructEncoder<P>>(values, this, ((StructField) field.subField()));
}

public <T> StructEncoder<P> structs(String name, T[] array, StructArrayEncoderFunction<T> function) {
public <T> StructEncoder<P> structs(String name, T[] array, StructArrayEncoderFunction<T, StructArrayEncoder<StructEncoder<P>>> function) {
return structs(name, Arrays.asList(array), function);
}

public <T> StructEncoder<P> structs(String name, Iterable<T> iterable, StructArrayEncoderFunction<T> function) {
public <T> StructEncoder<P> structs(String name, Iterable<T> iterable, StructArrayEncoderFunction<T, StructArrayEncoder<StructEncoder<P>>> function) {
final ArrayField field = fieldSearcher.findField(name, ArrayField.class, StructField.class);
List<StructDataHolder> values = new ArrayList<StructDataHolder>();
data.add(new ArrayDataHolder(values, field.index()));
StructArrayEncoder<StructEncoder<P>> subStructArrayEncoder = new StructArrayEncoder<StructEncoder<P>>(values, this, ((StructField) field.subField()));
for (T t : iterable) {
function.encode((PrimitiveEncodingSupport) subStructArrayEncoder, t);
function.encode(subStructArrayEncoder, t);
subStructArrayEncoder.next();
}
subStructArrayEncoder.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* @author Ludovic Orban
*/
public interface StructEncoderFunction {
public interface StructEncoderFunction<P extends PrimitiveEncodingSupport<?>> {

void encode(StructEncoder encoder);
void encode(P encoder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import org.junit.Test;
import org.terracotta.runnel.decoding.StructArrayDecoder;
import org.terracotta.runnel.decoding.StructDecoder;
import org.terracotta.runnel.encoding.PrimitiveEncodingSupport;
import org.terracotta.runnel.encoding.StructArrayEncoder;
import org.terracotta.runnel.encoding.StructArrayEncoderFunction;
import org.terracotta.runnel.encoding.StructEncoder;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand Down Expand Up @@ -115,9 +116,9 @@ public void testReadAll_withLambda() throws Exception {

ByteBuffer bb = struct.encoder()
.string("name", "joe")
.structs("mapEntry", stuff.entrySet(), new StructArrayEncoderFunction<Map.Entry<String, String>>() {
.structs("mapEntry", stuff.entrySet(), new StructArrayEncoderFunction<Map.Entry<String, String>, StructArrayEncoder<StructEncoder<Void>>>() {
@Override
public void encode(PrimitiveEncodingSupport<PrimitiveEncodingSupport> encoder, Map.Entry<String, String> entry) {
public void encode(StructArrayEncoder<StructEncoder<Void>> encoder, Map.Entry<String, String> entry) {
encoder
.string("key", entry.getKey())
.enm("type", Type.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public void testReadAll() throws Exception {
public void testReadAll_withLambda() throws Exception {
ByteBuffer bb = struct.encoder()
.string("name", "joe")
.struct("mapEntry", new StructEncoderFunction() {
.struct("mapEntry", new StructEncoderFunction<StructEncoder<StructEncoder<Void>>>() {
@Override
public void encode(StructEncoder encoder) {
public void encode(StructEncoder<StructEncoder<Void>> encoder) {
encoder.string("key", "1")
.string("value", "one");
}
Expand Down

0 comments on commit 27f0c0a

Please sign in to comment.