java.lang.Object
tech.guilhermekaua.spigotboot.inventoryapi.pagination.source.EagerPageSource<T>
All Implemented Interfaces:
PageSource<T>

public final class EagerPageSource<T> extends Object implements PageSource<T>
In-memory PageSource: serves slices of a fixed list and settles every request synchronously on the calling thread. Totals are always known, loading is never observable and requests never fail.
  • Constructor Details

    • EagerPageSource

      public EagerPageSource(List<T> elements)
      Creates an eager source over a defensive copy of the given list.
      Parameters:
      elements - the backing elements, not null
      Throws:
      NullPointerException - if elements is null
  • Method Details

    • empty

      public static <T> EagerPageSource<T> empty()
      Type Parameters:
      T - the element type
      Returns:
      an eager source over an empty list
    • request

      public void request(PageRequest request, BiConsumer<PageResult<T>,Throwable> onSettle)
      Description copied from interface: PageSource
      Requests the page described by request.

      Contract for implementations:

      • onSettle is invoked at most once per request, with exactly one of result/error non-null — and possibly never (a superseded request).
      • It MAY be invoked synchronously inside this method and MAY be invoked on any thread.
      • A request superseded by a newer one MUST NOT be settled.
      • PageSource.isLoading() must return false once the latest request settled.
      Specified by:
      request in interface PageSource<T>
      Parameters:
      request - the page to load, never null
      onSettle - receives the result or the failure, never null
    • totalElements

      public int totalElements()
      Specified by:
      totalElements in interface PageSource<T>
      Returns:
      the total number of elements in the backing store; 0 until known for async sources
    • totalsKnown

      public boolean totalsKnown()
      Specified by:
      totalsKnown in interface PageSource<T>
      Returns:
      true once PageSource.totalElements() reflects the real store size — always for eager sources, after the first successful settle for async sources
    • isLoading

      public boolean isLoading()
      Specified by:
      isLoading in interface PageSource<T>
      Returns:
      true while the latest request has not settled
    • lastError

      public Throwable lastError()
      Specified by:
      lastError in interface PageSource<T>
      Returns:
      the failure of the most recently settled request, or null; cleared when a new request is dispatched
    • elements

      public List<T> elements()
      Specified by:
      elements in interface PageSource<T>
      Returns:
      the elements currently held locally — the full backing list for eager sources, the items of the most recently applied page for async sources