java - 如何使用 T 和 List<T> 泛化类

标签 java android generics caching robospice

我正在尝试泛化我的类结构。
我将更具体地展示我的真实结构。

我正在编写支持离线模式的应用程序,所以我决定使用 RobospiceGreenDao ORM 来实现我的 ETag 缓存机制。

我只需要缓存 GET 请求。

首先我的请求应该扩展基本请求(不是我的),在我的例子中是RetrofitSpiceRequest<T, V>

T is type of return data   
V is service type, in my case I am  using Retrofit.

问题是返回类型不是List of T默认类型,我需要创建扩展 T 对象数组并将其用作返回类型的子类。

类似这样的东西

public class City {
....
....
....
    public static class List extends ArrayList<City> {
    .....
    .....
    }

}

并使用 City.List 作为返回类型。

但是我的 DAO 声明如下

public class CityDao extends AbstractDao<City, Long> {

}

在每个请求 (GET) 中,我需要将特定的 DAO 作为成员,以便在数据与服务器数据不同时缓存数据。或者在没有连接的情况下从本地数据库加载数据。

这里的问题是请求由 T 类型生成,在我的例子中,T 类型主要是一些对象的列表,City.List,但我的 dao 是由例如 E 类型生成的,在我的例子中是 City。

我想创建这样的方法

public AbastractDao<T,Long> getRequestDao() {

}

但是对于我的Request返回City.List,我不知道如何泛化这个类,我觉得可以,但是现在没有想法。
在非通用 dao 方法的情况下,我必须像这样复制代码

 @Override
    public void insertReceivedData(City.List received) {
        mCityDao.insertOrReplaceInTx(received);
    }

 @Override
    public City.List getCachedData() {
        if (mFilterMap != null && mFilterMap.size() > 0) {
            return (City.List) mCityDao.loadAll();
        } else {
            WhereCondition[] whereConditions = QueryUtils.convertPropertyMapToConditionalArray(mFilterMap);
            return (City.List) mCityDao.queryBuilder().where(whereConditions[0], Arrays.copyOfRange(whereConditions, 1, whereConditions.length)).list();
        }
    }

在每个请求中

请分享您的想法。

谢谢。

最佳答案

我最终得到了以下解决方案。它没有我想要的那么好,但它比复制代码更有效。

我的基本请求类。

public abstract class BaseGetRequest<L extends List<T>, T, V> extends RetrofitSpiceRequest<L, V> implements FilterableRequest {
    // Context
    protected Context mContext;
    // Filter used in request and in queries
    protected Map<Property, String> mFilterMap;
    // Session provided Singletone
    protected DaoSessionProvider mSessionProvider;

    public BaseGetRequest(Class<L> clazz, Class<V> retrofitedInterfaceClass, Context context, Map<Property, String> filterMap) {
        super(clazz, retrofitedInterfaceClass);
        mContext = context;
        mFilterMap = filterMap;
        mSessionProvider = ((DaoSessionProvider) mContext.getApplicationContext());
        // TODO determine required retry count
        setRetryPolicy(new RetryPolicy() {
            @Override
            public int getRetryCount() {
                return 0;
            }

            @Override
            public void retry(SpiceException e) {

            }

            @Override
            public long getDelayBeforeRetry() {
                return 0;
            }
        });
    }

    protected WhereCondition[] getWhereConditions() {
        return QueryUtils.convertPropertyMapToConditionalArray(mFilterMap);
    }

    public BaseGetRequestV2(Class<L> clazz, Class<V> retrofitedInterfaceClass, Context context) {
        this(clazz, retrofitedInterfaceClass, context, null);
    }

    public abstract AbstractDao<T, Long> getDao();

    public abstract L createDataList(List<T> list);

    public L getCachedData() {
        if (mFilterMap != null && mFilterMap.size() > 0) {
            WhereCondition[] whereConditions = getWhereConditions();
            return createDataList(getDao().queryBuilder().where(whereConditions[0], Arrays.copyOfRange(whereConditions, 1, whereConditions.length)).list());
        } else {
            return createDataList(getDao().loadAll());
        }
    }

    public abstract L getData();

    @Override
    public Map<Property, String> getFilterMap() {
        return mFilterMap;
    }

    public Map<String, String> getStringMap() {
        return QueryUtils.convertPropertyMapToString(mFilterMap);
    }

    @Override
    public L loadDataFromNetwork() throws Exception {
        L receivedData = null;
        try {
            receivedData = getData();
            WhereCondition[] conditions = getWhereConditions();
            getDao().queryBuilder().where(conditions[0],Arrays.copyOfRange(conditions, 1, conditions.length)).buildDelete().executeDeleteWithoutDetachingEntities();
            getDao().insertOrReplaceInTx(receivedData);
        } catch (Exception ex) {
            receivedData = getCachedData();
        }
        return receivedData;
    }
}

我可以像这样扩展这个类:

public class NewsRequest extends BaseGetRequest<NewsArticle.List, NewsArticle, API> {

    public static final String TARGET_URL = "/news";
    NewsArticleDao mNewsArticleDao;

    public NewsRequest(Context context) {
        this(context, null);
    }

    public NewsRequest(Context context, Map<Property, String> filterMap) {
        super(NewsArticle.List.class, API.class, context, filterMap);
        mNewsArticleDao = mSessionProvider.getDaoSession().getNewsArticleDao();
    }

    @Override
    public AbstractDao<NewsArticle, Long> getDao() {
        return mNewsArticleDao;
    }

    @Override
    public NewsArticle.List createDataList(List<NewsArticle> list) {
       return new NewsArticle.List(list);
    }

    @Override
    public NewsArticle.List getData() {
        return getService().getNews(getStringMap());
    }
}

关于java - 如何使用 T 和 List<T> 泛化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754844/

相关文章:

java - Eclipse 在资源管理器中显示项目

android - 如何管理自定义首选项布局

android - 如何使用 Mockito 在单元测试中调用 AppCompatActivity onCreate

java - 如果我使用与泛型方法中使用的类型参数占位符 'X' 相同的类名 'X',哪个 X 有效?

java - 类型注释属于有界通配符的什么位置?

java - 使用java区分文件和目录

java - 为什么java不支持多重继承

java - 如何修复 'Unchecked cast from MyClass to T'

java - Java中自定义鼠标光标绘制(swing)

android - 如何保护/检测 Android 设备上的数据库还原?