ENH: Convert Hunspell Dictionary affix directive parsing else-if chain to switch
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 1.4k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 88
Description
### 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:
```java
// 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
1. **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.
2. **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:
```bash
./gradlew :lucene:analysis:common:test --tests "org.apache.lucene.analysis.hunspell.*"
```
**Result**: Build succeeded, all 158 tests passed.
Contributor guide
Research direction
Start in org.apache.lucene.analysis.hunspell.Dictionary#readAffixFile and inspect the related getDecoderAndFlagParsingStrategy and getFlagParsingStrategy methods. Replace the specified conditional chains with switch forms while preserving directive handling, then run ./gradlew :lucene:analysis:common:test --tests "org.apache.lucene.analysis.hunspell.*". Done means the refactor is complete, the stale TODO is removed, and all 158 Hunspell tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- search
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100