AbstractMongoDao extends AbstractDao<M,P,K,I> { protected Results<M, Integer> findWithSkipLimitPagination(FindIterable<Document> iterable, int skip, int limit) { ImmutableList.Builder<M> builder = ImmutableList.builder(); M mostRecentObject = null; int foundCount = 0; for (Document doc : iterable.skip(skip).limit(limit+1)) { if (mostRecentObject != null) { builder.add(mostRecentObject); } mostRecentObject = modelMapper.toObject(doc); foundCount++; } if (foundCount <= 0) { return Results.of(builder.build()); } else if (foundCount <= limit) { builder.add(mostRecentObject); return Results.of(builder.build()); } else { return Results.of(builder.build(), skip+limit); } } AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection, ObjectMapper objectMapper, ObjectMapper extendedJsonMapper);  AbstractMongoDao(Class<M> modelClass, Class<P> partialClass, MongoCollection<Document> collection,
                            DocumentMapper<M> modelMapper, DocumentMapper<P> partialMapper); MongoCollection<Document> getCollection(); String getCollectionName(); DocumentMapper<M> getModelMapper(); DocumentMapper<P> getPartialMapper(); @Override Optional<M> loadOpt(K id); Optional<P> loadOpt(K id, Iterable<String> fields); @Override Iterable<M> iterateAll(); @Override M create(P partial, Instant createAt, I createBy); @Override void update(K id, Update<P> update, Instant updateAt, I updateBy); @Override M updateAndReturn(K id, Update<P> update, Instant updateAt, I updateBy); @Override void delete(K id); @Override void initTable(); @Override void dropTable(); List<IndexModel> getIndexes(); Update<P> updateOf(P object); Update<P> updateOf(P object, Iterable<String> removeFields); static final String ID_FIELD; }
@Test public void testFindWithSkipLimitPagination() throws Exception { for (int i=0; i < 100; i++) { dao.create(TestUser.builder().name("Bob " + String.format("%02d", i)).active(true).build()); } Document query = new Document("active", true); Document sort = new Document("name", 1); Results<TestUser,Integer> page1 = dao.findWithSkipLimitPagination(dao.getCollection().find(query).sort(sort), 0, 50); assertEquals(50, page1.getData().size()); assertEquals("Bob 00", page1.getData().get(0).getName()); assertEquals("Bob 49", page1.getData().get(49).getName()); assertEquals(new Integer(50), page1.getNext().get()); Results<TestUser,Integer> page2 = dao.findWithSkipLimitPagination(dao.getCollection().find(query).sort(sort), 50, 50); assertEquals(50, page2.getData().size()); assertEquals("Bob 50", page2.getData().get(0).getName()); assertEquals("Bob 99", page2.getData().get(49).getName()); assertFalse(page2.getNext().isPresent()); page2 = dao.findWithSkipLimitPagination(dao.getCollection().find(query).sort(sort), 50, 1000); assertEquals(50, page2.getData().size()); assertEquals("Bob 50", page2.getData().get(0).getName()); assertEquals("Bob 99", page2.getData().get(49).getName()); assertFalse(page2.getNext().isPresent()); Results<TestUser,Integer> nomatch = dao.findWithSkipLimitPagination(dao.getCollection().find(query).sort(sort), 100, 0); assertEquals(0, nomatch.getData().size()); assertFalse(nomatch.getNext().isPresent()); }