Java tutorial
/* * Copyright 2015 Kaiserpfalz EDV-Service Roland Lichti * * 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 de.kaiserpfalzEdv.commons.jee.persistence.impl; import de.kaiserpfalzEdv.commons.jee.persistence.CRUDOrder; import de.kaiserpfalzEdv.commons.jee.persistence.PageRequest; import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import static com.google.common.base.Preconditions.checkArgument; /** * This is the request for a given page. * * @author klenkes * @since 1.0 */ public class PageRequestImpl implements PageRequest, Serializable { private static final long serialVersionUID = -7739663953536959586L; /** * Developer's logger. */ private static final Logger LOG = LoggerFactory.getLogger(PageRequestImpl.class); /** * The start row of the page that should be generated. */ private int startRow; /** * The size of the page that should be generated. */ private int pageSize; private final ArrayList<CRUDOrder> order = new ArrayList<>(); /** * Creates a new page request. Need's a start row and a page size. * * @param startRow The start row of the result set. Must not be negative. * @param pageSize The page size of the result set. Must be a positive long value. * @throws IllegalArgumentException if any parameter is negative or the page size is zero. */ public PageRequestImpl(final int startRow, final int pageSize) { checkArgument(startRow >= 0, "'%s' is not valid as start row counter.", startRow); checkArgument(pageSize > 0, "'%s' is not valid as page size.", pageSize); this.startRow = startRow; this.pageSize = pageSize; LOG.trace("Created: {}", this); } @Override public int getStartRow() { return startRow; } @Override public int getPageSize() { return pageSize; } @Override public List<CRUDOrder> getOrder() { return order; } @Override public PageRequestImpl orderBy(String sortField) { return orderBy(sortField, CRUDOrder.ASCENDING); } @Override public PageRequestImpl orderBy(String sortField, CRUDOrder.Direction sortOrder) { order.add(new CRUDOrder(sortField, sortOrder)); return this; } @Override public PageRequestImpl orderBy(CRUDOrder order) { this.order.add(order); return this; } @Override public PageRequestImpl orderBy(CRUDOrder[] order) { Collections.addAll(this.order, order); return this; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof PageRequestImpl)) return false; PageRequestImpl that = (PageRequestImpl) o; return pageSize == that.pageSize && startRow == that.startRow; } @Override public int hashCode() { int result = startRow; result = 31 * result + pageSize; return result; } @Override public String toString() { ToStringBuilder sb = new ToStringBuilder(this).append("startRow", startRow).append("pageSize", pageSize); if (!order.isEmpty()) sb.append("ordered", order); return sb.toString(); } }