Java tutorial
/** * Copyright (c) 2010 www.pub.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: Page.java,v 1.4 2010/08/22 07:10:35 xp Exp $ */ package com.abssh.util; import java.io.Serializable; import java.util.Collections; import java.util.List; import org.apache.commons.lang.StringUtils; /** * ORM???. ???1. pageSize=0? * * @param <T> * Page. * @author pub */ public class Page<T> implements Serializable { private static final long serialVersionUID = 7489398000885811711L; public static final String ASC = "asc"; public static final String DESC = "desc"; public static final int DEFAILT_PAGE_SIZE = 10; /** ? */ private int pageNo = 1; // ??1? private int pageSize = DEFAILT_PAGE_SIZE; // ? 0-? private String orderBy = null; // ?? private String order = null; // ?(ascdesc)? private boolean autoCount = true;// ????true private String searchParam_h; private String searchValue; private String searchName; private List<T> result = Collections.emptyList();// private long totalCount = -1; /** * Page(10) */ public Page() { this.pageSize = DEFAILT_PAGE_SIZE; } /** * Page * * @param pageSize * ? */ public Page(final int pageSize) { setPageSize(pageSize);// 0-??? } /** * Page * * @param pageSize * ? * @param autoCount * ?? */ public Page(final int pageSize, final boolean autoCount) { setPageSize(pageSize);// 0-??? setAutoCount(autoCount); } // -- ? --// /** * ??,??1,1. */ public int getPageNo() { return pageNo; } /** * ??,??1,11. */ public void setPageNo(final Long pageNo) { if (pageNo == null || pageNo.intValue() < 1) { this.pageNo = 1; } else { this.pageNo = pageNo.intValue(); } } /** * ??. */ public int getPageSize() { return pageSize; } /** * ??,11. */ public void setPageSize(final int pageSize) { if (pageSize >= 0) { this.pageSize = pageSize; } else { this.pageSize = 0; } } /** * ?pageNopageSize???,??1. */ public int getFirst() { return ((pageNo - 1) * pageSize) + 1; } /** * ?,.?','. */ public String getOrderBy() { return orderBy; } /** * ?,?','. */ public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } /** * ??,. */ public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order)); } /** * ??. */ public String getOrder() { return order; } /** * ???. * * @param order * ?descasc,?','. */ public void setOrder(final String order) { // order? if (order == null) { return; } 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); } /** * ??count?, false. */ public boolean isAutoCount() { return autoCount; } /** * ??count?. */ public void setAutoCount(final boolean autoCount) { this.autoCount = autoCount; } // -- --// /** * ?. */ public List<T> getResult() { return result; } /** * . */ public void setResult(final List<T> result) { this.result = result; } /** * ?, -1. */ public long getTotalCount() { return totalCount; } /** * . */ public void setTotalCount(final long totalCount) { this.totalCount = totalCount; } /** * ?pageSizetotalCount, -1. */ public long getTotalPages() { if (pageSize <= 0 || totalCount < 0) { return -1; } long count = totalCount / pageSize; if (totalCount % pageSize > 0) { count++; } return count; } /** * ?. */ public boolean isHasNext() { return (pageNo + 1 <= getTotalPages()); } /** * ??, ??1. ????. */ public int getNextPage() { if (isHasNext()) { return pageNo + 1; } else { return pageNo; } } /** * ?. */ public boolean isHasPre() { return (pageNo - 1 >= 1); } /** * ??, ??1. ???. */ public int getPrePage() { if (isHasPre()) { return pageNo - 1; } else { return pageNo; } } public String getSearchParam_h() { return searchParam_h; } public void setSearchParam_h(String searchParam_h) { this.searchParam_h = searchParam_h; } public String getSearchValue() { return searchValue; } public void setSearchValue(String searchValue) { this.searchValue = searchValue; } public String getSearchName() { return searchName; } public void setSearchName(String searchName) { this.searchName = searchName; } }