PaginationHelper { public static List<BaseDimensionalItemObject> slice( final WebOptions options, List<BaseDimensionalItemObject> dimensionalItems ) { state( options.getPage() > 0, "Current page must be greater than zero." ); state( options.getPageSize() > 0, "Page size must be greater than zero." ); if ( options.hasPaging() && isNotEmpty( dimensionalItems ) ) { final int currentPage = options.getPage(); final int totalOfElements = dimensionalItems.size(); final int maxElementsPerPage = options.getPageSize(); final Pager pager = new Pager( currentPage, totalOfElements, maxElementsPerPage ); final int currentElementIndex = pager.getOffset(); final boolean hasMorePages = (totalOfElements - currentElementIndex) > pager.getPageSize(); if ( hasMorePages ) { final int nextElementsWindow = pager.getPageSize() * pager.getPage(); dimensionalItems = dimensionalItems.subList( currentElementIndex, nextElementsWindow ); } else { dimensionalItems = dimensionalItems.subList( pager.getOffset(), totalOfElements ); } } return dimensionalItems; }  static List<BaseDimensionalItemObject> slice( final WebOptions options,
        List<BaseDimensionalItemObject> dimensionalItems );  }
@Test public void testSliceWhenFirstPage() { final int pageSize = 5; final int firstPage = 1; final int totalOfItems = 13; final WebOptions theWebOptions = mockWebOptions( pageSize, firstPage ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( totalOfItems ); final List<BaseDimensionalItemObject> resultingList = slice( theWebOptions, anyDimensionalItems ); assertThat( resultingList, hasSize( 5 ) ); }
@Test public void testSliceWhenIntermediatePage() { final int pageSize = 5; final int secondPage = 2; final int totalOfItems = 13; final WebOptions theWebOptions = mockWebOptions( pageSize, secondPage ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( totalOfItems ); final List<BaseDimensionalItemObject> resultingList = slice( theWebOptions, anyDimensionalItems ); assertThat( resultingList, hasSize( 5 ) ); }
@Test public void testSliceWhenLastPage() { final int pageSize = 5; final int lastPage = 3; final int totalOfItems = 13; final WebOptions theWebOptions = mockWebOptions( pageSize, lastPage ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( totalOfItems ); final List<BaseDimensionalItemObject> resultingList = slice( theWebOptions, anyDimensionalItems ); assertThat( resultingList, hasSize( 3 ) ); }
@Test public void testSliceWhenPageSizeIsZero() { final int pageSize = 0; final int lastPage = 3; final int totalOfItems = 13; final WebOptions theWebOptions = mockWebOptions( pageSize, lastPage ); final List<BaseDimensionalItemObject> anyDimensionalItems = mockDimensionalItems( totalOfItems ); expectedException.expect( IllegalStateException.class ); expectedException.expectMessage( "Page size must be greater than zero." ); slice( theWebOptions, anyDimensionalItems ); }
@Test public void testSliceWhenDimensionalItemListIsEmpty() { final int pageSize = 5; final int lastPage = 3; final WebOptions theWebOptions = mockWebOptions( pageSize, lastPage ); final List<BaseDimensionalItemObject> emptyDimensionalItems = emptyList(); final List<BaseDimensionalItemObject> resultingList = slice( theWebOptions, emptyDimensionalItems ); assertThat( resultingList, is( emptyDimensionalItems ) ); assertThat( resultingList, hasSize( 0 ) ); }
@Test public void testSliceWhenPageIsZero() { final int pageSize = 5; final int currentPage = 0; final WebOptions theWebOptions = mockWebOptions( pageSize, currentPage ); final List<BaseDimensionalItemObject> emptyDimensionalItems = emptyList(); expectedException.expect( IllegalStateException.class ); expectedException.expectMessage( "Current page must be greater than zero." ); slice( theWebOptions, emptyDimensionalItems ); }