@Test public void pipelinesPagingAndSorting() { Pipeline pipe1 = mock(Pipeline.class); when(pipe1.getName()).thenReturn("pipeline 1"); Pipeline pipe2 = mock(Pipeline.class); when(pipe2.getName()).thenReturn("pipeline 2"); Pipeline pipe3 = mock(Pipeline.class); when(pipe3.getName()).thenReturn("pipeline 3"); Pipeline pipe4 = mock(Pipeline.class); when(pipe4.getName()).thenReturn("pipeline 4"); Pipeline pipe5 = mock(Pipeline.class); when(pipe5.getName()).thenReturn("pipeline 5"); Collection<Pipeline> pipes = new ArrayList<>(); pipes.add(pipe1); pipes.add(pipe2); pipes.add(pipe3); pipes.add(pipe4); pipes.add(pipe5); String sort = "name"; List<Pipeline> pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 0, 2, sort, true); assertEquals(2, pageSort.size()); assertEquals("pipeline 1", pageSort.get(0).getName()); assertEquals("pipeline 2", pageSort.get(1).getName()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 0, 2, sort, false); assertEquals(2, pageSort.size()); assertEquals("pipeline 2", pageSort.get(0).getName()); assertEquals("pipeline 1", pageSort.get(1).getName()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 1, 2, sort, true); assertEquals(2, pageSort.size()); assertEquals("pipeline 3", pageSort.get(0).getName()); assertEquals("pipeline 4", pageSort.get(1).getName()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 1, 2, sort, false); assertEquals(2, pageSort.size()); assertEquals("pipeline 4", pageSort.get(0).getName()); assertEquals("pipeline 3", pageSort.get(1).getName()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 2, 2, sort, false); assertEquals(1, pageSort.size()); assertEquals("pipeline 5", pageSort.get(0).getName()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 3, 2, sort, false); assertEquals(0, pageSort.size()); pageSort = PageSortUtil.pageSort(pipes, (Pipeline p1, Pipeline p2) -> { switch (sort) { case "name": return p1.getName().compareTo(p2.getName()); default: return p1.toString().compareTo(p2.toString()); } }, 0, 10, "", false); assertEquals(5, pageSort.size()); }
public static <T> List<T> pageSort(Collection<T> values, Comparator<T> comparator, Integer page, Integer pageSize, String sort, boolean sortOrder) { if (page < 0) { throw new IllegalStateException("Page must be greater or equals than 0"); } if (pageSize < 1) { throw new IllegalStateException("PageSize must be greater than 0"); } if (page == 0 && values.size() <= pageSize) { List<T> result = new ArrayList<>(values); sort(result, comparator, sort, sortOrder); return result; } if (values.size() <= pageSize * page) { return Collections.emptyList(); } List<T> result = new ArrayList<>(pageSize); List<T> allValues = new ArrayList<>(values); for (int i = page * pageSize; i < (page * pageSize) + pageSize; i++) { if (allValues.size() > i) { result.add(allValues.get(i)); } } sort(result, comparator, sort, sortOrder); return result; }
PageSortUtil { public static <T> List<T> pageSort(Collection<T> values, Comparator<T> comparator, Integer page, Integer pageSize, String sort, boolean sortOrder) { if (page < 0) { throw new IllegalStateException("Page must be greater or equals than 0"); } if (pageSize < 1) { throw new IllegalStateException("PageSize must be greater than 0"); } if (page == 0 && values.size() <= pageSize) { List<T> result = new ArrayList<>(values); sort(result, comparator, sort, sortOrder); return result; } if (values.size() <= pageSize * page) { return Collections.emptyList(); } List<T> result = new ArrayList<>(pageSize); List<T> allValues = new ArrayList<>(values); for (int i = page * pageSize; i < (page * pageSize) + pageSize; i++) { if (allValues.size() > i) { result.add(allValues.get(i)); } } sort(result, comparator, sort, sortOrder); return result; } }
PageSortUtil { public static <T> List<T> pageSort(Collection<T> values, Comparator<T> comparator, Integer page, Integer pageSize, String sort, boolean sortOrder) { if (page < 0) { throw new IllegalStateException("Page must be greater or equals than 0"); } if (pageSize < 1) { throw new IllegalStateException("PageSize must be greater than 0"); } if (page == 0 && values.size() <= pageSize) { List<T> result = new ArrayList<>(values); sort(result, comparator, sort, sortOrder); return result; } if (values.size() <= pageSize * page) { return Collections.emptyList(); } List<T> result = new ArrayList<>(pageSize); List<T> allValues = new ArrayList<>(values); for (int i = page * pageSize; i < (page * pageSize) + pageSize; i++) { if (allValues.size() > i) { result.add(allValues.get(i)); } } sort(result, comparator, sort, sortOrder); return result; }  }
PageSortUtil { public static <T> List<T> pageSort(Collection<T> values, Comparator<T> comparator, Integer page, Integer pageSize, String sort, boolean sortOrder) { if (page < 0) { throw new IllegalStateException("Page must be greater or equals than 0"); } if (pageSize < 1) { throw new IllegalStateException("PageSize must be greater than 0"); } if (page == 0 && values.size() <= pageSize) { List<T> result = new ArrayList<>(values); sort(result, comparator, sort, sortOrder); return result; } if (values.size() <= pageSize * page) { return Collections.emptyList(); } List<T> result = new ArrayList<>(pageSize); List<T> allValues = new ArrayList<>(values); for (int i = page * pageSize; i < (page * pageSize) + pageSize; i++) { if (allValues.size() > i) { result.add(allValues.get(i)); } } sort(result, comparator, sort, sortOrder); return result; }  static List<T> pageSort(Collection<T> values,
                                       Comparator<T> comparator,
                                       Integer page,
                                       Integer pageSize,
                                       String sort,
                                       boolean sortOrder); }
PageSortUtil { public static <T> List<T> pageSort(Collection<T> values, Comparator<T> comparator, Integer page, Integer pageSize, String sort, boolean sortOrder) { if (page < 0) { throw new IllegalStateException("Page must be greater or equals than 0"); } if (pageSize < 1) { throw new IllegalStateException("PageSize must be greater than 0"); } if (page == 0 && values.size() <= pageSize) { List<T> result = new ArrayList<>(values); sort(result, comparator, sort, sortOrder); return result; } if (values.size() <= pageSize * page) { return Collections.emptyList(); } List<T> result = new ArrayList<>(pageSize); List<T> allValues = new ArrayList<>(values); for (int i = page * pageSize; i < (page * pageSize) + pageSize; i++) { if (allValues.size() > i) { result.add(allValues.get(i)); } } sort(result, comparator, sort, sortOrder); return result; }  static List<T> pageSort(Collection<T> values,
                                       Comparator<T> comparator,
                                       Integer page,
                                       Integer pageSize,
                                       String sort,
                                       boolean sortOrder);  }