Class GuardedCacheValue<ValueType,​GuardType>

  • Type Parameters:
    ValueType - The type of the thing being stored here
    GuardType - The type of the guard value
    All Implemented Interfaces:
    Function<GuardType,​Optional<ValueType>>

    public class GuardedCacheValue<ValueType,​GuardType>
    extends Object
    implements Function<GuardType,​Optional<ValueType>>
    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.

    This is similar to AtomicStampedReference, but can use any value or behavior as the ‘stamp’. With a guard type of Integer, and the default matcher of Object::equals, this class would be identical to that.

    • 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