package com.jiuqi.mobile.nigo.comeclose.bean;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.internal.$Gson$Types;
import com.jiuqi.mobile.nigo.comeclose.bean.app.news.CarTaskNewsBean;
import com.jiuqi.mobile.nigo.comeclose.exception.IndexOutException;
import com.jiuqi.mobile.nigo.comeclose.manager.ILoadDataHandler;
import com.jiuqi.mobile.nigo.comeclose.manager.ManagerFactory;
import com.jiuqi.mobile.nigo.comeclose.manager.app.INewsManager;
import com.jiuqi.mobile.nigo.comeclose.manager.app.SelectNewsKey;
/**
* 返回结果集封装,此类包含数据请求。不利于用户远程使用。<br/>
* 客户端访问时,请谨慎使用。。。使用前请于本人沟通<br/>
* 客户端使用方法:<br/>
* void handler(){<br/>
* String session = "";<br/>
* INewsManager manager = ManagerFactory.instanceManager(INewsManager.class,
* session );<br/>
* int pageSize = 15;<br/>
* long startIndex = 0;<br/>
* SelectNewsKey key = new SelectNewsKey(pageSize);<br/>
* key.setStartIndex(startIndex);<br/>
* iterateList(manager, pageSize, startIndex, key);<br/>
* //第二次请求<br/>
* startIndex += pageSize;<br/>
* key.setStartIndex(startIndex);<br/>
* iterateList(manager, pageSize, startIndex, key);<br/>
* ...<br/>
* }<br/>
* private void iterateList(INewsManager manager, int pageSize,<br/>
* long startIndex, SelectNewsKey key) {<br/>
* LoadOnGetList<CarTaskNewsBean> list = manager.get(CarTaskNewsBean.class,
* key);<br/>
* for(long i=startIndex; i<Math.min(pageSize+startIndex,
* list.getTotalCount());i++){<br/>
* CarTaskNewsBean bean = list.get(startIndex);<br/>
* }<br/>
* }<br/>
*
* @author modi
*
* @param <T>
*/
public class LoadOnGetList20131227<T> {
@Expose(deserialize = false, serialize = false)
private ILoadDataHandler<T> loadData;
private int size;
// private final SelectKey key;
private List<T> list = new ArrayList<T>();
private long startIndex;
private long totalCount;
private boolean isEnd = false;
void mobileFoo() {
String session = "";
INewsManager manager = ManagerFactory.instanceManager(
INewsManager.class, session);
int pageSize = 15;
long startIndex = 0;
SelectNewsKey key = new SelectNewsKey(pageSize);
key.setStartIndex(startIndex);
iterateList(manager, pageSize, startIndex, key);
// 第二次请求
startIndex += pageSize;
key.setStartIndex(startIndex);
iterateList(manager, pageSize, startIndex, key);
}
private void iterateList(INewsManager manager, int pageSize,
long startIndex, SelectNewsKey key) {
LoadOnGetList<CarTaskNewsBean> list = manager.get(
CarTaskNewsBean.class, key);
for (long i = startIndex; i < Math.min(pageSize + startIndex,
list.getTotalCount()); i++) {
CarTaskNewsBean bean = list.get(startIndex);
System.out.println(bean);
}
}
public LoadOnGetList20131227() {
}
public LoadOnGetList20131227(List<T> list) {
this.list = list;
this.totalCount = list.size();
this.startIndex = list.size();
this.size = list.size();
}
public LoadOnGetList20131227(ILoadDataHandler<T> loadData, SelectKey key) {
this.size = key.getSize();
this.loadData = loadData;
this.startIndex = 0;
this.startIndex = key.getStartIndex();
try {
load();
} catch (IndexOutException e) {
}
}
public T get(long index) throws IndexOutException {
if (isEnd || index < 0) {
throw new IndexOutException();
}
if (index >= startIndex) {
load();
}
if (index < startIndex - list.size()) {
while (index < startIndex - list.size()) {
startIndex = startIndex - size;
startIndex = startIndex < 0 ? 0 : startIndex;
}
load();
}
int i = (int) (index + list.size() - startIndex);
if (i < 0) {
throw new IndexOutException();
}
return list.get(i);
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
// public SelectKey getKey() {
// return key;
// }
private void load() throws IndexOutException {
if (null == loadData) {
throw new IndexOutException();
}
list = loadData.getData(startIndex, startIndex + size);
if (list.isEmpty()) {
isEnd = true;
setTotalCount(startIndex);
throw new IndexOutException();
} else {
setTotalCount(loadData.getTotalCount());
startIndex += list.size();
}
}
/**
* 服务端禁用,仅限android客户端使用
*
* @return
*/
public List<T> getList() {
return list;
}
public Type getType() {
Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return $Gson$Types
.canonicalize(parameterized.getActualTypeArguments()[0]);
}
}