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

public interface CacheView<T> extends Iterable<T>
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 Type
    Method
    Description
    boolean
    allMatch(Predicate<? super T> predicate)
    Returns whether all elements of this cache match the provided predicate.
    boolean
    anyMatch(Predicate<? super T> predicate)
    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
    collect(Collector<? super T,A,R> collector)
    Performs a mutable reduction operation on the elements of this cache using a Collector.
    long
    count(Predicate<? super T> filter)
    Returns the amount of elements that match the provided predicate.
    find(Predicate<? super T> filter)
    Returns all elements in this cache that matches the given filter.
    <C extends Collection<T>>
    C
    find(Predicate<? super T> filter, Supplier<C> supplier)
    Returns all elements in this cache that matches the given filter.
    findAny(Predicate<? super T> filter)
    Returns any element in this cache that matches the given filter.
    void
    forEach(Consumer<? super T> action)
    Iterates this view, providing all elements to the given consumer.
    getById(long id)
     
    default T
     
    boolean
     
     
    max(Comparator<? super T> comparator)
    Returns the maximum element of this cache according to the provided Comparator.
    min(Comparator<? super T> comparator)
    Returns the minimum element of this cache according to the provided Comparator.
    boolean
    noneMatch(Predicate<? super T> predicate)
    Returns whether no elements of this cache match the provided predicate.
    static <T> MutableNamedCacheView<T>
     
    reduce(BinaryOperator<T> accumulator)
    Performs a reduction on the elements of this cache, using an associative accumulation function, and returns an Optional 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
     
     
    <C extends Collection<T>>
    C
    snapshot(Supplier<C> supplier)
     
    default Spliterator<T>
    default Stream<T>
     
     

    Methods inherited from interface java.lang.Iterable

    iterator
  • Method Details

    • noop

      @Nonnull static <T> MutableNamedCacheView<T> noop()
      Returns:
      A noop cache view. All mutation methods are noop. Always empty.
    • forEach

      void forEach(Consumer<? super T> action)
      Iterates this view, providing all elements to the given consumer.
      Specified by:
      forEach in interface Iterable<T>
      Parameters:
      action - Action to execute on each element.
    • 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

      T getById(long id)
      Parameters:
      id - ID of the entity to fetch.
      Returns:
      The element with the provided ID, or null if it isn't cached.
    • getById

      default T getById(@Nonnull String id)
      Parameters:
      id - ID of the entity to fetch.
      Returns:
      The element with the provided ID, or null if it isn't cached.
    • findAny

      T findAny(@Nonnull Predicate<? super T> filter)
      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

      @Nonnull Collection<T> find(@Nonnull Predicate<? super T> filter)
      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

      @Nonnull <A, R> R collect(@Nonnull Collector<? super T,A,R> collector)
      Performs a mutable reduction operation on the elements of this cache using a Collector.
      Type Parameters:
      R - The type of the result.
      A - The intermediate accumulation type of the Collector.
      Parameters:
      collector - The Collector 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

      @Nonnull Optional<T> reduce(@Nonnull BinaryOperator<T> accumulator)
      Performs a reduction on the elements of this cache, using an associative accumulation function, and returns an Optional 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

      @Nonnull T reduce(@Nonnull T identity, @Nonnull 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.
      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

      boolean anyMatch(@Nonnull Predicate<? super T> predicate)
      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 then false 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, otherwise false.
      See Also:
    • allMatch

      boolean allMatch(@Nonnull Predicate<? super T> predicate)
      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 then true 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, otherwise false.
      See Also:
    • noneMatch

      boolean noneMatch(@Nonnull Predicate<? super T> predicate)
      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 then true 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, otherwise false.
      See Also:
    • min

      @Nonnull Optional<T> min(@Nonnull Comparator<? super T> comparator)
      Returns the minimum element of this cache according to the provided Comparator. This is a special case of a reduction.
      Parameters:
      comparator - A non-interfering, stateless Comparator to compare elements of this stream.
      Returns:
      An Optional describing the minimum element of this cache, or an empty Optional if the cache is empty.
      Throws:
      NullPointerException - If the minimum element is null.
      See Also:
    • max

      @Nonnull Optional<T> max(@Nonnull Comparator<? super T> comparator)
      Returns the maximum element of this cache according to the provided Comparator. This is a special case of a reduction.
      Parameters:
      comparator - A non-interfering, stateless Comparator to compare elements of this stream.
      Returns:
      An Optional describing the maximum element of this cache, or an empty Optional if the cache is empty.
      Throws:
      NullPointerException - If the maximum element is null.
      See Also:
    • count

      @Nonnegative long count(@Nonnull Predicate<? super T> filter)
      Returns the amount of elements that match the provided predicate.
      Parameters:
      filter - Filter to find matching elements.
      Returns:
      Amount of matching elements.
    • keys

      @Nonnull Set<Long> keys()
      Returns:
      A view of all the keys in this cache. Updated if this cache is modified.
      See Also:
    • values

      @Nonnull Collection<T> values()
      Returns:
      A view of all the values in this cache. Updated if this cache is modified.
      See Also:
    • snapshot

      @Nonnull Collection<T> snapshot()
      Returns:
      A snapshot of all the values in this cache. Not updated if this cache is modified.
      See Also:
    • snapshot

      @Nonnull <C extends Collection<T>> C snapshot(@Nonnull Supplier<C> supplier)
      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

      @Nonnull default Stream<T> stream()
      Returns:
      A stream with the elements cached, in no specific order.
      See Also:
    • spliterator

      @Nonnull default Spliterator<T> spliterator()
      Specified by:
      spliterator in interface Iterable<T>