jsr354-api icon indicating copy to clipboard operation
jsr354-api copied to clipboard

Code in class comments of MonetaryAmountFormat does not compile

Open marschall opened this issue 6 years ago • 1 comments

The class comment of MonetaryAmountFormat has two code snippets. Both of them are full of compile errors

MonetaryAmountFormat f = MonetaryFormats.getInstance(loc);
f.setStyle(f.getStyle().toBuilder().setPattern("###.##;(###.##)").build());
  • there are no #setStyle and #getStyle methods
  • there is no #getInstance method on MonetaryFormats, you probably mean #getAmountFormat

        Locale[] locales = MonetaryFormats.getAvailableLocales();
        MonetaryAmount amount = ...;
        MonetaryAmountFormat form;
            System.out.println("FORMAT");
            for (int i = 0; i < locales.length; ++i) {
                if (locales[i].getCountry().length() == 0) {
                   continue; // Skip language-only locales
                }
                System.out.print(locales[i].getDisplayName());
                form = MonetaryFormats.getInstance(locales[i]);
                System.out.print(": " + form.getStyle().getPattern());
                String myAmount = form.format(amount);
                System.out.print(" -> " + myAmount);
                try {
                    System.out.println(" -> " + form.parse(form.format(myAmount)));
                } catch (ParseException e) {}
            }
        }
  • there is no MonetaryFormats#getAvailableLocales(), you could use Locale#getAvailableLocales() but then you should probably also check MonetaryFormats#isAvailable
  • while no a compile error an enhanced for loop over the locales would be nice
  • again there is no #getStyle method
  • there is no #getInstance method on MonetaryFormats
  • #parse does not throw ParseException but MonetaryParseException
  • the last } is too much

marschall avatar Jan 06 '19 13:01 marschall

I ran into this as well, the documentation clearly shows an expectation of a MonetaryFormat.getInstance but it's not supported.

https://javamoney.github.io/apidocs/javax/money/format/MonetaryAmountFormat.html#print-java.lang.Appendable-javax.money.MonetaryAmount-

retinaburn avatar Nov 07 '19 17:11 retinaburn