Class CachingStemmer

All Implemented Interfaces:
Stemmer

@ThreadSafe public final class CachingStemmer extends DelegatingStemmer<CachingStemmer.ThreadState>
A Stemmer that memoizes word-to-stem mappings in a bounded per-thread LRU cache.

Each thread gets its own delegate stemmer (minted from the supplied StemmerFactory) and its own cache, so instances are safe to share regardless of whether the factory's stemmers are, and memory is bounded to capacity entries per thread that stems. Caching pays off on threads reused across many words, such as a fixed platform-thread pool; on a virtual-thread-per-task executor every task starts with an empty cache, so repeats are served only within one task. stemAll(CharSequence) is forwarded to the delegate uncached.

Long-running environments such as application containers should call DelegatingStemmer.clearThreadLocalState() when a pooled thread no longer uses this stemmer.

  • Field Details

    • DEFAULT_CAPACITY

      public static final int DEFAULT_CAPACITY
      The default maximum number of word-to-stem entries cached per thread: 1024.
      See Also:
  • Constructor Details

    • CachingStemmer

      public CachingStemmer(StemmerFactory factory)
      Creates a caching stemmer with the default capacity.
      Parameters:
      factory - The factory that mints one delegate per thread. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if factory is null.
    • CachingStemmer

      public CachingStemmer(StemmerFactory factory, int capacity)
      Creates a caching stemmer.
      Parameters:
      factory - The factory that mints one delegate per thread. Must not be null.
      capacity - The maximum number of word-to-stem entries kept per thread; must be positive.
      Throws:
      IllegalArgumentException - Thrown if factory is null or capacity is not positive.
  • Method Details

    • threadStateSupplier

      private static Supplier<CachingStemmer.ThreadState> threadStateSupplier(StemmerFactory factory, int capacity)
      Validates the constructor arguments eagerly and returns a supplier that mints one delegate and its per-thread cache, so invalid arguments fail at construction rather than on first use.
      Parameters:
      factory - The factory that mints one delegate per thread. Must not be null.
      capacity - The maximum number of word-to-stem entries kept per thread; must be positive.
      Returns:
      a supplier of fresh per-thread state.
      Throws:
      IllegalArgumentException - Thrown if factory is null or capacity is not positive.
    • stem

      public CharSequence stem(CharSequence word)
      Stems word.
      Parameters:
      word - The input word. Must not be null.
      Returns:
      The stemmed form.
    • stemAll

      public List<CharSequence> stemAll(CharSequence word)
      Returns all stem forms for word. Defaults to a single-element list from Stemmer.stem(CharSequence). Dictionary-based engines may override this to return multiple roots; delegating wrappers must forward it to preserve the full list.
      Parameters:
      word - The input word. Must not be null.
      Returns:
      all stem forms for word
    • clearCache

      public void clearCache()
      Empties the calling thread's cache while keeping its delegate, forcing every subsequent word through a fresh stemming pass. Only the calling thread's cache is affected. A thread that has not stemmed yet has its state initialized by this call, so invoke it on the thread whose cache should be emptied, not from an unrelated maintenance thread.