package sexy.code;

import java.io.IOException;
import java.util.concurrent.Executor;

public class Call<T> {

    private final ConverterProvider.ResponseConverter<T> converter;

    private final Request request;

    private final HttpEngine engine;

    Call(final HttpEngine httpEngine, final ConverterProvider.ResponseConverter<T> converter, final Request request) {
        this.engine = httpEngine;
        this.converter = converter;
        this.request = request;
    }

    public Response<T> execute() throws IOException {
        try {
            final RequestConvertTask<?> task = request.getPendingTask();
            if (task != null) {
                request.setBody(task.execute());
            }
            final HttpResponse httpResponse = engine.execute(request);
            T obj = null;
            if (httpResponse.isSuccess()) {
                obj = converter.convert(httpResponse.getBody());
            }
            return new Response<>(httpResponse, obj);
        } catch (RuntimeException e) {
            throw new IOException(e);
        }
    }

    public void enqueue(final Callback<T> callback) {
        final Executor callbackExecutor = engine.callbackExecutor();

        engine.httpExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    final Response<T> response = execute();
                    callbackExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            callback.onResponse(response);
                        }
                    });
                } catch (final Throwable t) {
                    callbackExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            callback.onFailure(t);
                        }
                    });
                }
            }
        });
    }
}

@@ -884,16 +884,19 @@
 nverter.
+%3CT%3E
 convert(
@@ -916,24 +916,24 @@
 getBody());%0A
-
             
@@ -1082,30 +1082,79 @@
 %0A    %7D%0A%0A    
-public
+@SuppressWarnings(%22unchecked%22)%0A    public %3CU extends T%3E
  void enqueu
@@ -1170,17 +1170,17 @@
 allback%3C
-T
+U
 %3E callba
@@ -1181,24 +1181,234 @@
 callback) %7B%0A
+        // this is a weird, imperfect solution to the woes of type erasure%0A        // the user will be able to pass in a Callback parametrized by a subclass which is wrong, but I haven't got a better solution%0A%0A
         fina
@@ -1464,17 +1464,16 @@
 utor();%0A
-%0A
         
@@ -1787,32 +1787,32 @@
 ic void run() %7B%0A
-
                 
@@ -1843,16 +1843,30 @@
 esponse(
+(Response%3CU%3E) 
 response

Add hack to combat type erasure