Class Cache

java.lang.Object
com.maybeitssquid.safeascii.Chainable
com.maybeitssquid.safeascii.Cache
All Implemented Interfaces:
IntFunction<CharSequence>

public class Cache extends Chainable
A Chainable implementation that caches the results of character processing to improve performance.

This class maintains a cache of processed results to avoid re-computing values for the same inputs. It employs a hybrid storage strategy:

  • A direct array is used for fast lookup of ASCII characters.
  • A HashMap is used for all other Unicode codepoints.
This structure ensures minimal overhead for common ASCII characters while supporting the full Unicode range.
  • Constructor Details

    • Cache

      public Cache(IntFunction<CharSequence> delegate)
      Creates a new Cache instance.
      Parameters:
      delegate - the next function in the chain to compute values for cache misses
  • Method Details

    • cache

      public void cache(int codepoint, CharSequence value)
      Manually adds a value to the cache for a specific codepoint.
      Parameters:
      codepoint - the Unicode codepoint to cache
      value - the processed string value to associate with the codepoint
    • process

      protected CharSequence process(int codepoint)
      Retrieves a cached value for the given codepoint.
      Specified by:
      process in class Chainable
      Parameters:
      codepoint - the character to look up
      Returns:
      the cached CharSequence if available, otherwise null
    • apply

      public CharSequence apply(int value)
      Returns the processed string for a given codepoint, using the cache if available.

      If the value is found in the cache, it is returned immediately. If not, the request is delegated to the upstream handler, and the result is cached for future use.

      Specified by:
      apply in interface IntFunction<CharSequence>
      Overrides:
      apply in class Chainable
      Parameters:
      value - the codepoint to process
      Returns:
      the processed CharSequence