implement asReader() for list types

This commit is contained in:
David Renshaw 2018-02-03 14:17:42 -05:00
parent d2cf14b6d7
commit f4411ba2c7
9 changed files with 179 additions and 7 deletions

View file

@ -245,6 +245,98 @@ class EncodingSuite extends FunSuite {
}
}
test("ListBuilderAsReader") {
val message = new MessageBuilder()
val allTypes = message.initRoot(TestAllTypes.factory)
allTypes.initVoidList(10)
allTypes.getVoidList().asReader().size() should equal (10)
val boolList = allTypes.initBoolList(7)
boolList.set(3, true)
val boolListReader = boolList.asReader()
boolListReader.size() should equal (7)
boolListReader.get(0) should equal (false)
boolListReader.get(1) should equal (false)
boolListReader.get(2) should equal (false)
boolListReader.get(3) should equal (true)
boolListReader.get(4) should equal (false)
val int8List = allTypes.initInt8List(9)
int8List.set(4, 100)
int8List.set(8, 11)
val int8ListReader = int8List.asReader()
int8ListReader.size() should equal (9)
int8ListReader.get(0) should equal (0)
int8ListReader.get(4) should equal (100)
int8ListReader.get(8) should equal (11)
val int16List = allTypes.initInt16List(2)
int16List.set(0, 1)
val int16ListReader = int16List.asReader()
int16ListReader.size() should equal (2)
int16ListReader.get(0) should equal (1)
int16ListReader.get(1) should equal (0)
// TODO other primitive lists
val textList = allTypes.initTextList(1)
textList.set(0, new Text.Reader("abcdefg"))
val textListReader = textList.asReader()
textListReader.size() should equal (1)
textListReader.get(0).toString() should equal ("abcdefg")
val dataList = allTypes.initDataList(1)
dataList.set(0, new Data.Reader(Array(1,2,3,4)))
val dataListReader = dataList.asReader()
dataListReader.size() should equal (1)
dataListReader.get(0).toArray() should equal (Array(1,2,3,4))
val structList = allTypes.initStructList(2)
structList.get(0).setInt8Field(5)
structList.get(1).setInt8Field(9)
val structListReader = structList.asReader(TestAllTypes.factory)
structListReader.size() should equal (2)
structListReader.get(0).getInt8Field() should equal (5)
structListReader.get(1).getInt8Field() should equal (9)
val enumList = allTypes.initEnumList(3)
enumList.set(0, TestEnum.FOO)
enumList.set(1, TestEnum.BAR)
enumList.set(2, TestEnum.BAZ)
val enumListReader = enumList.asReader()
enumListReader.size() should equal (3)
enumListReader.get(0) should equal (TestEnum.FOO)
enumListReader.get(1) should equal (TestEnum.BAR)
enumListReader.get(2) should equal (TestEnum.BAZ)
}
test("NestedListBuilderAsReader") {
val builder = new MessageBuilder()
val root = builder.initRoot(TestLists.factory)
val structListList = root.initStructListList(3)
val structList0 = structListList.init(0, 1)
structList0.get(0).setInt16Field(1)
// leave structList1 as default
val structList2 = structListList.init(2, 3)
structList2.get(0).setInt16Field(22)
structList2.get(1).setInt16Field(333)
structList2.get(2).setInt16Field(4444)
val structListListReader = structListList.asReader(new StructList.Factory(TestAllTypes.factory))
structListListReader.size() should equal (3)
val structList0Reader = structListListReader.get(0)
structList0Reader.size() should equal(1)
structList0Reader.get(0).getInt16Field() should equal (1)
structListListReader.get(1).size() should equal (0)
val structList2Reader = structListListReader.get(2)
structList2Reader.size() should equal (3)
structList2Reader.get(0).getInt16Field() should equal (22)
structList2Reader.get(1).getInt16Field() should equal (333)
structList2Reader.get(2).getInt16Field() should equal (4444)
}
test("Generics") {
val message = new MessageBuilder()
val factory = TestGenerics.newFactory(TestAllTypes.factory, Text.factory)
@ -344,7 +436,6 @@ class EncodingSuite extends FunSuite {
}
}
test("NestedLists") {
val builder = new MessageBuilder()
val root = builder.initRoot(TestLists.factory)

View file

@ -116,7 +116,6 @@ object TestUtil {
enumList.set(1, TestEnum.GARPLY)
}
def checkTestMessage(builder : TestAllTypes.Builder) {
builder.getVoidField()
assert(builder.getBoolField() == true)
@ -167,6 +166,7 @@ object TestUtil {
assert(builder.getVoidList().size() == 6)
val boolList = builder.getBoolList()
assert(boolList.size() == 4)
assert(boolList.get(0) == true)
assert(boolList.get(1) == false)
assert(boolList.get(2) == false)

View file

@ -87,7 +87,7 @@ public final class AnyPointer {
}
public final Reader asReader() {
return new Reader(segment, pointer, 0x7fffffff);
return new Reader(segment, pointer, java.lang.Integer.MAX_VALUE);
}
public final void clear() {

View file

@ -93,6 +93,12 @@ public final class DataList {
_setPointerElement(Data.factory, index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
public final class Iterator implements java.util.Iterator<Data.Builder> {
public Builder list;
public int idx = 0;

View file

@ -91,5 +91,12 @@ public class EnumList {
public void set(int index, T value) {
_setShortElement(index, (short)value.ordinal());
}
public final Reader<T> asReader() {
return new Reader(this.values,
this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}

View file

@ -64,7 +64,6 @@ public final class ListList {
public T get(int index) {
return _getPointerElement(this.factory, index);
}
}
public static final class Builder<T> extends ListBuilder {
@ -85,5 +84,13 @@ public final class ListList {
public final T get(int index) {
return _getPointerElement(this.factory, index);
}
// TODO: rework generics so that we don't need this factory parameter
public final <U extends ListReader> Reader<U> asReader(ListFactory<T, U> factor) {
return new Reader(factor,
this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}

View file

@ -64,6 +64,13 @@ public class PrimitiveList {
int structDataSize, short structPointerCount){
super(segment, ptr, elementCount, step, structDataSize, structPointerCount);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
@ -96,7 +103,7 @@ public class PrimitiveList {
super(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
}
public boolean get(int index) {
public final boolean get(int index) {
return _getBooleanElement(index);
}
}
@ -115,6 +122,12 @@ public class PrimitiveList {
public void set(int index, boolean value) {
_setBooleanElement(index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
@ -166,8 +179,13 @@ public class PrimitiveList {
public void set(int index, byte value) {
_setByteElement(index, value);
}
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
public static class Short {
@ -219,8 +237,13 @@ public class PrimitiveList {
public void set(int index, short value) {
_setShortElement(index, value);
}
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
public static class Int {
@ -272,6 +295,12 @@ public class PrimitiveList {
public void set(int index, int value) {
_setIntElement(index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
@ -323,6 +352,12 @@ public class PrimitiveList {
public void set(int index, float value) {
_setFloatElement(index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
@ -375,6 +410,12 @@ public class PrimitiveList {
public void set(int index, long value) {
_setLongElement(index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
@ -426,6 +467,12 @@ public class PrimitiveList {
public void set(int index, double value) {
_setDoubleElement(index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
}
}
}

View file

@ -130,6 +130,14 @@ public final class StructList {
return _getStructElement(factory, index);
}
// TODO: rework generics so that we don't need this factory parameter
public final <U extends StructReader> Reader<U> asReader(StructFactory<T, U> factory) {
return new Reader(factory,
this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
public final class Iterator implements java.util.Iterator<T> {
public Builder<T> list;
public int idx = 0;

View file

@ -93,6 +93,12 @@ public final class TextList {
_setPointerElement(Text.factory, index, value);
}
public final Reader asReader() {
return new Reader(this.segment, this.ptr, this.elementCount, this.step,
this.structDataSize, this.structPointerCount,
java.lang.Integer.MAX_VALUE);
}
public final class Iterator implements java.util.Iterator<Text.Builder> {
public Builder list;
public int idx = 0;