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

net.anotheria.util.slicer.Slicer Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package net.anotheria.util.slicer;

import java.util.ArrayList;
import java.util.List;
/**
 * Slicer is an utility for cutting large amount of data in slices needed for paging.
 *
 * @author lrosenberg
 * @version $Id: $Id
 */
public class Slicer {

	/**
	 * Returns the requested slice of the data if available.
	 *
	 * @param data the data to slice.
	 * @return a typed Slice.
	 * @param segment a {@link net.anotheria.util.slicer.Segment} object.
	 * @param  a T object.
	 */
	public static  Slice slice(Segment segment, List data){
		int totalElements = data.size();
		
		Slice ret = new Slice<>(segment);
		ret.setTotalNumberOfItems(totalElements);

		double pageCount = (double)totalElements / segment.getElementsPerSlice();
		int showedPageCount = (int)pageCount;
		if (showedPageCounttotalElements){
			ret.setSliceData(new ArrayList());
			return ret;
		}
		
		if (endIndex>totalElements)
			endIndex = totalElements;
		
		if (endIndex "+startIndex);
		
		List sliced = data.subList(startIndex, endIndex);
		ret.setSliceData(sliced);
		
		return ret;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy