Slice.java

/*
 * Copyright 2020 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 java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Sort;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * The data Slice.
 *
 * @param <T> the generic type
 */
public class Slice<T> implements Serializable, Iterable<T> {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 6965069448240229428L;

	/** The data is serialized on the base JSON level. */
	public List<T> content;

	/** The sort. */
	public Sort sort;

	/** The total elements. */
	public long totalElements;

	/** The offset. */
	public long offset;

	/**
	 * Instantiates a new filtered page.
	 *
	 * @param data the data
	 * @param offset the offset
	 * @param totalElements the total elements
	 * @param sort the sort
	 */
	public Slice(final List<T> data, final long offset, final long totalElements, Sort sort) {
		this.content = data;
		this.sort = sort;
		this.totalElements = totalElements;
		this.offset = offset;
	}

	/**
	 * Gets the slice size.
	 *
	 * @return the slice size
	 */
	@JsonProperty
	public int getSliceSize() {
		return this.content == null ? 0 : this.content.size();
	}

	/**
	 * Iterator.
	 *
	 * @return the iterator
	 */
	@Override
	public Iterator<T> iterator() {
		return content.iterator();
	}
}