use of org.terasology.reflection.reflect.CollectionCopyConstructor in project Terasology by MovingBlocks.
the class CollectionTypeHandlerFactory method create.
@Override
public <T> Optional<TypeHandler<T>> create(TypeInfo<T> typeInfo, TypeHandlerContext context) {
Class<? super T> rawType = typeInfo.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return Optional.empty();
}
Type elementType = ReflectionUtil.getTypeParameterForSuper(typeInfo.getType(), Collection.class, 0);
if (elementType == null) {
LOGGER.error("Collection is not parameterized and cannot be serialized");
return Optional.empty();
}
TypeInfo<?> elementTypeInfo = TypeInfo.of(elementType);
Optional<TypeHandler<?>> declaredElementTypeHandler = context.getTypeHandlerLibrary().getTypeHandler(elementType);
@SuppressWarnings("unchecked") TypeHandler<?> elementTypeHandler = new RuntimeDelegatingTypeHandler(declaredElementTypeHandler.orElse(null), elementTypeInfo, context);
CollectionCopyConstructor constructor = ConstructorLibrary.getCollectionCopyConstructor((TypeInfo) typeInfo);
@SuppressWarnings("unchecked") TypeHandler<T> typeHandler = new CollectionTypeHandler(elementTypeHandler, constructor);
return Optional.of(typeHandler);
}
use of org.terasology.reflection.reflect.CollectionCopyConstructor in project Terasology by MovingBlocks.
the class CollectionTypeHandlerTest method testDeserialize.
@Test
void testDeserialize() {
IntTypeHandler elementTypeHandler = mock(IntTypeHandler.class);
CollectionCopyConstructor<Collection<Integer>, Integer> constructor = mock(CollectionCopyConstructor.class);
when(constructor.construct(Lists.newArrayList())).then((Answer<Collection<Integer>>) invocation -> Queues.newArrayDeque());
CollectionTypeHandler<Integer> typeHandler = new CollectionTypeHandler<>(elementTypeHandler, constructor);
TIntList intList = new TIntArrayList();
for (Integer i : Collections.nCopies(500, -1)) {
intList.add(i);
}
typeHandler.deserialize(new PersistedIntegerArray(intList));
verify(constructor).construct(Lists.newArrayList());
verify(elementTypeHandler, times(intList.size())).deserialize(any());
}
Aggregations