Skip to content

AlphabetConverter.toString() renders the encoded value, not the original code point - #798

Open
amalsaidov wants to merge 1 commit into
apache:masterfrom
amalsaidov:fix-alphabetconverter-tostring-encoded-value
Open

amalsaidov wants to merge 1 commit into
apache:masterfrom
amalsaidov:fix-alphabetconverter-tostring-encoded-value

Conversation

@amalsaidov

Copy link
Copy Markdown

Summary

AlphabetConverter.toString() renders the integer code point of each original letter instead of its encoded value.

The toString() method iterates originalToEncoded (a Map<Integer, String> of original code point → encoded string) with a lambda (k, v), but appends k after the arrow instead of v. The v parameter is left unused, which is the tell-tale of the mix-up (likely introduced when the loop was converted to a lambda).

As a result the output shows the raw code point rather than the mapping:

AlphabetConverter ac = AlphabetConverter.createConverterFromChars(
        new Character[] { 'a', 'b', 'c', 'd' }, new Character[] { '0', '1' }, new Character[0]);

ac.getOriginalToEncoded(); // {97=000, 98=001, 99=010, 100=011}
toString() output
Before a -> 97
b -> 98
c -> 99
d -> 100
After a -> 000
b -> 001
c -> 010
d -> 011

Fix

Append the encoded value v instead of the key k.

Test

Adds AlphabetConverterTest.testToStringRendersEncodedValues, which asserts each original -> encoded mapping appears in toString(). It fails before the fix (output contains a -> 97, not a -> 000) and passes after. The existing toString() round-trip assertion is unaffected.

  • AlphabetConverterTest: 21/21 green.
  • mvn checkstyle:check and mvn pmd:check: clean.

…nal code point

The toString() lambda appended the Integer key (the original letter's
code point, e.g. 97) after the arrow instead of the mapped encoded value
(e.g. 000), leaving the declared 'v' parameter unused. This made the
output show, for example, "a -> 97" rather than "a -> 000".

Append the encoded value and add a regression test.
@amalsaidov
amalsaidov force-pushed the fix-alphabetconverter-tostring-encoded-value branch from 5d5c326 to 0920708 Compare December 1, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant