Java tutorial
/** * Copyright 2016 benjobs * <p> * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.jredrain.tag; import org.apache.commons.lang3.StringUtils; import java.io.Serializable; import java.util.Collections; import java.util.List; public class Page<T> implements Serializable { private static final long serialVersionUID = 8199494836903931588L; // -- ?? --// public static final String ASC = "asc"; public static final String DESC = "desc"; // -- ? --// protected int pageNo = 1; protected int pageSize = 15; protected String orderBy = null; protected String order = null; protected boolean autoCount = true; // -- --// protected List<T> result = Collections.emptyList(); protected long totalCount = -1; // -- --// public Page() { } public Page(final int pageSize) { setPageSize(pageSize); } public Page(final Integer pageNo, final Integer pageSize) { if (pageNo != null) { setPageNo(pageNo); } if (pageSize != null) { setPageSize(pageSize); } } public Page(final int pageSize, final boolean autoCount) { setPageSize(pageSize); setAutoCount(autoCount); } // -- ? --// /** * ??,??1,1. */ public int getPageNo() { return pageNo; } /** * ??,??1,11. */ public void setPageNo(final int pageNo) { this.pageNo = pageNo; if (pageNo < 1) { this.pageNo = 1; } } /** * ??,15. */ public int getPageSize() { return pageSize; } /** * ??,11. */ public void setPageSize(final int pageSize) { this.pageSize = pageSize; if (pageSize < 1) { this.pageSize = 1; } } /** * ?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? 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 (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; } }