@Test public void downwardsToFirstPageIsSameAsStartingFromFirstPage() throws Exception { final PaginatedList<SubjectReference> paginatedList = getPaginatedList( Stream.of( getObject("c2", "http: getObject("c1", "http: ), MAKE_ITEM, PaginationArguments.create(2, "D\nc3", Optional.empty()), Optional.empty() ); assertThat(decode(paginatedList.getNextCursor()), is("A\nc2")); assertThat(decode(paginatedList.getPrevCursor()), is(nullValue())); assertThat(paginatedList.getItems().size(), is(2)); assertThat(paginatedList.getItems().get(0).getSubjectUri(), is("http: assertThat(paginatedList.getItems().get(1).getSubjectUri(), is("http: }
static <U extends DatabaseResult> PaginatedList<U> getPaginatedList(Stream<CursorQuad> subjectStream, Function<CursorQuad, U> makeItem, PaginationArguments arguments, Optional<Long> total) { String[] cursors = new String[3]; int count = arguments.getCount(); if (count < 0 || count > MAX_COUNT) { count = MAX_COUNT; } count += 1; List<U> subjects = subjectStream .limit(count) .peek(cs -> { if (cursors[0] == null) { cursors[0] = cs.getCursor(); } cursors[1] = cursors[2]; cursors[2] = cs.getCursor(); }) .map(makeItem) .collect(Collectors.toList()); if (subjects.isEmpty()) { return PaginatedList.create( null, null, subjects, total ); } else { String prevCursor; String nextCursor; final List<U> items; if (subjects.size() == count) { items = subjects.subList(0, count - 1); } else { items = subjects; } if (arguments.getCursor().startsWith("D") || arguments.getCursor().equals("LAST")) { Collections.reverse(items); prevCursor = subjects.size() == count ? "D\n" + cursors[1] : null; nextCursor = arguments.getCursor().equals("LAST") ? null : "A\n" + cursors[0]; } else { prevCursor = arguments.getCursor().equals("") ? null : "D\n" + cursors[0]; nextCursor = subjects.size() == count ? "A\n" + cursors[1] : null; } return PaginatedList.create( prevCursor, nextCursor, items, total ); } }
PaginationHelper { static <U extends DatabaseResult> PaginatedList<U> getPaginatedList(Stream<CursorQuad> subjectStream, Function<CursorQuad, U> makeItem, PaginationArguments arguments, Optional<Long> total) { String[] cursors = new String[3]; int count = arguments.getCount(); if (count < 0 || count > MAX_COUNT) { count = MAX_COUNT; } count += 1; List<U> subjects = subjectStream .limit(count) .peek(cs -> { if (cursors[0] == null) { cursors[0] = cs.getCursor(); } cursors[1] = cursors[2]; cursors[2] = cs.getCursor(); }) .map(makeItem) .collect(Collectors.toList()); if (subjects.isEmpty()) { return PaginatedList.create( null, null, subjects, total ); } else { String prevCursor; String nextCursor; final List<U> items; if (subjects.size() == count) { items = subjects.subList(0, count - 1); } else { items = subjects; } if (arguments.getCursor().startsWith("D") || arguments.getCursor().equals("LAST")) { Collections.reverse(items); prevCursor = subjects.size() == count ? "D\n" + cursors[1] : null; nextCursor = arguments.getCursor().equals("LAST") ? null : "A\n" + cursors[0]; } else { prevCursor = arguments.getCursor().equals("") ? null : "D\n" + cursors[0]; nextCursor = subjects.size() == count ? "A\n" + cursors[1] : null; } return PaginatedList.create( prevCursor, nextCursor, items, total ); } } }
PaginationHelper { static <U extends DatabaseResult> PaginatedList<U> getPaginatedList(Stream<CursorQuad> subjectStream, Function<CursorQuad, U> makeItem, PaginationArguments arguments, Optional<Long> total) { String[] cursors = new String[3]; int count = arguments.getCount(); if (count < 0 || count > MAX_COUNT) { count = MAX_COUNT; } count += 1; List<U> subjects = subjectStream .limit(count) .peek(cs -> { if (cursors[0] == null) { cursors[0] = cs.getCursor(); } cursors[1] = cursors[2]; cursors[2] = cs.getCursor(); }) .map(makeItem) .collect(Collectors.toList()); if (subjects.isEmpty()) { return PaginatedList.create( null, null, subjects, total ); } else { String prevCursor; String nextCursor; final List<U> items; if (subjects.size() == count) { items = subjects.subList(0, count - 1); } else { items = subjects; } if (arguments.getCursor().startsWith("D") || arguments.getCursor().equals("LAST")) { Collections.reverse(items); prevCursor = subjects.size() == count ? "D\n" + cursors[1] : null; nextCursor = arguments.getCursor().equals("LAST") ? null : "A\n" + cursors[0]; } else { prevCursor = arguments.getCursor().equals("") ? null : "D\n" + cursors[0]; nextCursor = subjects.size() == count ? "A\n" + cursors[1] : null; } return PaginatedList.create( prevCursor, nextCursor, items, total ); } }  }
PaginationHelper { static <U extends DatabaseResult> PaginatedList<U> getPaginatedList(Stream<CursorQuad> subjectStream, Function<CursorQuad, U> makeItem, PaginationArguments arguments, Optional<Long> total) { String[] cursors = new String[3]; int count = arguments.getCount(); if (count < 0 || count > MAX_COUNT) { count = MAX_COUNT; } count += 1; List<U> subjects = subjectStream .limit(count) .peek(cs -> { if (cursors[0] == null) { cursors[0] = cs.getCursor(); } cursors[1] = cursors[2]; cursors[2] = cs.getCursor(); }) .map(makeItem) .collect(Collectors.toList()); if (subjects.isEmpty()) { return PaginatedList.create( null, null, subjects, total ); } else { String prevCursor; String nextCursor; final List<U> items; if (subjects.size() == count) { items = subjects.subList(0, count - 1); } else { items = subjects; } if (arguments.getCursor().startsWith("D") || arguments.getCursor().equals("LAST")) { Collections.reverse(items); prevCursor = subjects.size() == count ? "D\n" + cursors[1] : null; nextCursor = arguments.getCursor().equals("LAST") ? null : "A\n" + cursors[0]; } else { prevCursor = arguments.getCursor().equals("") ? null : "D\n" + cursors[0]; nextCursor = subjects.size() == count ? "A\n" + cursors[1] : null; } return PaginatedList.create( prevCursor, nextCursor, items, total ); } }   }
PaginationHelper { static <U extends DatabaseResult> PaginatedList<U> getPaginatedList(Stream<CursorQuad> subjectStream, Function<CursorQuad, U> makeItem, PaginationArguments arguments, Optional<Long> total) { String[] cursors = new String[3]; int count = arguments.getCount(); if (count < 0 || count > MAX_COUNT) { count = MAX_COUNT; } count += 1; List<U> subjects = subjectStream .limit(count) .peek(cs -> { if (cursors[0] == null) { cursors[0] = cs.getCursor(); } cursors[1] = cursors[2]; cursors[2] = cs.getCursor(); }) .map(makeItem) .collect(Collectors.toList()); if (subjects.isEmpty()) { return PaginatedList.create( null, null, subjects, total ); } else { String prevCursor; String nextCursor; final List<U> items; if (subjects.size() == count) { items = subjects.subList(0, count - 1); } else { items = subjects; } if (arguments.getCursor().startsWith("D") || arguments.getCursor().equals("LAST")) { Collections.reverse(items); prevCursor = subjects.size() == count ? "D\n" + cursors[1] : null; nextCursor = arguments.getCursor().equals("LAST") ? null : "A\n" + cursors[0]; } else { prevCursor = arguments.getCursor().equals("") ? null : "D\n" + cursors[0]; nextCursor = subjects.size() == count ? "A\n" + cursors[1] : null; } return PaginatedList.create( prevCursor, nextCursor, items, total ); } }   static final int MAX_COUNT; }