/*
 * 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.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.persistence.typeHandling.TypeSerializationLibrary;

import java.util.HashMap;
import java.util.Map;

public class GsonTypeHandlerAdapterFactory implements TypeAdapterFactory {
    private Map<Class<?>, TypeHandler<?>> typeHandlerMap;

    public GsonTypeHandlerAdapterFactory() {
        typeHandlerMap = new HashMap<>();
    }

    public <T> void addTypeHandler(Class<T> type, TypeHandler<T> typeHandler) {
        typeHandlerMap.put(type, typeHandler);
    }

    public boolean containsTypeHandlerFor(Class<?> type) {
        return typeHandlerMap.containsKey(type);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        Class<? super T> rawType = type.getRawType();

        if (!containsTypeHandlerFor(rawType)) {
            return null;
        }

        return new GsonTypeHandlerAdapter<>((TypeHandler<T>) typeHandlerMap.get(rawType), gson, type);
    }
}

@@ -978,16 +978,182 @@
 l.Map;%0A%0A
+/**%0A * A Gson %7B@link TypeAdapterFactory%7D that creates a %7B@link GsonTypeHandlerAdapter%7D for each%0A * %7B@link TypeHandler%7D registered in the %7B@link #typeHandlerMap%7D.%0A */%0A
 public c
@@ -1380,137 +1380,537 @@
     
-public %3CT%3E void addTypeHandler(Class%3CT%3E type, TypeHandler%3CT%3E typeHandler) %7B%0A        typeHandlerMap.put(type, typeHandler);%0A    %7D%0A
+/**%0A     * Adds a %7B@link TypeHandler%7D to the %7B@link #typeHandlerMap%7D for the given type.%0A     * @param type The %7B@link Class%7D of the type.%0A     * @param typeHandler The %7B@link TypeHandler%7D for the type.%0A     */%0A    public %3CT%3E void addTypeHandler(Class%3CT%3E type, TypeHandler%3CT%3E typeHandler) %7B%0A        typeHandlerMap.put(type, typeHandler);%0A    %7D%0A%0A    /**%0A     * Returns a boolean stating whether the %7B@link #typeHandlerMap%7D contains a type handler for the given type.%0A     * @param type The %7B@link Class%7D of the given type.%0A     */
 %0A   

Document GsonTypeHandlerAdapterFactory methods