Pagination { public int[] generateSuggestedPages(int count) { Preconditions.checkState(count > 3,"the count of pages must be more than 3"); int pages = getPages(); if (1 == pages) { return new int[] { 0 }; } int page = getPage(); if (pages <= count) { return linearSeries(pages); } int[] result = new int[count]; int middleI = count / 2; Arrays.fill(result,-10); if (page < middleI) { for(int i = 0; i <= page; i++) { result[i] = i; } fanFillRight(result, page+1); } else { int remainder = pages - page; if (remainder <= (result.length - middleI) - 1) { for (int i = 0; i < remainder; i++) { result[result.length - (i + 1)] = (pages - 1) - i; } fanFillLeft(result,result.length-(remainder + 1)); } else { result[middleI] = page; fanFillRight(result, middleI+1); fanFillLeft(result, middleI-1); } } return result; } Pagination(int total, int offset, int max); int getOffset(); void setOffset(int offset); int getTotal(); void setTotal(int total); int getMax(); void setMax(int max); int getPage(); int getPages(); int[] generateSuggestedPages(int count); @Override String toString();  }
@Test public void testGenerateSuggestedPages_one() { Pagination p = new Pagination(1,0,10); Assertions.assertThat(p.generateSuggestedPages(6)).isEqualTo(new int[] {0}); }
@Test public void testGenerateSuggestedPages_linear() { Pagination p = new Pagination(50,23,10); Assertions.assertThat(p.generateSuggestedPages(6)).isEqualTo(new int[] {0,1,2,3,4}); }
@Test public void testGenerateSuggestedPages_fanRight() { Pagination p = new Pagination(500,21,10); Assertions.assertThat(p.generateSuggestedPages(10)).isEqualTo(new int[] {0,1,2,3,4,8,14,23,34,49}); }
@Test public void testGenerateSuggestedPages_fanLeft() { Pagination p = new Pagination(500,475,10); Assertions.assertThat(p.generateSuggestedPages(10)).isEqualTo(new int[] {0,15,26,35,41,45,46,47,48,49}); }
@Test public void testGenerateSuggestedPages_fanLeftAndRight() { Pagination p = new Pagination(500,250,10); Assertions.assertThat(p.generateSuggestedPages(10)).isEqualTo(new int[] {0,11,18,23,24,25,26,28,36,49}); }
@Test public void testGenerateSuggestedPages_general_1() { Pagination p = new Pagination(168,120,15); Assertions.assertThat(p.generateSuggestedPages(9)).isEqualTo(new int[] {0,4,5,6,7,8,9,10,11}); }
@Test public void testGenerateSuggestedPages_general_2() { Pagination p = new Pagination(168,75,15); Assertions.assertThat(p.generateSuggestedPages(9)).isEqualTo(new int[] {0,2,3,4,5,6,7,8,11}); }