implement asReader() for list types
This commit is contained in:
parent
d2cf14b6d7
commit
f4411ba2c7
9 changed files with 179 additions and 7 deletions
|
@ -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") {
|
test("Generics") {
|
||||||
val message = new MessageBuilder()
|
val message = new MessageBuilder()
|
||||||
val factory = TestGenerics.newFactory(TestAllTypes.factory, Text.factory)
|
val factory = TestGenerics.newFactory(TestAllTypes.factory, Text.factory)
|
||||||
|
@ -344,7 +436,6 @@ class EncodingSuite extends FunSuite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
test("NestedLists") {
|
test("NestedLists") {
|
||||||
val builder = new MessageBuilder()
|
val builder = new MessageBuilder()
|
||||||
val root = builder.initRoot(TestLists.factory)
|
val root = builder.initRoot(TestLists.factory)
|
||||||
|
|
|
@ -116,7 +116,6 @@ object TestUtil {
|
||||||
enumList.set(1, TestEnum.GARPLY)
|
enumList.set(1, TestEnum.GARPLY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def checkTestMessage(builder : TestAllTypes.Builder) {
|
def checkTestMessage(builder : TestAllTypes.Builder) {
|
||||||
builder.getVoidField()
|
builder.getVoidField()
|
||||||
assert(builder.getBoolField() == true)
|
assert(builder.getBoolField() == true)
|
||||||
|
@ -167,6 +166,7 @@ object TestUtil {
|
||||||
assert(builder.getVoidList().size() == 6)
|
assert(builder.getVoidList().size() == 6)
|
||||||
|
|
||||||
val boolList = builder.getBoolList()
|
val boolList = builder.getBoolList()
|
||||||
|
assert(boolList.size() == 4)
|
||||||
assert(boolList.get(0) == true)
|
assert(boolList.get(0) == true)
|
||||||
assert(boolList.get(1) == false)
|
assert(boolList.get(1) == false)
|
||||||
assert(boolList.get(2) == false)
|
assert(boolList.get(2) == false)
|
||||||
|
|
|
@ -87,7 +87,7 @@ public final class AnyPointer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Reader asReader() {
|
public final Reader asReader() {
|
||||||
return new Reader(segment, pointer, 0x7fffffff);
|
return new Reader(segment, pointer, java.lang.Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void clear() {
|
public final void clear() {
|
||||||
|
|
|
@ -93,6 +93,12 @@ public final class DataList {
|
||||||
_setPointerElement(Data.factory, index, value);
|
_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 final class Iterator implements java.util.Iterator<Data.Builder> {
|
||||||
public Builder list;
|
public Builder list;
|
||||||
public int idx = 0;
|
public int idx = 0;
|
||||||
|
|
|
@ -91,5 +91,12 @@ public class EnumList {
|
||||||
public void set(int index, T value) {
|
public void set(int index, T value) {
|
||||||
_setShortElement(index, (short)value.ordinal());
|
_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,6 @@ public final class ListList {
|
||||||
public T get(int index) {
|
public T get(int index) {
|
||||||
return _getPointerElement(this.factory, index);
|
return _getPointerElement(this.factory, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Builder<T> extends ListBuilder {
|
public static final class Builder<T> extends ListBuilder {
|
||||||
|
@ -85,5 +84,13 @@ public final class ListList {
|
||||||
public final T get(int index) {
|
public final T get(int index) {
|
||||||
return _getPointerElement(this.factory, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,13 @@ public class PrimitiveList {
|
||||||
int structDataSize, short structPointerCount){
|
int structDataSize, short structPointerCount){
|
||||||
super(segment, ptr, elementCount, step, structDataSize, 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);
|
super(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean get(int index) {
|
public final boolean get(int index) {
|
||||||
return _getBooleanElement(index);
|
return _getBooleanElement(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,6 +122,12 @@ public class PrimitiveList {
|
||||||
public void set(int index, boolean value) {
|
public void set(int index, boolean value) {
|
||||||
_setBooleanElement(index, 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) {
|
public void set(int index, byte value) {
|
||||||
_setByteElement(index, 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 {
|
public static class Short {
|
||||||
|
@ -219,8 +237,13 @@ public class PrimitiveList {
|
||||||
public void set(int index, short value) {
|
public void set(int index, short value) {
|
||||||
_setShortElement(index, 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 {
|
public static class Int {
|
||||||
|
@ -272,6 +295,12 @@ public class PrimitiveList {
|
||||||
public void set(int index, int value) {
|
public void set(int index, int value) {
|
||||||
_setIntElement(index, 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) {
|
public void set(int index, float value) {
|
||||||
_setFloatElement(index, 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) {
|
public void set(int index, long value) {
|
||||||
_setLongElement(index, 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) {
|
public void set(int index, double value) {
|
||||||
_setDoubleElement(index, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,6 +130,14 @@ public final class StructList {
|
||||||
return _getStructElement(factory, index);
|
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 final class Iterator implements java.util.Iterator<T> {
|
||||||
public Builder<T> list;
|
public Builder<T> list;
|
||||||
public int idx = 0;
|
public int idx = 0;
|
||||||
|
|
|
@ -93,6 +93,12 @@ public final class TextList {
|
||||||
_setPointerElement(Text.factory, index, value);
|
_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 final class Iterator implements java.util.Iterator<Text.Builder> {
|
||||||
public Builder list;
|
public Builder list;
|
||||||
public int idx = 0;
|
public int idx = 0;
|
||||||
|
|
Loading…
Reference in a new issue