Java tutorial
/* * Copyright 2013-2014 the original author or authors. * * Licensed 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.eryansky.common.orm.core; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.util.Assert; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * ??. * @author maurice */ public class PageRequest implements java.io.Serializable { private static final long serialVersionUID = 1L; /** * */ protected int pageNo = 1; /** * ?? */ protected int pageSize = 10; /** * ? */ protected String orderBy = null; /** * ?? * @see com.eryansky.common.orm.core.PageRequest.Sort */ protected String orderDir = null; /** * ?. */ protected boolean countTotal = true; /** * */ public PageRequest() { } /** * * @param pageNo * @param pageSize ?? */ public PageRequest(int pageNo, int pageSize) { this.pageNo = pageNo; this.pageSize = pageSize; } /** * ??, ??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; } } /** * ?, . ?','. */ public String getOrderBy() { return orderBy; } /** * ?, ?','. */ public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } /** * ??, . */ 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, ','); if (ArrayUtils.isEmpty(orderDirs)) { return; } for (String orderDirStr : orderDirs) { if (!StringUtils.equals(Sort.DESC, orderDirStr) && !StringUtils.equals(Sort.ASC, orderDirStr)) { throw new IllegalArgumentException("??" + orderDirStr + "??"); } } this.orderDir = lowcaseOrderDir; } /** * ??. */ @SuppressWarnings("unchecked") public List<Sort> getSort() { if (orderBy == null || orderDir == null) { return Collections.EMPTY_LIST; } String[] orderBys = StringUtils.split(orderBy, ','); String[] orderDirs = StringUtils.split(orderDir, ','); Assert.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; } /** * ??,. */ public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(orderDir)); } /** * ?. */ public boolean isCountTotal() { return countTotal; } /** * ?. */ public void setCountTotal(boolean countTotal) { this.countTotal = countTotal; } /** * ?pageNopageSize???, ??0. */ public int getOffset() { return ((pageNo - 1) * pageSize); } /** * ?Order By?sql * @return String */ public String getOrderSortString() { List<Sort> list = this.getSort(); StringBuffer buffer = new StringBuffer(); for (Iterator<Sort> it = list.iterator(); it.hasNext();) { Sort sort = it.next(); buffer.append(sort.property + " " + sort.dir).append(","); } return StringUtils.substringBeforeLast(buffer.toString(), ","); } /** * ?? * * @author maurice * */ public static class Sort { public static final String ASC = "asc"; public static final String DESC = "desc"; private final String property; private final String dir; /** * * @param property ?? * @param dir ?? */ public Sort(String property, String dir) { this.property = property; this.dir = dir; } /** * ?? * @return String */ public String getProperty() { return property; } /** * ??? * * @return String */ public String getDir() { return dir; } } }