001/*
002 * Copyright 2014 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.common.paging;
017
018import static org.gbif.api.model.common.paging.PagingConstants.DEFAULT_PARAM_LIMIT;
019import static org.gbif.api.model.common.paging.PagingConstants.DEFAULT_PARAM_OFFSET;
020
021/**
022 * Generically is a class that contains attributes used by operations that are aware of pagination.
023 * Its properties are final to allow calculations in other setter methods.
024 */
025public class PageableBase implements Pageable {
026
027  protected long offset;
028  protected int limit;
029
030  /**
031   * Default constructor with default paging values.
032   */
033  protected PageableBase() {
034    offset = DEFAULT_PARAM_OFFSET;
035    limit = DEFAULT_PARAM_LIMIT;
036  }
037
038  /**
039   * Full constructor based on a request.
040   *
041   * @throws IllegalArgumentException if negative offset or limit.
042   */
043  protected PageableBase(Pageable page) {
044    setOffset(page.getOffset());
045    setLimit(page.getLimit());
046  }
047
048  /**
049   * Full constructor.
050   *
051   * @throws IllegalArgumentException if negative offset or limit.
052   */
053  protected PageableBase(long offset, int limit) {
054    setOffset(offset);
055    setLimit(limit);
056  }
057
058  /**
059   * Total of rows that are returned.
060   */
061  @Override
062  public int getLimit() {
063    return limit;
064  }
065
066  /**
067   * @param limit the non negative limit to set
068   *
069   * @throws IllegalArgumentException if negative
070   */
071  public void setLimit(int limit) {
072    if (limit < 0) {
073      throw new IllegalArgumentException("Limit cannot be negative");
074    }
075    this.limit = limit;
076  }
077
078  /**
079   * Defines how many items to skip before beginning to return rows.
080   */
081  @Override
082  public long getOffset() {
083    return offset;
084  }
085
086  /**
087   * @param offset the non negative offset to set
088   *
089   * @throws IllegalArgumentException if negative
090   */
091  public void setOffset(long offset) {
092    if (offset < 0) {
093      throw new IllegalArgumentException("Offset cannot be negative");
094    }
095    this.offset = offset;
096  }
097
098  /**
099   * Adds to existing offset, setting offset to zero if it would be negative.
100   *
101   * @param offsetDiff to be added to existing offset.
102   */
103  public void addOffset(long offsetDiff) {
104    offset += offsetDiff;
105    if (offset < 0) {
106      offset = 0;
107    }
108  }
109
110  /**
111   * Utility method to copy paging values.
112   */
113  public void copyPagingValues(Pageable pageable) {
114    limit = pageable.getLimit();
115    offset = pageable.getOffset();
116  }
117
118}