samedi 10 octobre 2020

How to return a List inside this bounded generic method?

I have class Response

public class Response<T extends ResponseData> {
    private final T data;
    //other fields, getters, setters ...
}

and the empty interface ResponseData:

public interface ResponseData {
}

It takes any object as data but this object must implement the "empty" interface which I created just to force all classes returned inside "data" element be of same super type

Now the problem is that I need to return a List inside the data element, but:

  1. I don't find it clean to create a ResponseData implementation which serves only as a wrapper around my List

  2. I can't return the List directly because it doesn't belong to my code and therefore I can't make it implement the marker interface (ResponseData)

So is there a solution to my problem other than the one that says I should delete the marker interface and allow any object to be returned in the data element?

n.b. : My purpose of the marker interface is to make any created classes which will be returned inside the data element inside the response,for anyone who reads them, clear towards their purpose of existence

Aucun commentaire:

Enregistrer un commentaire