diff --git a/src/main/java/io/ebean/EmptyPagedList.java b/src/main/java/io/ebean/EmptyPagedList.java new file mode 100644 index 000000000..bbf72a731 --- /dev/null +++ b/src/main/java/io/ebean/EmptyPagedList.java @@ -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. + *

+ * For use in application code when we need to return a PagedList but don't want to + * execute a query. + *

+ * + *
{@code
+ *
+ *   PagedList empty = PagedList.emptyList();
+ *
+ * }
+ */ +public class EmptyPagedList implements PagedList { + + @Override + public void loadCount() { + // do nothing + } + + @Nonnull + @Override + public Future getFutureCount() { + return null; + } + + @Nonnull + @Override + public List 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 ""; + } +} diff --git a/src/main/java/io/ebean/PagedList.java b/src/main/java/io/ebean/PagedList.java index 6b0a92ef1..7e84ff351 100644 --- a/src/main/java/io/ebean/PagedList.java +++ b/src/main/java/io/ebean/PagedList.java @@ -60,6 +60,13 @@ import java.util.concurrent.Future; */ public interface PagedList { + /** + * Return an empty PagedList. + */ + static PagedList emptyList() { + return new EmptyPagedList<>(); + } + /** * Initiate the loading of the total row count in the background. *
{@code
diff --git a/src/test/java/org/tests/query/TestQueryFindPagedList.java b/src/test/java/org/tests/query/TestQueryFindPagedList.java
index 2376fefda..8fe837988 100644
--- a/src/test/java/org/tests/query/TestQueryFindPagedList.java
+++ b/src/test/java/org/tests/query/TestQueryFindPagedList.java
@@ -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 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 {