Package com.maybeitssquid.safeascii
Class Cache
java.lang.Object
com.maybeitssquid.safeascii.Chainable
com.maybeitssquid.safeascii.Cache
- All Implemented Interfaces:
IntFunction<CharSequence>
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
HashMapis used for all other Unicode codepoints.
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply(int value) Returns the processed string for a given codepoint, using the cache if available.voidcache(int codepoint, CharSequence value) Manually adds a value to the cache for a specific codepoint.protected CharSequenceprocess(int codepoint) Retrieves a cached value for the given codepoint.
-
Constructor Details
-
Cache
Creates a new Cache instance.- Parameters:
delegate- the next function in the chain to compute values for cache misses
-
-
Method Details
-
cache
Manually adds a value to the cache for a specific codepoint.- Parameters:
codepoint- the Unicode codepoint to cachevalue- the processed string value to associate with the codepoint
-
process
Retrieves a cached value for the given codepoint.- Specified by:
processin classChainable- Parameters:
codepoint- the character to look up- Returns:
- the cached
CharSequenceif available, otherwisenull
-
apply
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:
applyin interfaceIntFunction<CharSequence>- Overrides:
applyin classChainable- Parameters:
value- the codepoint to process- Returns:
- the processed
CharSequence
-