Interface Weigher<K,V>

Type Parameters:
K - the type of keys
V - the type of values
All Known Implementing Classes:
Async.AsyncWeigher, BoundedWeigher, SingletonWeigher
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@ThreadSafe @FunctionalInterface public interface Weigher<K,V>
Calculates the weights of cache entries. The total weight threshold is used to determine when an eviction is required.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> Weigher<K,V>
    boundedWeigher(Weigher<K,V> delegate)
    Returns a weigher that enforces that the weight is non-negative.
    static <K, V> Weigher<K,V>
    Returns a weigher where an entry has a weight of 1.
    int
    weigh(K key, V value)
    Returns the weight of a cache entry.
  • Method Details

    • weigh

      @Nonnegative int weigh(@Nonnull K key, @Nonnull V value)
      Returns the weight of a cache entry. There is no unit for entry weights; rather they are simply relative to each other.
      Parameters:
      key - the key to weigh
      value - the value to weigh
      Returns:
      the weight of the entry; must be non-negative
    • singletonWeigher

      @Nonnull static <K, V> Weigher<K,V> singletonWeigher()
      Returns a weigher where an entry has a weight of 1.
      Type Parameters:
      K - the type of keys
      V - the type of values
      Returns:
      a weigher where an entry has a weight of 1
    • boundedWeigher

      @Nonnull static <K, V> Weigher<K,V> boundedWeigher(@Nonnull Weigher<K,V> delegate)
      Returns a weigher that enforces that the weight is non-negative.
      Type Parameters:
      K - the type of keys
      V - the type of values
      Parameters:
      delegate - the weigher to that weighs the entry
      Returns:
      a weigher that enforces that the weight is non-negative