2015-11-17 25 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