Search in sources :

Example 1 with CollectionCopyConstructor

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);
}
Also used : CollectionTypeHandler(org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler) Type(java.lang.reflect.Type) CollectionCopyConstructor(org.terasology.reflection.reflect.CollectionCopyConstructor) Collection(java.util.Collection) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) CollectionTypeHandler(org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler) RuntimeDelegatingTypeHandler(org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler)

Example 2 with CollectionCopyConstructor

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Collection(java.util.Collection) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ArgumentMatcher(org.mockito.ArgumentMatcher) Answer(org.mockito.stubbing.Answer) Queues(com.google.common.collect.Queues) Lists(com.google.common.collect.Lists) PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) CollectionCopyConstructor(org.terasology.reflection.reflect.CollectionCopyConstructor) Queue(java.util.Queue) Collections(java.util.Collections) PersistedIntegerArray(org.terasology.persistence.typeHandling.inMemory.arrays.PersistedIntegerArray) Mockito.mock(org.mockito.Mockito.mock) Collection(java.util.Collection) TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) PersistedIntegerArray(org.terasology.persistence.typeHandling.inMemory.arrays.PersistedIntegerArray) Test(org.junit.jupiter.api.Test)

Aggregations

Collection (java.util.Collection)2 CollectionCopyConstructor (org.terasology.reflection.reflect.CollectionCopyConstructor)2 Lists (com.google.common.collect.Lists)1 Queues (com.google.common.collect.Queues)1 TIntList (gnu.trove.list.TIntList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1 Queue (java.util.Queue)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.times (org.mockito.Mockito.times)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1 Answer (org.mockito.stubbing.Answer)1 PersistedData (org.terasology.persistence.typeHandling.PersistedData)1 PersistedDataSerializer (org.terasology.persistence.typeHandling.PersistedDataSerializer)1