#1848 - ENH: Add EmptyPagedList ... as helper implementation of PagedList<T>

This commit is contained in:
rob bygrave
2019-10-18 17:22:19 +13:00
parent 7a974320ed
commit ecab219635
3 changed files with 95 additions and 0 deletions
@@ -0,0 +1,74 @@
package io.ebean;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;
/**
* An empty PagedList.
* <p>
* For use in application code when we need to return a PagedList but don't want to
* execute a query.
* </p>
*
* <pre>{@code
*
* PagedList<Customer> empty = PagedList.emptyList();
*
* }</pre>
*/
public class EmptyPagedList<T> implements PagedList<T> {
@Override
public void loadCount() {
// do nothing
}
@Nonnull
@Override
public Future<Integer> getFutureCount() {
return null;
}
@Nonnull
@Override
public List<T> getList() {
return Collections.emptyList();
}
@Override
public int getTotalCount() {
return 0;
}
@Override
public int getTotalPageCount() {
return 0;
}
@Override
public int getPageSize() {
return 0;
}
@Override
public int getPageIndex() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
@Override
public boolean hasPrev() {
return false;
}
@Override
public String getDisplayXtoYofZ(String to, String of) {
return "";
}
}
+7
View File
@@ -60,6 +60,13 @@ import java.util.concurrent.Future;
*/
public interface PagedList<T> {
/**
* Return an empty PagedList.
*/
static <B> PagedList<B> emptyList() {
return new EmptyPagedList<>();
}
/**
* Initiate the loading of the total row count in the background.
* <pre>{@code
@@ -5,6 +5,7 @@ import io.ebean.Ebean;
import io.ebean.PagedList;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.Order;
import org.tests.model.basic.ResetBasicData;
@@ -22,6 +23,19 @@ import static org.junit.Assert.assertTrue;
public class TestQueryFindPagedList extends BaseTestCase {
@Test
public void empty() {
PagedList<Customer> empty = PagedList.emptyList();
assertThat(empty.getList()).isEmpty();
assertThat(empty.getTotalPageCount()).isEqualTo(0);
assertThat(empty.getTotalCount()).isEqualTo(0);
assertThat(empty.getPageIndex()).isEqualTo(0);
assertThat(empty.getPageSize()).isEqualTo(0);
assertThat(empty.hasNext()).isFalse();
assertThat(empty.hasPrev()).isFalse();
assertThat(empty.getDisplayXtoYofZ("a", "b")).isEqualTo("");
}
@Test(expected = PersistenceException.class)
public void test_noMaxRows() throws ExecutionException, InterruptedException {