/*
 * Copyright 2018 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.persistence.typeHandling.coreTypes;

import org.terasology.persistence.typeHandling.DeserializationContext;
import org.terasology.persistence.typeHandling.PersistedData;
import org.terasology.persistence.typeHandling.SerializationContext;
import org.terasology.persistence.typeHandling.SimpleTypeHandler;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.reflection.reflect.ObjectConstructor;

import java.util.Collection;
import java.util.List;

public class CollectionTypeHandler<E> extends SimpleTypeHandler<Collection<E>> {
    private TypeHandler<E> elementTypeHandler;
    private ObjectConstructor<? extends Collection<E>> constructor;

    public CollectionTypeHandler(TypeHandler<E> elementTypeHandler, ObjectConstructor<? extends Collection<E>> constructor) {
        this.elementTypeHandler = elementTypeHandler;
        this.constructor = constructor;
    }

    @Override
    public PersistedData serialize(Collection<E> value, SerializationContext context) {
        if (value != null && value.size() > 0) {
            return elementTypeHandler.serializeCollection(value, context);
        }

        return context.createNull();
    }

    @Override
    public Collection<E> deserialize(PersistedData data, DeserializationContext context) {
        Collection<E> collection = constructor.construct();

        List<E> elements = elementTypeHandler.deserializeCollection(data, context);

        collection.addAll(elements);

        return collection;
    }
}

@@ -650,16 +650,56 @@
 Types;%0A%0A
+import com.google.common.collect.Lists;%0A
 import o
@@ -1670,45 +1670,89 @@
     
-if (value != null && value.size() %3E 0
+List%3CPersistedData%3E items = Lists.newArrayList();%0A%0A        for (E element : value
 ) %7B%0A
@@ -1763,23 +1763,26 @@
         
-return 
+items.add(
 elementT
@@ -1805,24 +1805,16 @@
 lize
-Collection(value
+(element
 , co
@@ -1819,16 +1819,17 @@
 context)
+)
 ;%0A      
@@ -1866,13 +1866,14 @@
 eate
-Null(
+(items
 );%0A 
@@ -2056,27 +2056,82 @@
     
-List%3CE%3E elements = 
+for (PersistedData item : data.getAsArray()) %7B%0A            collection.add(
 elem
@@ -2160,23 +2160,13 @@
 lize
-Collection(data
+(item
 , co
@@ -2171,18 +2171,18 @@
 context)
+)
 ;
-%0A
 %0A       
@@ -2186,36 +2186,9 @@
     
-collection.addAll(elements);
+%7D
 %0A%0A  

Make CollectionTypeHandler manually serialize collection