Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow streaming pages and rows in Table APIs; renames #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main/java/com/redhat/darcy/ui/api/elements/PaginatedTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends PaginatedTable<T>> extends Table<T> {
public interface PaginatedTable<T extends PaginatedTable<T>> extends Table<T>, Comparable<T> {
/**
* @param page The specific page to navigate to, in a range from 1 to {@link #getMaxPages()},
* inclusive.
Expand Down Expand Up @@ -62,7 +70,7 @@ public interface PaginatedTable<T extends PaginatedTable<T>> extends Table<T> {
* @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<T> ascendingPages() {
default Iterable<T> pagesAscending() {
return () -> new Iterator<T>() {
int cursor = 0;

Expand All @@ -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<T> descendingPages() {
default Iterable<T> pagesDescending() {
return () -> new Iterator<T>() {
int cursor = getMaxPages() + 1;

Expand All @@ -102,6 +110,18 @@ public T next() {
};
}

default <U> Stream<T> getPagesAscending() {
return StreamSupport.stream(
Spliterators.spliterator(pagesAscending().iterator(), getMaxPages(),
ORDERED | SORTED | NONNULL), false);
}

default <U> Stream<T> 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
Expand All @@ -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();
}
}
11 changes: 7 additions & 4 deletions src/main/java/com/redhat/darcy/ui/api/elements/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ default <U> U getHeader(Header<T, U> header) {
return header.getHeader((T) this);
}

default Stream<Row<T>> 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 <U> Stream<Row<T>> getRowsWhere(Column<T, U> column, Predicate<? super U> 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)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -58,7 +58,7 @@ public void shouldStartAtFirstPageWhenIteratingAscending() {

testTable.toPage(5);

testTable.ascendingPages().iterator().next();
testTable.pagesAscending().iterator().next();

assertThat(testTable.getCurrentPage(), equalTo(1));
}
Expand All @@ -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));
Expand All @@ -88,7 +88,7 @@ public void shouldStartAtLastPageWhenIteratingDescending() {
.totalEntries(95)
.build();

testTable.descendingPages().iterator().next();
testTable.pagesDescending().iterator().next();

assertThat(testTable.getCurrentPage(), equalTo(10));
}
Expand Down