From 1241c64d3e16a41088a167425d7f404ec7c110d5 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Fri, 26 May 2023 18:32:13 +0200 Subject: [PATCH] ValueIterator returns the right type --- .../contiguous/ContiguousList.java | 40 ++++++++++++++----- .../contiguous/ContiguousListTest.java | 6 +-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java b/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java index c9c79db..0ab376a 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java @@ -214,14 +214,8 @@ public class ContiguousList extends NotImplementedList implements List *

* Because the values differ in type the output type is Object *

- * NB the actual type (right now) is the `raw` value: all integers are of type Long, BigInteger is String - * // That is unfortunate (or must I say: annoying!), but for something like JSON not a problem (I think). - * // So maybe keep this in (say 'rawValueIterator') and also create a typesafe iterator. - *

* It detects {@link ConcurrentModificationException} if the underlying list was updated while iterating. *

- * // I should probably include a type so that the caller could cast to the correct type - * // not sure yet * * @return an Iterator */ @@ -230,8 +224,7 @@ public class ContiguousList extends NotImplementedList implements List } /** - * Returns a list of the types that are in the data - * @return + * @return a list of types in the Object(graph). So the element type and all (nested) properties */ public List> getTypes() { final List> types = new ArrayList<>(); @@ -239,6 +232,7 @@ public class ContiguousList extends NotImplementedList implements List return types; } + private void getTypes(TypeHandler handler, List> types) { if (handler instanceof BuiltinTypeHandler) { types.add(handler.getType()); @@ -249,11 +243,30 @@ public class ContiguousList extends NotImplementedList implements List } } + List> getBuiltinTypeHandlers() { + final List> types = new ArrayList<>(); + getStoredTypes(rootHandler, types); + return types; + } + + private void getStoredTypes(TypeHandler handler, List> types) { + if (handler instanceof BuiltinTypeHandler) { + types.add((BuiltinTypeHandler) handler); + } else { + ((CompoundTypeHandler) handler).getProperties() + .forEach(propertyHandler -> getStoredTypes(propertyHandler, types)); + } + } + public class ValueIterator implements Iterator { private final int originalSize; private final int originalPosition; + private final List> typeHandlers; + private Iterator> typeHandlersIterator; ValueIterator() { + typeHandlers = getBuiltinTypeHandlers(); + typeHandlersIterator = typeHandlers.iterator(); this.originalSize = size; this.originalPosition = data.position(); data.position(0); @@ -273,7 +286,16 @@ public class ContiguousList extends NotImplementedList implements List * so that's why we first check for modifications (me and the computer) * * I could also maintain the position here. But you main want to fail ... dunno */ - return ValueReader.read(data); + Object rawValue = ValueReader.read(data); + + // transform (currently integers to the expected type) + BuiltinTypeHandler handler; + while (!typeHandlersIterator.hasNext()) { + typeHandlersIterator = typeHandlers.iterator(); + } + handler = typeHandlersIterator.next(); + + return handler.transform(rawValue); } } diff --git a/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java b/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java index 0ee3365..dbc7126 100644 --- a/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java +++ b/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java @@ -168,7 +168,7 @@ public class ContiguousListTest { } long prevValue = -1; for (Iterator intIter = integers.valueIterator(); intIter.hasNext(); ) { - long value = (long) intIter.next(); + int value = (int) intIter.next(); assertEquals(prevValue, value - 1); prevValue = value; } @@ -187,7 +187,7 @@ public class ContiguousListTest { } long prevValue = -1; for (Iterator intIter = integers.valueIterator(); intIter.hasNext(); ) { - long value = (long) intIter.next(); // here a value (IntBean.value) + int value = (int) intIter.next(); assertEquals(prevValue, value - 1); prevValue = value; } @@ -199,7 +199,7 @@ public class ContiguousListTest { } @Test - public void testGetStoredTypesWhenCompound() { + public void testGetTypesWhenCompound() { ContiguousList integers = new ContiguousList<>(NestedBean.class); Iterator> typeIterator = integers.getTypes().iterator(); assertTrue(typeIterator.hasNext());