2015-11-17 22 views
7

我正试图生成我的类结构。
我将展示我的真实结构以更具体。如何通过T和列表生成类<T>

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

我只需要缓存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作为成员,以高速缓存数据如果来自服务器的数据不同。或者如果没有连接,则从本地数据库加载数据。

这里的问题是,请求通过大部分是列表的城市列表,我的情况下,一些对象City.List,但我的道路被基因化,例如城市在我的情况下,E类型。

我要像创建方法此

public AbastractDao<T,Long> getRequestDao() { 

} 

但据我的请求返回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(); 
     } 
    } 

复制代码在每次请求

请分享您的想法。

谢谢。

+0

我理解的这个权利,你希望你的服务,以自动注入,并调用适当的道? –

+0

是的,但问题是,道是扩展AbstractDao 和主要用作AbstractDao 其中T是映射到我的情况城市表中的类型,但我需要获取城市的列表,只要请求返回对象的TI类必须创建自定义类MyCityList扩展ArrayList 这里是问题所在。 – CROSP

回答

2

我最终得到以下解决方案。它不如我想要的那样好,但它比重复代码更有效。

我的基地请求类。

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; 
    } 
} 

,我还可以扩展此类

公共类NewsRequest扩展BaseGetRequest { 公共静态最后弦乐TARGET_URL = “/新闻”; 新闻文章道mNews文章道;

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()); 
} 

}

+0

伟大的解决方案的人 – ketazafor