import java.io.Serializable; import java.util.Collection; import java.util.concurrent.Callable; import org.springframework.web.context.request.async.WebAsyncTask; import com.github.miemiedev.mybatis.paginator.domain.PageList; /** * 对于将会频繁创建的对象,我们要让这个类实现Cloneable接口,通过getInstance()函数处理获取对象 * clone的最大特点就是,不会去调用任何构造方法,提高很多频繁new对象产生的时耗 * * @作者 light-zhang * @时间 2018年8月16日 * @file WebAsyncSupport.java * */ public final class WebAsyncSupport implements Serializable, Cloneable { private static final long serialVersionUID = -1642396555652972191L; private static WebAsyncSupport support = new WebAsyncSupport(); private WebAsyncSupport() { super(); } /** * 调用对象创建优化 * * @return */ public static WebAsyncSupport getInstance() { try { return (WebAsyncSupport) support.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return new WebAsyncSupport(); } /** * 分页参数处理 * * @param items * @param containsTotalCount * @return */ public WebAsyncTask<ReturnMsg> asyncPageList(Collection<?> items, boolean containsTotalCount) { ReturnMsg m = null; m = ReturnMsg.getPage("SUCCESS", HttpStatusCode.OK, items, null); if (containsTotalCount) {// 使用containsTotalCount来决定查不查询totalCount,即返回ArrayList还是PageList @SuppressWarnings("unchecked") final PageList<Object> pageData = (PageList<Object>) items; if (!validateListNull(pageData)) { m = ReturnMsg.getPage("SUCCESS", HttpStatusCode.OK, pageData, pageData.getPaginator()); } } return this.getWebAsyncTask(m); } /** * 异步任务处理 * * @param m * @return */ public WebAsyncTask<ReturnMsg> getWebAsyncTask(ReturnMsg m) { return new WebAsyncTask<ReturnMsg>(1000, new Callable<ReturnMsg>() { @Override public ReturnMsg call() throws Exception { return m; } }); } /** * 验证数组参数 * * @param list * @return */ public static boolean validateListNull(Collection<?> list) { return (list == null || list.isEmpty()) ? true : false; } }