Retrofit 2 with java 7 future

carvr3hs  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(138)

有没有办法在java 7 futures(而不是java 8 completablefutures)中使用reflective 2?
一些沿着

interface MyService {
  GET("somepath")
  Future<User> getUser()
}

字符串

798qvoo8

798qvoo81#

您应该能够创建一个类似于Java8CallAdapterFactory.java的适配器。它看起来像这样:

public class Java7CallAdapterFactory extends CallAdapter.Factory {

    public static Java7CallAdapterFactory create() {
        return new Java7CallAdapterFactory();
    }

    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        // TODO: Check type of returnType and innerType, null checks, etc ...
        Type responseType = getParameterUpperBound(0, (ParameterizedType) returnType);
        return new BodyCallAdapter<R>(responseType);
    }

    private static final class BodyCallAdapter<R> implements CallAdapter<R, Future<R>> {
        private final Type responseType;

        BodyCallAdapter(Type responseType) {
            this.responseType = responseType;
        }

        @Override public Type responseType() {
            return responseType;
        }

        @Override public Future<R> adapt(final Call<R> call) {
            final Java7Future<R> future = new Java7Future<>(call);

            call.enqueue(new Callback<R>() {
                @Override 
                public void onResponse(Call<R> call, Response<R> response) {
                    if (response.isSuccessful()) {
                        future.complete(response.body());
                    } // TODO: else
                }

                @Override 
                public void onFailure(Call<R> call, Throwable t) {
                    // TODO
                }
            });

            return future;
        }
    }

    private static class Java7Future<R> implements Future<R> {
        private Call<R> call;
        private boolean completed = false;
        private R value;

        public Java7Future(Call<R> call) {
            this.call = call;
        }

        public void complete(R result) {
            completed = true;
            value = result;
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            if (call != null) {
                call.cancel();
            }
            return false;
        }

        @Override
        public boolean isCancelled() {
            return call == null || call.isCanceled();
        }

        @Override
        public boolean isDone() {
            return completed;
        }

        @Override
        public R get() {
            if (isCancelled()) {
                throw new CancellationException();
            } else {
                // TODO: wait
                return value;
            }
        }

        @Override
        public R get(long timeout, @NonNull TimeUnit unit) {
            // TODO: wait
            return get();
        }
    }
}

字符串

相关问题