summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util
diff options
context:
space:
mode:
authorYoung Gyu Park <younggyu@google.com>2018-04-12 17:47:04 +0900
committerYoung Gyu Park <younggyu@google.com>2018-04-16 09:41:16 +0900
commita0698d6c8f7e91e5067a82a1c16884fcc534e681 (patch)
tree3f57cefdd7030e22fd1b0eea2786769f4ad2be19 /src/main/java/com/android/vts/util
parentd3fe7f44e6d49e3a423b423c44b1625c771fe995 (diff)
downloaddashboard-a0698d6c8f7e91e5067a82a1c16884fcc534e681.tar.gz
Implementing pagination
Test: go/vts-web-staging/show_plan_release?plan=vts&type=suite&page=1 Bug: 77456536 Change-Id: I0e9c7ad04c8c832f4229d77c2840be813869d293
Diffstat (limited to 'src/main/java/com/android/vts/util')
-rw-r--r--src/main/java/com/android/vts/util/Pagination.java108
1 files changed, 78 insertions, 30 deletions
diff --git a/src/main/java/com/android/vts/util/Pagination.java b/src/main/java/com/android/vts/util/Pagination.java
index c8bec0f..6223c35 100644
--- a/src/main/java/com/android/vts/util/Pagination.java
+++ b/src/main/java/com/android/vts/util/Pagination.java
@@ -22,7 +22,9 @@ import com.googlecode.objectify.cmd.Query;
import java.util.ArrayList;
import java.util.Iterator;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.stream.Collectors;
/** Helper class for pagination. */
public class Pagination<T> implements Iterable<T> {
@@ -41,9 +43,18 @@ public class Pagination<T> implements Iterable<T> {
/** the total number of found entities */
private int totalCount;
- /** the cursor string token where to start */
+ /** the previous cursor string token where to start */
+ private String previousPageCountToken = "";
+
+ /** the previous cursor string token where to start */
+ private String currentPageCountToken = "";
+
+ /** the next cursor string token where to start */
private String nextPageCountToken = "";
+ /** the previous cursor string token list where to start */
+ private LinkedHashSet<String> pageCountTokenSet;
+
/** the maximum number of pages */
private int maxPages;
@@ -57,33 +68,53 @@ public class Pagination<T> implements Iterable<T> {
this.totalCount = totalCount;
}
- public Pagination(Query<T> query, int page, int pageSize, String nextPageToken) {
+ public Pagination(
+ Query<T> query,
+ int page,
+ int pageSize,
+ String startPageToken,
+ LinkedHashSet<String> pageCountTokenSet) {
this.page = page;
this.pageSize = pageSize;
+ this.pageCountTokenSet = pageCountTokenSet;
+ this.currentPageCountToken = startPageToken;
+
+ List<String> pageCountTokenList =
+ this.pageCountTokenSet.stream().collect(Collectors.toList());
+ if (pageCountTokenList.size() > 1) {
+ int foundIndex = pageCountTokenList.indexOf(startPageToken);
+ if (foundIndex <= 0) {
+ this.previousPageCountToken = "";
+ } else {
+ this.previousPageCountToken = pageCountTokenList.get(foundIndex - 1);
+ }
+ }
- int limitValue = pageSize * DEFAULT_PAGE_WINDOW + pageSize;
+ int limitValue = pageSize * DEFAULT_PAGE_WINDOW + pageSize / 2;
query = query.limit(limitValue);
- if (nextPageToken.equals("")) {
+ if (startPageToken.equals("")) {
this.totalCount = query.count();
} else {
- query = query.startAt(Cursor.fromWebSafeString(nextPageToken));
+ query = query.startAt(Cursor.fromWebSafeString(startPageToken));
this.totalCount = query.count();
}
- this.maxPages = this.totalCount / this.pageSize + (this.totalCount % this.pageSize == 0 ? 0 : 1);
+ this.maxPages =
+ this.totalCount / this.pageSize + (this.totalCount % this.pageSize == 0 ? 0 : 1);
+
+ int iteratorIndex = 0;
+ int startIndex = (page % pageSize == 0 ? 1 : page % pageSize - 1) * pageSize;
QueryResultIterator<T> resultIterator = query.iterator();
- int iteratorIndex = 1;
- int startIndex = page / pageSize * pageSize;
while (resultIterator.hasNext()) {
if (startIndex <= iteratorIndex && iteratorIndex < startIndex + this.pageSize)
this.list.add(resultIterator.next());
- else
- resultIterator.next();
+ else resultIterator.next();
+ if (iteratorIndex == DEFAULT_PAGE_SIZE * this.pageSize) {
+ this.nextPageCountToken = resultIterator.getCursor().toWebSafeString();
+ }
iteratorIndex++;
}
-
- this.nextPageCountToken = resultIterator.getCursor().toWebSafeString();
}
public Iterator<T> iterator() {
@@ -93,7 +124,7 @@ public class Pagination<T> implements Iterable<T> {
/**
* Gets the total number of objects.
*
- * @return the total number of objects as an int
+ * @return the total number of objects as an int
*/
public int getTotalCount() {
return totalCount;
@@ -102,7 +133,7 @@ public class Pagination<T> implements Iterable<T> {
/**
* Gets the number of page window.
*
- * @return the number of page window as an int
+ * @return the number of page window as an int
*/
public int getPageSize() {
return pageSize;
@@ -111,7 +142,7 @@ public class Pagination<T> implements Iterable<T> {
/**
* Gets the maximum number of pages.
*
- * @return the maximum number of pages as an int
+ * @return the maximum number of pages as an int
*/
public int getMaxPages() {
return this.maxPages;
@@ -120,7 +151,7 @@ public class Pagination<T> implements Iterable<T> {
/**
* Gets the page.
*
- * @return the page as an int
+ * @return the page as an int
*/
public int getPage() {
return page;
@@ -129,49 +160,66 @@ public class Pagination<T> implements Iterable<T> {
/**
* Gets the minimum page in the window.
*
- * @return the page number
+ * @return the page number
*/
public int getMinPageRange() {
- if (this.getPage() < DEFAULT_PAGE_WINDOW) {
+ if (this.getPage() <= this.getPageSize()) {
return 1;
} else {
- return this.getPage() / DEFAULT_PAGE_WINDOW * DEFAULT_PAGE_WINDOW;
+ if ((this.getPage() % this.getPageSize()) == 0) {
+ return (this.getPage() / this.getPageSize() - 1) * this.getPageSize() + 1;
+ } else {
+ return this.getPage() / this.getPageSize() * this.getPageSize() + 1;
+ }
}
}
/**
* Gets the maximum page in the window.
*
- * @return the page number
+ * @return the page number
*/
public int getMaxPageRange() {
- if (this.getPage() < DEFAULT_PAGE_WINDOW) {
- return DEFAULT_PAGE_WINDOW;
+ if (this.getMaxPages() > this.getPageSize()) {
+ return getMinPageRange() + this.getPageSize() - 1;
} else {
- if (this.getMaxPages() > DEFAULT_PAGE_WINDOW) {
- return this.getPage() / DEFAULT_PAGE_WINDOW * DEFAULT_PAGE_WINDOW + this.pageSize;
- } else {
- return this.getMinPageRange() + this.getMaxPages();
- }
+ return this.getMinPageRange() + this.getMaxPages() - 1;
}
}
/**
* Gets the subset of the list for the current page.
*
- * @return a List
+ * @return a List
*/
public List<T> getList() {
return this.list;
}
/**
+ * Gets the cursor token for the previous page starting point.
+ *
+ * @return a string of cursor of previous starting point
+ */
+ public String getPreviousPageCountToken() {
+ return this.previousPageCountToken;
+ }
+
+ /**
+ * Gets the cursor token for the current page starting point.
+ *
+ * @return a string of cursor of current starting point
+ */
+ public String getCurrentPageCountToken() {
+ return this.currentPageCountToken;
+ }
+
+ /**
* Gets the cursor token for the next page starting point.
*
- * @return a string of cursor of next starting point
+ * @return a string of cursor of next starting point
*/
public String getNextPageCountToken() {
return this.nextPageCountToken;
}
-
}