use of org.terasology.persistence.typeHandling.coreTypes.CollectionTypeHandler 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);
}
Aggregations