Add overall count to collection results

This commit is contained in:
René Pfeuffer
2018-06-22 10:15:35 +02:00
parent 2daad8aab7
commit 9ec3833a97
8 changed files with 68 additions and 57 deletions

View File

@@ -33,8 +33,6 @@
package sonia.scm;
//~--- JDK imports ------------------------------------------------------------
import sonia.scm.util.Util;
import java.io.IOException;
@@ -130,6 +128,8 @@ public interface Manager<T extends ModelObject, E extends Exception>
* Returns objects from the store divided into pages with the given page
* size for the given page number (zero based) and sorted by the given
* {@link java.util.Comparator}.
* <p>This default implementation reads all items, first, so you might want to adapt this
* whenever reading is expensive!</p>
*
* @param comparator to sort the returned objects
* @param pageNumber the number of the page to be returned (zero based)
@@ -144,9 +144,11 @@ public interface Manager<T extends ModelObject, E extends Exception>
checkArgument(pageSize > 0, "pageSize must be at least 1");
checkArgument(pageNumber >= 0, "pageNumber must be non-negative");
Collection<T> entities = getAll(comparator, pageNumber * pageSize, pageSize + 1);
boolean hasMore = entities.size() > pageSize;
return new PageResult<>(Util.createSubCollection(entities, 0, pageSize), hasMore);
Collection<T> allEntities = getAll(comparator);
Collection<T> pagedEntities = Util.createSubCollection(allEntities, pageNumber * pageSize, pageSize);
return new PageResult<>(pagedEntities, allEntities.size());
}
}

View File

@@ -5,16 +5,16 @@ import java.util.Collections;
/**
* This represents the result of a page request. Contains the results for
* the page and a flag whether there are more pages or not.
* the page and the overall count of all elements.
*/
public class PageResult<T extends ModelObject> {
private final Collection<T> entities;
private final boolean hasMore;
private final int overallCount;
public PageResult(Collection<T> entities, boolean hasMore) {
public PageResult(Collection<T> entities, int overallCount) {
this.entities = entities;
this.hasMore = hasMore;
this.overallCount = overallCount;
}
/**
@@ -25,9 +25,9 @@ public class PageResult<T extends ModelObject> {
}
/**
* If this is <code>true</code>, there are more pages (that is, more entities).
* The overall count of all available elements.
*/
public boolean hasMore() {
return hasMore;
public int getOverallCount() {
return overallCount;
}
}