Skip to content

ENH: Convert Hunspell Dictionary affix directive parsing else-if chain to switch #16421

Description

@rajat315315

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

  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:

./gradlew :lucene:analysis:common:test --tests "org.apache.lucene.analysis.hunspell.*"

Result: Build succeeded, all 158 tests passed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions