Package com.mewna.catnip.cache.view
Interface CacheView<T>
- Type Parameters:
T
- Type of the entity held by this cache.
- All Superinterfaces:
Iterable<T>
- All Known Subinterfaces:
MutableCacheView<T>
,MutableNamedCacheView<T>
,NamedCacheView<T>
- All Known Implementing Classes:
CompositeCacheView
,CompositeNamedCacheView
,DefaultCacheView
,DefaultNamedCacheView
,NoopCacheView
Represents a view of a given cache. When the cache is updated, the view is also updated.
This interface represents a low overhead API of reading the cache, without exposing methods
that may modify it, possibly leading to an inconsistent state.
- Since:
- 12/15/18
- Author:
- natanbc
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns whether all elements of this cache match the provided predicate.boolean
Returns whether any elements of this cache match the provided predicate.<R> R
collect
(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) Performs a mutable reduction operation on the elements of this cache.<A,
R> R Performs a mutable reduction operation on the elements of this cache using aCollector
.long
Returns the amount of elements that match the provided predicate.Returns all elements in this cache that matches the given filter.<C extends Collection<T>>
CReturns all elements in this cache that matches the given filter.Returns any element in this cache that matches the given filter.void
Iterates this view, providing all elements to the given consumer.getById
(long id) default T
boolean
isEmpty()
keys()
max
(Comparator<? super T> comparator) Returns the maximum element of this cache according to the providedComparator
.min
(Comparator<? super T> comparator) Returns the minimum element of this cache according to the providedComparator
.boolean
Returns whether no elements of this cache match the provided predicate.static <T> MutableNamedCacheView<T>
noop()
reduce
(BinaryOperator<T> accumulator) Performs a reduction on the elements of this cache, using an associative accumulation function, and returns anOptional
describing the reduced value, if any.reduce
(T identity, BinaryOperator<T> accumulator) Performs a reduction on the elements of this cache, using the provided identity value and an associative accumulation function, and returns the reduced value.<U> U
reduce
(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) Performs a reduction on the elements of this cache, using the provided identity, accumulation and combining functions.long
size()
snapshot()
<C extends Collection<T>>
Cdefault Spliterator<T>
stream()
values()
-
Method Details
-
noop
- Returns:
- A noop cache view. All mutation methods are noop. Always empty.
-
forEach
Iterates this view, providing all elements to the given consumer. -
size
@Nonnegative long size()- Returns:
- The size of this cache.
-
isEmpty
boolean isEmpty()- Returns:
- Whether or not this view is empty. Equivalent to
size() == 0
.
-
getById
- Parameters:
id
- ID of the entity to fetch.- Returns:
- The element with the provided ID, or
null
if it isn't cached.
-
getById
- Parameters:
id
- ID of the entity to fetch.- Returns:
- The element with the provided ID, or
null
if it isn't cached.
-
findAny
Returns any element in this cache that matches the given filter. There are no order guarantees if multiple elements match. Use with caution.- Parameters:
filter
- Filter to find matching elements.- Returns:
- Any element that matches the provided filter, or
null
if none match.
-
find
Returns all elements in this cache that matches the given filter. There are no order guarantees if multiple elements match.- Parameters:
filter
- Filter to find matching elements.- Returns:
- A collection with all the matching elements. May be empty.
-
find
@Nonnull <C extends Collection<T>> C find(@Nonnull Predicate<? super T> filter, @Nonnull Supplier<C> supplier) Returns all elements in this cache that matches the given filter. There are no order guarantees if multiple elements match.- Parameters:
filter
- Filter to find matching elements.supplier
- Supplier for the collection to add the elements to. The returned collection must be mutable.- Returns:
- The collection returned by
supplier
, after adding the matching elements. May be empty.
-
collect
Performs a mutable reduction operation on the elements of this cache using aCollector
.- Type Parameters:
R
- The type of the result.A
- The intermediate accumulation type of theCollector
.- Parameters:
collector
- TheCollector
describing the reduction.- Returns:
- The result of the reduction.
- See Also:
-
collect
@Nonnull <R> R collect(@Nonnull Supplier<R> supplier, @Nonnull BiConsumer<R, ? super T> accumulator, @Nonnull BiConsumer<R, R> combiner) Performs a mutable reduction operation on the elements of this cache.- Type Parameters:
R
- The type of the result.- Parameters:
supplier
- A function that creates a new result container. For a parallel execution, this function may be called multiple times and must return a fresh value each time.accumulator
- An associative, non-interfering, stateless function for incorporating an additional element into a result.combiner
- An associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function.- Returns:
- The result of the reduction.
- See Also:
-
reduce
@Nonnull <U> U reduce(U identity, @Nonnull BiFunction<U, ? super T, U> accumulator, @Nonnull BinaryOperator<U> combiner) Performs a reduction on the elements of this cache, using the provided identity, accumulation and combining functions.- Type Parameters:
U
- The type of the result.- Parameters:
identity
- The identity value for the combiner function.accumulator
- An associative, non-interfering, stateless function for incorporating an additional element into a result.combiner
- An associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function- Returns:
- the result of the reduction
- See Also:
-
reduce
Performs a reduction on the elements of this cache, using an associative accumulation function, and returns anOptional
describing the reduced value, if any.- Parameters:
accumulator
- An associative, non-interfering, stateless function for combining two values.- Returns:
- An
Optional
describing the result of the reduction. - Throws:
NullPointerException
- If the result of the reduction is null.- See Also:
-
reduce
Performs a reduction on the elements of this cache, using the provided identity value and an associative accumulation function, and returns the reduced value.- Parameters:
identity
- The identity value for the accumulating function.accumulator
- An associative, non-interfering, stateless function for combining two values.- Returns:
- the result of the reduction
- See Also:
-
anyMatch
Returns whether any elements of this cache match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the cache is empty thenfalse
is returned and the predicate is not evaluated.- Parameters:
predicate
- A non-interfering, stateless predicate to apply to elements of this cache.- Returns:
true
if any elements of the cache match the provided predicate, otherwisefalse
.- See Also:
-
allMatch
Returns whether all elements of this cache match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the cache is empty thentrue
is returned and the predicate is not evaluated.- Parameters:
predicate
- A non-interfering, stateless predicate to apply to elements of this cache.- Returns:
true
if either all elements of the cache match the provided predicate or the cache is empty, otherwisefalse
.- See Also:
-
noneMatch
Returns whether no elements of this cache match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the cache is empty thentrue
is returned and the predicate is not evaluated.- Parameters:
predicate
- A non-interfering, stateless predicate to apply to elements of this cache.- Returns:
true
if either no elements of the cache match the provided predicate or the cache is empty, otherwisefalse
.- See Also:
-
min
Returns the minimum element of this cache according to the providedComparator
. This is a special case of a reduction.- Parameters:
comparator
- A non-interfering, statelessComparator
to compare elements of this stream.- Returns:
- An
Optional
describing the minimum element of this cache, or an emptyOptional
if the cache is empty. - Throws:
NullPointerException
- If the minimum element is null.- See Also:
-
max
Returns the maximum element of this cache according to the providedComparator
. This is a special case of a reduction.- Parameters:
comparator
- A non-interfering, statelessComparator
to compare elements of this stream.- Returns:
- An
Optional
describing the maximum element of this cache, or an emptyOptional
if the cache is empty. - Throws:
NullPointerException
- If the maximum element is null.- See Also:
-
count
Returns the amount of elements that match the provided predicate.- Parameters:
filter
- Filter to find matching elements.- Returns:
- Amount of matching elements.
-
keys
- Returns:
- A view of all the keys in this cache. Updated if this cache is modified.
- See Also:
-
values
- Returns:
- A view of all the values in this cache. Updated if this cache is modified.
- See Also:
-
snapshot
- Returns:
- A snapshot of all the values in this cache. Not updated if this cache is modified.
- See Also:
-
snapshot
- Parameters:
supplier
- Supplier for the collection to add the elements to. The returned collection must be mutable.- Returns:
- The collection returned by
supplier
, after adding the cached elements. May be empty. - See Also:
-
stream
- Returns:
- A stream with the elements cached, in no specific order.
- See Also:
-
spliterator
- Specified by:
spliterator
in interfaceIterable<T>
-