Java tutorial
/** * Copyright (c) 2005-2011 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: Page.java 1453 2011-01-02 14:21:45Z calvinxiu $ */ package com.proper.uip.common.utils; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.IteratorUtils; import org.apache.commons.lang.StringUtils; /** * ORM???. * ???, ?Paginator. * * @param <T> Page. * */ public class Page<T> implements Serializable, Iterable<T> { /** * */ private static final long serialVersionUID = 1L; //-- ? --// protected int pageNo = 1; protected int pageSize = 10; protected String orderBy = null; protected String order = null; //-- --// protected List<T> rows = new ArrayList<T>(); protected long total = 0; protected List<T> footer = new ArrayList<T>(); public List<T> getFooter() { return footer; } public void setFooter(List<T> footer) { this.footer = footer; } //-- --// public Page() { } public Page(PageConfig config) { this.pageNo = config.pageNo; this.pageSize = config.pageSize; this.order = config.order; this.orderBy = config.orderBy; } //-- ? --// /** * ??,??1,1. */ public Integer getPageNo() { return pageNo; } /** * ??, 0. */ public int getPageSize() { return pageSize; } /** * ?,. ?','. */ public String getOrderBy() { return orderBy; } /** * ??, . */ public String getOrder() { return order; } /** * ???. * * @param order ?descasc,?','. */ public void setOrder(final String order) { String lowcaseOrder = StringUtils.lowerCase(order); //order? String[] orders = StringUtils.split(lowcaseOrder, ','); for (String orderStr : orders) { if (!StringUtils.equals(PageConfig.DESC, orderStr) && !StringUtils.equals(PageConfig.ASC, orderStr)) { throw new IllegalArgumentException("??" + orderStr + "??"); } } this.order = lowcaseOrder; } /** * ??,. */ public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order)); } /** * ?pageNopageSize???,??0. * Mysql,Hibernate. */ public int getOffset() { return ((pageNo - 1) * pageSize); } /** * ?pageNopageSize???,??1. * Oracle. */ public int getStartRow() { return getOffset() + 1; } /** * ?pageNopageSize????, ??1. * Oracle. */ public int getEndRow() { return pageSize * pageNo; } //-- --// /** * . */ public List<T> getRows() { return rows; } /** * . */ public void setRows(final List<T> rows) { this.rows = rows; } /** * Iterable?,?for(Object item : page)?? */ @SuppressWarnings("unchecked") public Iterator<T> iterator() { return rows == null ? IteratorUtils.EMPTY_ITERATOR : rows.iterator(); } /** * , -1. */ public long getTotal() { return total; } /** * . */ public void setTotal(final long total) { this.total = total; // ??, ? /*int pageSize = getPageSize(); if (pageSize > total) { this.pageSize = ((int) total); }*/ // , ? int pageNo = getPageNo(); long totalPage = getTotalPages(); if (pageNo > totalPage) { this.pageNo = ((int) totalPage); } } /** * ??. */ public boolean isLastPage() { return pageNo == getTotalPages(); } /** * ?. */ public boolean isHasNextPage() { return (pageNo + 1 <= getTotalPages()); } /** * ??, ??1. * ????. */ public int getNextPage() { if (isHasNextPage()) { return pageNo + 1; } else { return pageNo; } } /** * ?. */ public boolean isFirstPage() { return pageNo == 1; } /** * ?. */ public boolean isHasPrePage() { return (pageNo - 1 >= 1); } /** * ??, ??1. * ???. */ public int getPrePage() { if (isHasPrePage()) { return pageNo - 1; } else { return pageNo; } } /** * ?pageSizetotalItems, -1. */ public long getTotalPages() { if (total == 0) { return 0; } long count = total / pageSize; if (total % pageSize > 0) { count++; } return count; } /** * ??,",23,24,25,26,27," * @param count ?? * @return pageNo */ public List<Long> getSlider(int count) { int halfSize = count / 2; long totalPage = getTotalPages(); long startPageNumber = Math.max(pageNo - halfSize, 1); long endPageNumber = Math.min(startPageNumber + count - 1, totalPage); if (endPageNumber - startPageNumber < count) { startPageNumber = Math.max(endPageNumber - count, 1); } List<Long> result = new ArrayList<Long>(); for (long i = startPageNumber; i <= endPageNumber; i++) { result.add(new Long(i)); } return result; } }