From 3135c8e68050130527cf965e2bfebfbc29bf290d Mon Sep 17 00:00:00 2001 From: Alec Henninger Date: Fri, 10 Apr 2015 09:43:02 -0400 Subject: [PATCH] Allow streaming pages and rows in Table APIs; renames --- .../darcy/ui/api/elements/PaginatedTable.java | 33 +++++++++++++++++-- .../redhat/darcy/ui/api/elements/Table.java | 11 ++++--- .../ui/api/elements/PaginatedTableTest.java | 8 ++--- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/redhat/darcy/ui/api/elements/PaginatedTable.java b/src/main/java/com/redhat/darcy/ui/api/elements/PaginatedTable.java index 9195f74..d7586aa 100644 --- a/src/main/java/com/redhat/darcy/ui/api/elements/PaginatedTable.java +++ b/src/main/java/com/redhat/darcy/ui/api/elements/PaginatedTable.java @@ -19,12 +19,20 @@ package com.redhat.darcy.ui.api.elements; +import static java.util.Spliterator.NONNULL; +import static java.util.Spliterator.ORDERED; +import static java.util.Spliterator.SORTED; + import java.util.Iterator; +import java.util.Objects; +import java.util.Spliterators; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; /** * A role interface for tables which show one of many possible pages at a time. */ -public interface PaginatedTable> extends Table { +public interface PaginatedTable> extends Table, Comparable { /** * @param page The specific page to navigate to, in a range from 1 to {@link #getMaxPages()}, * inclusive. @@ -62,7 +70,7 @@ public interface PaginatedTable> extends Table { * @return An iterable which will cause the table to navigate to the first page, and every page * thereafter. The iterator returns the table itself, navigated to the next page. */ - default Iterable ascendingPages() { + default Iterable pagesAscending() { return () -> new Iterator() { int cursor = 0; @@ -85,7 +93,7 @@ public T next() { * all tables will be able to navigate to the last page directly, and some may require first * paging through all of the pages ascending until the last page can be determined. */ - default Iterable descendingPages() { + default Iterable pagesDescending() { return () -> new Iterator() { int cursor = getMaxPages() + 1; @@ -102,6 +110,18 @@ public T next() { }; } + default Stream getPagesAscending() { + return StreamSupport.stream( + Spliterators.spliterator(pagesAscending().iterator(), getMaxPages(), + ORDERED | SORTED | NONNULL), false); + } + + default Stream getPagesDescending() { + return StreamSupport.stream( + Spliterators.spliterator(pagesDescending().iterator(), getMaxPages(), + ORDERED | SORTED | NONNULL), false); + } + /** * Note that not all table implementations will know all of their data at once, and so * calculating the total number of entries may take a considerable amount of time, and may even @@ -119,4 +139,11 @@ default int getMaxPages() { // inverse to avoid calculating the total entries before determining the row count. return (int) Math.ceil((double) getTotalEntries() * (1.0 / (double) getRowCount())); } + + @Override + default int compareTo(T o) { + Objects.requireNonNull(o); + + return getCurrentPage() - o.getCurrentPage(); + } } diff --git a/src/main/java/com/redhat/darcy/ui/api/elements/Table.java b/src/main/java/com/redhat/darcy/ui/api/elements/Table.java index 2f06bfd..c0c3c94 100644 --- a/src/main/java/com/redhat/darcy/ui/api/elements/Table.java +++ b/src/main/java/com/redhat/darcy/ui/api/elements/Table.java @@ -153,15 +153,18 @@ default U getHeader(Header header) { return header.getHeader((T) this); } + default Stream> getRows() { + return StreamSupport.stream( + Spliterators.spliterator(rows().iterator(), getRowCount(), + DISTINCT | ORDERED | SORTED | NONNULL), false); + } + /** * @return A filtered {@link java.util.stream.Stream} of the rows in this table where the * contents of a particular column match the specified {@link java.util.function.Predicate}. */ default Stream> getRowsWhere(Column column, Predicate predicate) { - return StreamSupport.stream( - Spliterators.spliterator(rows().iterator(), getRowCount(), - DISTINCT | ORDERED | SORTED | NONNULL), false) - .filter(r -> predicate.test(r.getCell(column))); + return getRows().filter(r -> predicate.test(r.getCell(column))); } /** diff --git a/src/test/java/com/redhat/darcy/ui/api/elements/PaginatedTableTest.java b/src/test/java/com/redhat/darcy/ui/api/elements/PaginatedTableTest.java index 9b06760..b2d32e3 100644 --- a/src/test/java/com/redhat/darcy/ui/api/elements/PaginatedTableTest.java +++ b/src/test/java/com/redhat/darcy/ui/api/elements/PaginatedTableTest.java @@ -40,7 +40,7 @@ public void shouldIterateThroughPagesAscending() { int curPage = 0; - for (FakePaginatedTable table : testTable.ascendingPages()) { + for (FakePaginatedTable table : testTable.pagesAscending()) { curPage++; assertThat(table.getCurrentPage(), equalTo(curPage)); @@ -58,7 +58,7 @@ public void shouldStartAtFirstPageWhenIteratingAscending() { testTable.toPage(5); - testTable.ascendingPages().iterator().next(); + testTable.pagesAscending().iterator().next(); assertThat(testTable.getCurrentPage(), equalTo(1)); } @@ -72,7 +72,7 @@ public void shouldIterateThroughPagesDescending() { int curPage = 11; testTable.toPage(10); - for (FakePaginatedTable table : testTable.descendingPages()) { + for (FakePaginatedTable table : testTable.pagesDescending()) { curPage--; assertThat(table.getCurrentPage(), equalTo(curPage)); @@ -88,7 +88,7 @@ public void shouldStartAtLastPageWhenIteratingDescending() { .totalEntries(95) .build(); - testTable.descendingPages().iterator().next(); + testTable.pagesDescending().iterator().next(); assertThat(testTable.getCurrentPage(), equalTo(10)); }