Description
Issue Description
In org.apache.lucene.analysis.hunspell.Dictionary#readAffixFile, affix directives (such as PFX, SFX, CIRCUMFIX, KEEPCASE, NEEDAFFIX, etc.) were being checked sequentially using a large if-else chain spanning over 120 lines.
The code previously included a TODO comment asking:
// TODO: convert to a switch?
Using a modern Java switch statement on firstWord simplifies this construct, improves code readability, and allows the Java compiler to produce a more efficient bytecode lookup scheme rather than sequential String.equals() comparisons.
Proposed Changes
-
Refactor readAffixFile Directive Parser:
- Replace the
if ("AF".equals(firstWord)) ... else if ... chain in Dictionary.java with a switch (firstWord) statement.
- Combine multi-label cases sharing identical logic (e.g.
"NEEDAFFIX", "PSEUDOROOT" and "ICONV", "OCONV").
- Removed the stale
// TODO: convert to a switch? comment.
-
Refactor Related Directive Helpers:
getDecoderAndFlagParsingStrategy: Converted "SET" / "FLAG" header check loop from if-else to switch (firstWord).
getFlagParsingStrategy: Refactored if ("num".equals(flagType)) chain to a concise switch expression returning strategy instances.
Benefits
- Readability & Maintainability: Clearer structure and alignment across all Hunspell directive handlers.
- Performance: Better bytecode optimization with
tableswitch/lookupswitch over linear string comparisons.
- Code Cleanliness: Resolves existing
TODO comment.
Verification
Ran all Hunspell analysis tests:
./gradlew :lucene:analysis:common:test --tests "org.apache.lucene.analysis.hunspell.*"
Result: Build succeeded, all 158 tests passed.
Description
Issue Description
In
org.apache.lucene.analysis.hunspell.Dictionary#readAffixFile, affix directives (such asPFX,SFX,CIRCUMFIX,KEEPCASE,NEEDAFFIX, etc.) were being checked sequentially using a largeif-elsechain spanning over 120 lines.The code previously included a TODO comment asking:
// TODO: convert to a switch?Using a modern Java
switchstatement onfirstWordsimplifies this construct, improves code readability, and allows the Java compiler to produce a more efficient bytecode lookup scheme rather than sequentialString.equals()comparisons.Proposed Changes
Refactor
readAffixFileDirective Parser:if ("AF".equals(firstWord)) ... else if ...chain inDictionary.javawith aswitch (firstWord)statement."NEEDAFFIX", "PSEUDOROOT"and"ICONV", "OCONV").// TODO: convert to a switch?comment.Refactor Related Directive Helpers:
getDecoderAndFlagParsingStrategy: Converted"SET"/"FLAG"header check loop fromif-elsetoswitch (firstWord).getFlagParsingStrategy: Refactoredif ("num".equals(flagType))chain to a conciseswitchexpression returning strategy instances.Benefits
tableswitch/lookupswitchover linear string comparisons.TODOcomment.Verification
Ran all Hunspell analysis tests:
./gradlew :lucene:analysis:common:test --tests "org.apache.lucene.analysis.hunspell.*"Result: Build succeeded, all 158 tests passed.