Package opennlp.tools.util.featuregen

# Class CachedFeatureGenerator

java.lang.Object
opennlp.tools.util.featuregen.CachedFeatureGenerator

All Implemented Interfaces:
  `opennlp.tools.util.featuregen.AdaptiveFeatureGenerator`

@ThreadSafe public class CachedFeatureGenerator extends Object implements opennlp.tools.util.featuregen.AdaptiveFeatureGenerator
Caches features of the aggregated `generators`.

The cache is maintained per-thread via `ThreadLocal`, making this class safe for concurrent use from multiple threads. Each thread gets its own independent cache that is cleared when a new sentence (token array) is encountered.

Cache key is reference identity, not content. The "is this still the same sentence?" check uses `tokens == state.prevTokens`; passing a freshly allocated `String[]` with the same contents is treated as a new sentence and triggers a cache miss + clear. Reuse the same `tokens` array across calls when you need the cache to hit.

Note: In container environments with classloader isolation (e.g. Jakarta EE), `ThreadLocal` state may pin the classloader. Ensure instances do not outlive the application's lifecycle, or call `ThreadLocal.remove()` on pooled threads.

See Also:
  - `Cache`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `DISABLE_CACHE_PROPERTY`
  System property to disable the feature cache globally.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CachedFeatureGenerator(opennlp.tools.util.featuregen.AdaptiveFeatureGenerator generator)`
  `CachedFeatureGenerator(opennlp.tools.util.featuregen.AdaptiveFeatureGenerator... generators)`
  Deprecated.
  Use `CachedFeatureGenerator(AdaptiveFeatureGenerator)` instead.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clearAdaptiveData()`
  `void`
  `createFeatures(List<String> features, String[] tokens, int index, String[] previousOutcomes)`
  `opennlp.tools.util.featuregen.AdaptiveFeatureGenerator`
  `getCachedFeatureGenerator()`
  `long`
  `getNumberOfCacheHits()`
  Deprecated.
  Cache statistics are no longer tracked.
  `long`
  `getNumberOfCacheMisses()`
  Deprecated.
  Cache statistics are no longer tracked.
  `void`
  `updateAdaptiveData(String[] tokens, String[] outcomes)`

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### DISABLE_CACHE_PROPERTY

    public static final String DISABLE_CACHE_PROPERTY
    System property to disable the feature cache globally. Set to `"true"` to bypass caching (useful for benchmarking).
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### CachedFeatureGenerator

    @Deprecated public CachedFeatureGenerator(opennlp.tools.util.featuregen.AdaptiveFeatureGenerator... generators)
    Deprecated.
    Use `CachedFeatureGenerator(AdaptiveFeatureGenerator)` instead.

  - ### CachedFeatureGenerator

    public CachedFeatureGenerator(opennlp.tools.util.featuregen.AdaptiveFeatureGenerator generator)

- ## Method Details

  - ### createFeatures

    public void createFeatures(List<String> features, String[] tokens, int index, String[] previousOutcomes)
    Specified by:
      `createFeatures` in interface `opennlp.tools.util.featuregen.AdaptiveFeatureGenerator`

  - ### updateAdaptiveData

    public void updateAdaptiveData(String[] tokens, String[] outcomes)
    Specified by:
      `updateAdaptiveData` in interface `opennlp.tools.util.featuregen.AdaptiveFeatureGenerator`

  - ### clearAdaptiveData

    public void clearAdaptiveData()
    Specified by:
      `clearAdaptiveData` in interface `opennlp.tools.util.featuregen.AdaptiveFeatureGenerator`

  - ### getNumberOfCacheHits

    @Deprecated(since="3.0.0") public long getNumberOfCacheHits()
    Deprecated.
    Cache statistics are no longer tracked.
    Returns:
      Retrieves the number of cache hits for the current thread.

  - ### getNumberOfCacheMisses

    @Deprecated(since="3.0.0") public long getNumberOfCacheMisses()
    Deprecated.
    Cache statistics are no longer tracked.
    Returns:
      Retrieves the number of cache misses for the current thread.

  - ### getCachedFeatureGenerator

    public opennlp.tools.util.featuregen.AdaptiveFeatureGenerator getCachedFeatureGenerator()