added getTypes (useful?)
This commit is contained in:
parent
610ce0ce41
commit
578600c3ae
3 changed files with 95 additions and 2 deletions
|
|
@ -213,7 +213,7 @@ public class ContiguousList<E> extends NotImplementedList<E> implements List<E>
|
||||||
* bean property values in a fixed order for all elements.
|
* bean property values in a fixed order for all elements.
|
||||||
* <p>
|
* <p>
|
||||||
* Because the values differ in type the output type is Object
|
* Because the values differ in type the output type is Object
|
||||||
*
|
* <p>
|
||||||
* NB the actual type (right now) is the `raw` value: all integers are of type Long, BigInteger is String
|
* 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).
|
* // 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.
|
* // So maybe keep this in (say 'rawValueIterator') and also create a typesafe iterator.
|
||||||
|
|
@ -229,7 +229,27 @@ public class ContiguousList<E> extends NotImplementedList<E> implements List<E>
|
||||||
return new ValueIterator();
|
return new ValueIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
class ValueIterator implements Iterator<Object> {
|
/**
|
||||||
|
* Returns a list of the types that are in the data
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<Class<?>> getTypes() {
|
||||||
|
final List<Class<?>> types = new ArrayList<>();
|
||||||
|
getTypes(rootHandler, types);
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getTypes(TypeHandler handler, List<Class<?>> types) {
|
||||||
|
if (handler instanceof BuiltinTypeHandler<?>) {
|
||||||
|
types.add(handler.getType());
|
||||||
|
} else {
|
||||||
|
types.add(handler.getType());
|
||||||
|
((CompoundTypeHandler) handler).getProperties()
|
||||||
|
.forEach(propertyHandler -> getTypes(propertyHandler, types));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ValueIterator implements Iterator<Object> {
|
||||||
private final int originalSize;
|
private final int originalSize;
|
||||||
private final int originalPosition;
|
private final int originalPosition;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -197,4 +197,63 @@ public class ContiguousListTest {
|
||||||
|
|
||||||
assertEquals(new IntBean(100), integers.get(100)); // here an instance
|
assertEquals(new IntBean(100), integers.get(100)); // here an instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStoredTypesWhenCompound() {
|
||||||
|
ContiguousList<NestedBean> integers = new ContiguousList<>(NestedBean.class);
|
||||||
|
Iterator<Class<?>> typeIterator = integers.getTypes().iterator();
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(NestedBean.class, typeIterator.next());
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(StringBean.class, typeIterator.next());
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(String.class, typeIterator.next());
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(IntBean.class, typeIterator.next());
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(Integer.class, typeIterator.next());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStoredTypesDeepCompound() {
|
||||||
|
ContiguousList<DeepBean> beans = new ContiguousList<>(DeepBean.class);
|
||||||
|
Iterator<Class<?>> typeIterator = beans.getTypes().iterator();
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(DeepBean.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(NestedBean.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(StringBean.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(String.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(IntBean.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(Integer.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(Long.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(StringBean.class, typeIterator.next());
|
||||||
|
|
||||||
|
assertTrue(typeIterator.hasNext());
|
||||||
|
assertEquals(String.class, typeIterator.next());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTypeIteratorBuiltin() {
|
||||||
|
ContiguousList<Integer> integers = new ContiguousList<>(Integer.class);
|
||||||
|
Iterator<Class<?>> typeIterator = integers.getTypes().iterator();
|
||||||
|
if (typeIterator.hasNext()) {
|
||||||
|
assertEquals(Integer.class, typeIterator.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
lib/src/test/java/nl/sanderhautvast/contiguous/DeepBean.java
Normal file
14
lib/src/test/java/nl/sanderhautvast/contiguous/DeepBean.java
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package nl.sanderhautvast.contiguous;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class DeepBean {
|
||||||
|
private NestedBean nestedBean;
|
||||||
|
private Long ageOfMagrathea;
|
||||||
|
private StringBean stringBean;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue