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

org.summerb.approaches.jdbccrud.api.dto.PagerParams Maven / Gradle / Ivy

There is a newer version: 4.1.1
Show newest version
package org.summerb.approaches.jdbccrud.api.dto;

import java.io.Serializable;

/**
 * Pagination params for queries
 * 
 * @author sergey.karpushin
 *
 */
public class PagerParams implements Serializable {
	private static final long serialVersionUID = -4006916172860172208L;

	public static final PagerParams ALL = new PagerParams(0, Integer.MAX_VALUE).unmodifiable();

	public static final String FIELD_OFFSET = "offset";
	public static final String FIELD_MAX = "max";

	protected long offset = 0;
	protected long max = 20;

	public PagerParams() {
	}

	public PagerParams(long offset, long max) {
		setOffset(offset);
		setMax(max);
	}

	public PagerParams unmodifiable() {
		return new Unmodifiable(this);
	}

	public long getOffset() {
		return offset;
	}

	public void setOffset(long offset) {
		if (offset < 0) {
			throw new IndexOutOfBoundsException("Offset must not be less then 0");
		}
		this.offset = offset;
	}

	public long getMax() {
		return max;
	}

	public void setMax(long max) {
		if (max < 1) {
			throw new IndexOutOfBoundsException("Max must not be less then 1");
		}
		this.max = max;
	}

	@Override
	public String toString() {
		return "PagerParams [offset=" + offset + ", max=" + max + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (max ^ (max >>> 32));
		result = prime * result + (int) (offset ^ (offset >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		// Minor LSP violation
		if (!(obj instanceof PagerParams))
			return false;
		PagerParams other = (PagerParams) obj;
		if (max != other.max)
			return false;
		if (offset != other.offset)
			return false;
		return true;
	}

	private static class Unmodifiable extends PagerParams {
		private static final long serialVersionUID = -6876008468615718964L;

		public Unmodifiable(PagerParams pagerParams) {
			this.offset = pagerParams.offset;
			this.max = pagerParams.max;
		}

		@Override
		public void setOffset(long offset) {
			throw new IllegalStateException("Not allowed for unmodifiable object");
		}

		@Override
		public void setMax(long max) {
			throw new IllegalStateException("Not allowed for unmodifiable object");
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy