OffsetRequest.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * 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 org.genesys.server.api;

import java.io.Serializable;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * OffsetRequest
 */
public class OffsetRequest implements Pageable, Serializable {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 3521785103051707272L;

	/** The offset. */
	private long offset;
	
	/** The page size. */
	private int pageSize;
	
	/** The sort. */
	private Sort sort;

	/**
	 * Of.
	 *
	 * @param offset the offset
	 * @param pageSize the page size
	 * @param sort the sort
	 * @return the offset request
	 */
	public static OffsetRequest of(long offset, int pageSize, Sort sort) {
		OffsetRequest or = new OffsetRequest();
		or.offset = offset;
		or.pageSize = pageSize;
		or.sort = sort;
		return or;
	}

	/**
	 * Of.
	 *
	 * @param offset the offset
	 * @param pageSize the page size
	 * @param direction the direction
	 * @param properties the properties
	 * @return the offset request
	 */
	public static OffsetRequest of(long offset, int pageSize, Sort.Direction direction, String... properties) {
		return of(offset, pageSize, Sort.by(direction, properties));
	}

	/**
	 * Gets the page number.
	 *
	 * @return the page number
	 */
	@Override
	@JsonIgnore
	public int getPageNumber() {
		return offset == 0 ? 0 : 1;
	}

	/**
	 * Gets the page size.
	 *
	 * @return the page size
	 */
	@Override
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * Gets the offset.
	 *
	 * @return the offset
	 */
	@Override
	public long getOffset() {
		return offset;
	}

	/**
	 * Gets the sort.
	 *
	 * @return the sort
	 */
	@Override
	public Sort getSort() {
		return sort;
	}

	/**
	 * Next.
	 *
	 * @return the pageable
	 */
	@Override
	@JsonIgnore
	public Pageable next() {
		return OffsetRequest.of(this.offset + pageSize, pageSize, sort);
	}

	/**
	 * Previous or first.
	 *
	 * @return the pageable
	 */
	@Override
	@JsonIgnore
	public Pageable previousOrFirst() {
		return OffsetRequest.of(offset == 0 ? 0 : offset - pageSize, pageSize, sort);
	}

	/**
	 * First.
	 *
	 * @return the pageable
	 */
	@Override
	@JsonIgnore
	public Pageable first() {
		return OffsetRequest.of(0, pageSize, sort);
	}

	@Override
	public Pageable withPage(int pageNumber) {
		if (pageNumber == 0) {
			return this;
		} else {
			throw new UnsupportedOperationException();
		}
	}

	/**
	 * Checks for previous.
	 *
	 * @return true, if successful
	 */
	@Override
	@JsonIgnore
	public boolean hasPrevious() {
		return offset > 0;
	}
}