All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jdroid.javaweb.search.Pager Maven / Gradle / Ivy

The newest version!
package com.jdroid.javaweb.search;

import com.jdroid.java.exception.UnexpectedException;

public class Pager {
	
	private final static Integer DEFAULT_PAGE = 1;
	private final static Integer DEFAULT_PAGE_SIZE = 25;
	
	private Integer size;
	private Integer page;
	
	/**
	 * @param page
	 * @param size
	 */
	public Pager(Integer page, Integer size) {
		
		this.page = page != null ? page : DEFAULT_PAGE;
		this.size = size != null ? size : DEFAULT_PAGE_SIZE;
		
		if (this.page < 1) {
			throw new UnexpectedException("Wrong page [" + page + "]");
		}
		
		if (this.size < 1) {
			throw new UnexpectedException("Wrong page size [" + size + "]");
		}
	}
	
	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}
	
	/**
	 * @return the size
	 */
	public Integer getSize() {
		return size;
	}
	
	public Integer getOffset() {
		return (page - 1) * size;
	}
	
	public Integer getMaxPages(Long total) {
		return (int)Math.ceil((total * 1.0) / size);
	}
	
	public Boolean isLastPage(Long total) {
		return getMaxPages(total).equals(page);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy