Class AsyncPageSource<T>
java.lang.Object
tech.guilhermekaua.spigotboot.inventoryapi.pagination.source.AsyncPageSource<T>
- All Implemented Interfaces:
PageSource<T>
Asynchronous
PageSource backed by an AsyncPageSupplier. Owns all async
machinery: monotonically increasing request ids with at-most-once settle semantics, stale
response discarding, optional request timeouts and an optional TTL-bounded page cache.
Thread-safety: every state transition happens inside one internal lock; the id check and the
state write are atomic. Asynchronously completed settles are routed through the configured
SettleDispatcher; synchronous settles (cache hits, immediate failures) run on the
calling thread.
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncPageSource(AsyncPageSupplier<T> supplier, PaginationErrorCallback errorCallback, Duration requestTimeout, Duration cacheTtl, int cacheMaxPages, SettleDispatcher settleDispatcher) Creates an async page source. -
Method Summary
Modifier and TypeMethodDescriptionelements()voidClears any cached pages.booleanvoidrequest(PageRequest request, BiConsumer<PageResult<T>, Throwable> onSettle) Requests the page described byrequest.static voidShuts the shared timeout scheduler down and clears it so the next timeout-bearing request lazily recreates a fresh one.intboolean
-
Constructor Details
-
AsyncPageSource
public AsyncPageSource(AsyncPageSupplier<T> supplier, PaginationErrorCallback errorCallback, Duration requestTimeout, Duration cacheTtl, int cacheMaxPages, SettleDispatcher settleDispatcher) Creates an async page source.- Parameters:
supplier- loads pages, not nullerrorCallback- invoked on failed loads, may be nullrequestTimeout- per-request timeout, may be null to disablecacheTtl- cache entry freshness window, may be null to disable cachingcacheMaxPages- cache LRU bound, must be>= 1settleDispatcher- routes asynchronously completed settles, not null- Throws:
NullPointerException- ifsupplierorsettleDispatcheris nullIllegalArgumentException- ifcacheMaxPages < 1
-
-
Method Details
-
request
Description copied from interface:PageSourceRequests the page described byrequest.Contract for implementations:
onSettleis 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 returnfalseonce the latest request settled.
- Specified by:
requestin interfacePageSource<T>- Parameters:
request- the page to load, never nullonSettle- receives the result or the failure, never null
-
totalElements
public int totalElements()- Specified by:
totalElementsin interfacePageSource<T>- Returns:
- the total number of elements in the backing store;
0until known for async sources
-
totalsKnown
public boolean totalsKnown()- Specified by:
totalsKnownin interfacePageSource<T>- Returns:
trueoncePageSource.totalElements()reflects the real store size — always for eager sources, after the first successful settle for async sources
-
isLoading
public boolean isLoading()- Specified by:
isLoadingin interfacePageSource<T>- Returns:
truewhile the latest request has not settled
-
lastError
- Specified by:
lastErrorin interfacePageSource<T>- Returns:
- the failure of the most recently settled request, or
null; cleared when a new request is dispatched
-
elements
- Specified by:
elementsin interfacePageSource<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
-
invalidate
public void invalidate()Description copied from interface:PageSourceClears any cached pages. No-op by default.- Specified by:
invalidatein interfacePageSource<T>
-