@Test public void testPaginationFitsLeft(){ Map<String,Object> stringObjectMap = paginationBuilderService.buildPages(3, 15, 4); Integer[] pages = (Integer[]) stringObjectMap.get("pages"); Integer selected = (Integer) stringObjectMap.get("selected"); Assert.assertEquals(3,selected, 0); Assert.assertEquals(9, pages.length); testPages(pages, new Integer[]{ 1,2,3,4,5,6,7,8,9 }); }
public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; }
PaginationBuilderService { public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; } }
PaginationBuilderService { public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; }  }
PaginationBuilderService { public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; }  Map<String, Object> buildPages(
            Integer selectedPage,
            int totalPages,
            int depthSize
    ); }
PaginationBuilderService { public Map<String, Object> buildPages( Integer selectedPage, int totalPages, int depthSize ){ logger.trace("Map<String,Object> buildPages(selectedPage= {},totalPages={},depthSize={})",new Object[]{selectedPage,totalPages,depthSize}); Map<String,Object> pagination = new HashMap<String, Object>(); int returnSize = Math.min(((depthSize*2)+1),totalPages); Integer[] pages = new Integer[returnSize]; if(selectedPage <= depthSize+1){ logger.trace("Map<String,Object> buildPages(pages) Keeping to left side no need to scroll "); for(int i =1; i <= returnSize ; i++){ pages[i-1]=i; } }else if( selectedPage > totalPages - (depthSize*2)){ logger.trace("Map<String,Object> buildPages(pages) Working from right <- left, not enough spaces on the right {}",returnSize); for(int i =0 ; i < returnSize; i++){ pages[i] = (totalPages-returnSize+1)+i; } }else{ pages[depthSize]=selectedPage; for(int i=0; i< depthSize; i++){ pages[i]= selectedPage-(depthSize-i); pages[depthSize+1+i] = selectedPage+1+i; } } pagination.put("first",1); pagination.put("pages",pages); pagination.put("last",totalPages); pagination.put("selected",selectedPage); return pagination; }  Map<String, Object> buildPages(
            Integer selectedPage,
            int totalPages,
            int depthSize
    );  }