Java tutorial
/** * Copyright (c) 2005-2011 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: Fixtures.java 1593 2011-05-11 10:37:12Z calvinxiu $ */ package org.hy.foundation.utils.page; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlTransient; import org.apache.commons.lang.StringUtils; import org.codehaus.jackson.annotate.JsonIgnore; import org.hy.foundation.utils.asserts.AssertUtils; /** * ??. */ public class Page { //-- ? --// protected int pageNo = 1; protected int pageSize = 10; protected String orderBy = null; protected String orderDir = null; protected boolean countTotal = true; protected Integer offset; //-- --// protected List<?> result = null; protected long totalCount = -1; public Page() { } /** * ??RESTFUL?String???? * @param pageStr <pre>2-20-nowdate,totalnum_desc,asc</pre> or <pre>2-20-</pre> */ public Page(String pageStr) { String[] nums = pageStr.split("-"); // ?- if (2 == nums.length) { this.pageNo = Integer.valueOf(nums[0]); this.pageSize = Integer.valueOf(nums[1]); if (pageStr.indexOf("_") != -1) { // ?# this.orderBy = nums[2].split("_")[0]; this.orderDir = nums[2].split("_")[1]; } } } public Page(int pageNo, int pageSize) { this.pageNo = pageNo; this.pageSize = pageSize; } public Page(int pageNo, int pageSize, final String orderByStr) { this.pageNo = pageNo; this.pageSize = pageSize; if (orderByStr.indexOf("_") > -1) { // ?# this.orderBy = orderByStr.split("_")[0]; this.orderDir = orderByStr.split("_")[1]; } } /** * ??, ??1, 1. */ public int getPageNo() { return pageNo; } /** * ??, ??1, 11. */ public void setPageNo(final int pageNo) { this.pageNo = pageNo; if (pageNo < 1) { this.pageNo = 1; } } /** * ??, 10. */ public int getPageSize() { return pageSize; } /** * ??, 11. */ public void setPageSize(final int pageSize) { this.pageSize = pageSize; if (pageSize < 1) { this.pageSize = 1; } } /** * ?, . ?','. */ @XmlTransient @JsonIgnore public String getOrderBy() { return orderBy; } /** * ?, ?','. */ public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } /** * ??, . */ @XmlTransient @JsonIgnore public String getOrderDir() { return orderDir; } /** * ???. * * @param orderDir * ?descasc,?','. */ public void setOrderDir(final String orderDir) { String lowcaseOrderDir = StringUtils.lowerCase(orderDir); // order? String[] orderDirs = StringUtils.split(lowcaseOrderDir, ','); for (String orderDirStr : orderDirs) { if (!StringUtils.equals(Sort.DESC, orderDirStr) && !StringUtils.equals(Sort.ASC, orderDirStr)) { throw new IllegalArgumentException("??" + orderDirStr + "??"); } } this.orderDir = lowcaseOrderDir; } /** * ??. */ @XmlTransient @JsonIgnore public List<Sort> getSort() { String[] orderBys = StringUtils.split(orderBy, ','); String[] orderDirs = StringUtils.split(orderDir, ','); AssertUtils.isTrue(orderBys.length == orderDirs.length, "???,????"); List<Sort> orders = new ArrayList<Sort>(); for (int i = 0; i < orderBys.length; i++) { orders.add(new Sort(orderBys[i], orderDirs[i])); } return orders; } /** * ??,. */ @XmlTransient @JsonIgnore public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(orderDir)); } /** * ?. */ @XmlTransient @JsonIgnore public boolean isCountTotal() { return countTotal; } /** * ?. */ public void setCountTotal(boolean countTotal) { this.countTotal = countTotal; } /** * ?pageNopageSize???, ??0. */ public Integer getOffset() { return null != offset ? offset : ((pageNo - 1) * pageSize); } public void setOffset(Integer offset) { this.offset = offset; } public List<?> getResult() { return result; } public Page setResult(List<?> result) { this.result = result; return this; } public long getTotalCount() { return totalCount; } public Page setTotalCount(long totalCount) { this.totalCount = totalCount; return this; } public static class Sort { public static final String ASC = "asc"; public static final String DESC = "desc"; private String property; private String dir; public Sort() { } public Sort(String toString) { String[] strs = toString.split(","); this.property = strs[0]; this.dir = strs[1]; } public Sort(String property, String dir) { this.property = property; this.dir = dir; } public String getProperty() { return property; } public String getDir() { return dir; } public String toString() { return this.property + "," + this.dir; } } }