package de.davherrmann.efficiently.immutable;

import java.io.IOException;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class ImmutableTypeAdapterFactory implements TypeAdapterFactory
{
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type)
    {
        if (Immutable.class.isAssignableFrom(type.getRawType()))
        {
            return new TypeAdapter<T>()
            {
                @Override
                public void write(JsonWriter out, T value) throws IOException
                {
                    gson.getAdapter(Map.class).write(out, ((Immutable) value).values());
                }
                @Override
                public T read(JsonReader in) throws IOException
                {
                    return null;
                }
            };
        }

        if (Immutable.ImmutableNode.class.isAssignableFrom(type.getRawType()))
        {
            return new TypeAdapter<T>()
            {
                @Override
                public void write(JsonWriter out, T value) throws IOException
                {
                    gson.getAdapter(Map.class).write(out, ((Immutable.ImmutableNode) value).values());
                }

                @Override
                public T read(JsonReader in) throws IOException
                {
                    return null;
                }
            };
        }

        return null;
    }
}

@@ -490,551 +490,8 @@
   %7B%0A
-        if (Immutable.class.isAssignableFrom(type.getRawType()))%0A        %7B%0A            return new TypeAdapter%3CT%3E()%0A            %7B%0A                @Override%0A                public void write(JsonWriter out, T value) throws IOException%0A                %7B%0A                    gson.getAdapter(Map.class).write(out, ((Immutable) value).values());%0A                %7D%0A                @Override%0A                public T read(JsonReader in) throws IOException%0A                %7B%0A                    return null;%0A                %7D%0A            %7D;%0A        %7D%0A%0A
     

simplify TypeAdapterFactory