Class CachingMatcher<T>

Type Parameters:
T - The actual matched type of this matcher.
All Implemented Interfaces:
ElementMatcher<T>, ElementMatcher.Junction<T>
Direct Known Subclasses:
CachingMatcher.WithInlineEviction

@Enhance(permitSubclassEquality=true) public class CachingMatcher<T> extends ElementMatcher.Junction.AbstractBase<T>
A matcher that remembers the results of previously matching an equal target.
  • Field Details

    • NULL_VALUE

      private static final Object NULL_VALUE
      A substitute value to store in a map instead of a null value.
    • matcher

      private final ElementMatcher<? super T> matcher
      The underlying matcher to apply for non-cached targets.
    • map

      @ValueHandling(IGNORE) protected final ConcurrentMap<? super T,Boolean> map
      A map that serves as a cache for previous matches.
  • Constructor Details

    • CachingMatcher

      public CachingMatcher(ElementMatcher<? super T> matcher, ConcurrentMap<? super T,Boolean> map)
      Creates a new caching matcher.
      Parameters:
      matcher - The underlying matcher to apply for non-cached targets.
      map - A map that serves as a cache for previous matches. This match is strongly referenced and can cause a memory leak if it is not evicted while keeping this matcher alive.
  • Method Details

    • matches

      public boolean matches(@MaybeNull T target)
      Matches a target against this element matcher.
      Parameters:
      target - The instance to be matched or null.
      Returns:
      true if the given element is matched by this matcher or false otherwise.
    • onCacheMiss

      protected boolean onCacheMiss(@MaybeNull T target)
      Invoked if the cache is not hit.
      Parameters:
      target - The element to be matched.
      Returns:
      true if the element is matched.
    • toString

      public String toString()
      Overrides:
      toString in class Object