So I have a model Model
.
public class Model { .... }
This has two subclasses:
public class SubmodelA extend Model { .... }
and
public class SubmodelB extend Model { .... }
These three are wrapped under Data
class.
public class ApiData<T extends Model> {
public T data;
}
My general response wrapper
looks like this:
public class ApiResponse<DATA> {
DATA data;
}
The "dummy" api operation remains the same:
public interface Endpoints {
Call<ApiResponse<ApiData>> getData();
}
I have an implementation of retrofit2.Callback
to handle the responses:
public class ApiCallbackProxy<T> implements retrofit2.Callback<T> {
public interface ApiResultListener<RESPONSE_TYPE> {
void onResult(RESPONSE_TYPE response, ApiError error);
}
private ApiResultListener<T> mListener;
private ApiCallbackProxy(ApiResultListener<T> listener) {
mListener = listener;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
}
@Override
public void onFailure(Call<T> call, Throwable t) {
}
public static <T> ApiCallbackProxy<T> with(ApiResultListener<T> callback) {
return new ApiCallbackProxy<>(callback);
}
}
The ApiClient
public class ApiClient {
public Endpoints mRetrofit;
public ApiClient() {
Retrofit retrofit = new Retrofit.Builder().build();
mRetrofit = retrofit.create(Endpoints.class);
}
public <U extends Model> void getData(ApiResultListener<ApiResponse<ApiData<U>>> callback) {
//Compiler hits here
mRetrofit.getData().enqueue(ApiCallbackProxy.with(callback));
}
}
Compiler hits at ApiCallbackProxy.with(callback)
with this error:
So I want depending on where this API call is used in the app to return a different subclass of model or the model itself.
ie.
public static void main (String[] args) {
ApiClient apiClient = new ApiClient();
apiClient.getData(listener2);
}
public static final ApiResultListener<ApiResponse<Data<SubmodelA>>> listener = (response, error) -> {};
public static final ApiResultListener<ApiResponse<Data<Model>>> listener2 = (response, error) -> {};
public static final ApiResultListener<ApiResponse<Data<SubmodelB>>> listener3 = (response, error) -> {};