Class GuardedCacheValue<ValueType,​GuardType>

  • Type Parameters:
    ValueType - The type of the thing being stored here
    GuardType - The type of the guard value

    public class GuardedCacheValue<ValueType,​GuardType>
    extends Object
    A cache value wrapper that allows access based on matching an arbitrary guard value.

    If the guard value is not matched, the getValue(Object) method returns an empty Optional.

    This can be used, for example, to detect changes to permissions or logged in users and clear the cache accordingly.

    • Constructor Detail

      • GuardedCacheValue

        public GuardedCacheValue​(ValueType value,
                                 GuardType token)
        Constructs a new cache value with the given token and value.

        The token will be matched using Object.equals.

        Parameters:
        value - The value wrapped by this cache token
        token - The token that must be matched to retrieve the value
      • GuardedCacheValue

        public GuardedCacheValue​(ValueType value,
                                 GuardType token,
                                 BiPredicate<GuardType,​GuardType> matcher)
        Constructs a new cache value with the given token, value, and token matcher.

        At getValue time, the matcher will be used to decide whether the input actually matches the token.

        Parameters:
        value - The value wrapped by this cache token
        token - The token that must be matched to retrieve the value
        matcher - The predicate that decides whether the input token matches
    • Method Detail

      • getValue

        public Optional<ValueTypegetValue​(GuardType guardTest)
        Parameters:
        guardTest - The guard test value
        Returns:
        An optional containing the stored value, if the guard value input matches, or else an empty optional object