protected Response<T> parseNetworkResponse

is a method that returns a type of <T> , but I need <T> to be a List<T> or other kind of generic collection. This is because I am serializing objects from the network, and sometimes those objects become lists.

My objects are implementing required methods, so I can't just change the return type to List<T>; what can I do?

有帮助吗?

解决方案

Below is a sample, getErrors() and getWarnings() return List<T>

public interface ErrorHolder<T> {

    public void addError(T t);

    public int totalErrors();

    public boolean hasErrors();

    public void addWarning(T t);

    public int totalWarnings();

    public boolean hasWarnings();

    public List<T> getErrors();

    public List<T> getWarnings();
}

and in the implementing class -

private List<T> errors = new ArrayList<T>();

    public void addError(T t) {
        errors.add(t);

    }

public List<T> getErrors() {
        return errors;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top