Class Cache
java.lang.Object
com.maybeitssquid.safeascii.internal.Chainable
com.maybeitssquid.safeascii.internal.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, thread-safe lookup of ASCII characters.
- A bounded
HashMapis used for all other codepoints.
MAX_NON_ASCII_ENTRIES, later
values are processed but not retained.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of non-ASCII mappings retained by one pipeline. -
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.
-
Field Details
-
MAX_NON_ASCII_ENTRIES
public static final int MAX_NON_ASCII_ENTRIESMaximum number of non-ASCII mappings retained by one pipeline.- See Also:
-
-
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
-