FormattingAndParsing
Business Purpose
Provides flexible, locale-aware printing and parsing of monetary amounts to/from strings, with full control over grouping, decimal symbols, sign, and currency representation. [AI Inference] This enables internationalization and custom formatting in user interfaces or reports.
Current Behaviors
MoneyFormatterBuilder.appendAmount()adds the numeric amount MoneyFormatterBuilder.java:61.MoneyFormatterBuilder.appendCurrencyCode()adds the three-letter currency code MoneyFormatterBuilder.java:106.MoneyFormatterBuilder.appendCurrencySymbolLocalized()adds the localized currency symbol MoneyFormatterBuilder.java:143.MoneyFormatterBuilder.appendLiteral(String)adds a literal string MoneyFormatterBuilder.java:156.MoneyFormatterBuilder.appendSigned(MoneyPrinter, MoneyPrinter)adds conditional sign MoneyFormatterBuilder.java:216.MoneyFormatterBuilder.toFormatter()creates the formatter MoneyFormatterBuilder.java:280.MoneyFormatter.print(Appendable, MoneyProvider)writes formatted output MoneyFormatter.java:183.MoneyFormatter.parseMoney(String)parses a string to aMoneyMoneyFormatter.java:259.MoneyFormatter.parseBigMoney(String)parses to aBigMoneyMoneyFormatter.java:229.AmountPrinterParserhandles grouped digit printing AmountPrinterParser.java:46.MoneyAmountStyleconfigures characters for grouping, decimal, zero, and signs MoneyAmountStyle.java:167.MoneyPrintContextholds locale during printing MoneyPrintContext.java:49.MoneyParseContextholds parsing state including currency and amount MoneyParseContext.java:96.
Technical Implementation
MoneyFormatterMoneyFormatter.java:58 andMoneyFormatterBuilderMoneyFormatterBuilder.java:61 are IMMUTABLE_VALUE_OBJECT (design patterns detected).- The builder creates a chain of printer-parsers, each implementing the same internal interface (e.g.,
AmountPrinterParser,LiteralPrinterParser). AmountPrinterParseruses@Overrideat lines 46, 129, 180.MoneyParseContexttracks parsing progress and allows child contexts for nesting MoneyParseContext.java:267.MoneyAmountStylesupports localization withlocalize(Locale)MoneyAmountStyle.java:225.
Definition of Done
- A formatter built with
appendCurrencyCode().appendAmount()prints "USD 12.34" for a Money of USD 12.34. MoneyFormatter.parseMoney("GBP 5.00")returns a Money with GBP and amount 5.00.- Changing
MoneyAmountStylegrouping character from comma to space alters the printed output accordingly. - Parsing an incomplete input like "JPY 123" with missing minor digits throws
MoneyFormatException. - The
appendSignedmethod prints a negative sign only for negative amounts and no sign for positive. MoneyPrintContext.getLocale()returns the locale set byMoneyFormatter.withLocale().