Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: Page.java */ package com.jkali.client.entity; import java.io.Serializable; import java.util.List; import org.apache.commons.lang.StringUtils; import com.google.common.collect.Lists; /** * ORM???. ???1. * * @param <T> * Page. * * @author paul */ public class Page<T> implements Serializable { /** * */ private static final long serialVersionUID = 1L; // -- ?? --// public static final String ASC = "asc"; public static final String DESC = "desc"; // -- ? --// // protected int pageNo = 1; // protected int pageSize = 1; protected String orderBy = null; protected String order = null; protected boolean autoCount = true; // -- --// // protected List<T> result = Collections.emptyList(); // protected long totalCount = -1; // jqGrid json// -- --// protected int page = 1; // like pageNo protected int total = 1; // total page protected int pageSize = 1;// like pageSize protected long records = -1; // like totalCount protected List<T> rows = Lists.newArrayList(); // -- ? --// /** * ??,??1,1. */ public int getPage() { return page; } /** * ??,??1,11. */ public void setPage(int page) { this.page = page; if (page < 1) { this.page = 1; } } public int getTotal() { return total; } public void setTotal(int total) { if (total < 0) { this.total = -1; } long count = this.records / pageSize; if (this.records % pageSize > 0) { count++; } this.total = (int) count; } /** * ?, -1. */ public long getRecords() { return records; } public void setRecords(long records) { this.records = records; setTotal(1); } // -- --// public Page() { } public Page(int pageSize) { this.pageSize = pageSize; } public Page<T> pageNo(final int thePageNo) { setPage(thePageNo); return this; } /** * ??,1. */ public int getPageSize() { return pageSize; } /** * ??,11. */ public void setPageSize(final int pageSize) { this.pageSize = pageSize; if (pageSize < 1) { this.pageSize = 1; } } public Page<T> pageSize(final int thePageSize) { setPageSize(thePageSize); return this; } /** * ?pageNopageSize???,??1. */ public int getFirst() { return ((page - 1) * pageSize) + 1; } /** * ?,.?','. */ public String getOrderBy() { return orderBy; } /** * ?,?','. */ public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } public Page<T> orderBy(final String theOrderBy) { setOrderBy(theOrderBy); return this; } /** * ??. */ public String getOrder() { return order; } /** * ???. * * @param order * ?descasc,?','. */ public void setOrder(final String order) { // order? String[] orders = StringUtils.split(StringUtils.lowerCase(order), ','); for (String orderStr : orders) { if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) throw new IllegalArgumentException("??" + orderStr + "??"); } this.order = StringUtils.lowerCase(order); } public Page<T> order(final String theOrder) { setOrder(theOrder); return this; } /** * ??,. */ public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order)); } /** * ??count?, false. */ public boolean isAutoCount() { return autoCount; } /** * ??count?. */ public void setAutoCount(final boolean autoCount) { this.autoCount = autoCount; } public Page<T> autoCount(final boolean theAutoCount) { setAutoCount(theAutoCount); return this; } // -- --// /** * ?. */ public List<T> getRows() { return rows; } /** * . */ public void setRows(final List<T> rows) { this.rows = rows; } /** * ?. */ public boolean isHasNext() { return (page + 1 <= getTotal()); } /** * ??, ??1. ????. */ public int getNextPage() { if (isHasNext()) return page + 1; else return page; } /** * ?. */ public boolean isHasPre() { return (page - 1 >= 1); } /** * ??, ??1. ???. */ public int getPrePage() { if (isHasPre()) return page - 1; else return page; } }