/** * Access the context's Locale. * * @return the Locale, or null. */ public Locale getLocale() { return get(Locale.class); }
/** * Access the style's {@link Locale}. * * @return the {@link Locale}, never {@code null}. */ public String getFormatName() { return getText(KEY_FORMAT_NAME); }
/** * Creates a new instance of {@link AmountFormatContext} that configures a {@link javax.money.format * .MonetaryAmountFormat}. * * @return a new {@link AmountFormatContext} instance. */ @Override public AmountFormatContext build() { return new AmountFormatContext(this); }
this.positiveTokens = new ArrayList<>(); this.negativeTokens = new ArrayList<>(); String pattern = amountFormatContext.getText("pattern"); if (pattern == null) { if (amountFormatContext.getLocale() != null && "BG".equals(amountFormatContext.getLocale().getCountry())) { pattern = "#,##0.00 ยค"; }else { pattern = ((DecimalFormat) DecimalFormat.getCurrencyInstance(amountFormatContext.getLocale())).toPattern(); if (Objects.nonNull(amountFormatContext.get(DecimalFormatSymbols.class))) { patternSeparator = amountFormatContext.get(DecimalFormatSymbols.class).getPatternSeparator();
Number num = ctx.getParsedNumber(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory();
/** * AccessingMonetaryAmountFormat using * MonetaryFormats.getAmountFormat(Locale locale), all locales * available also from java.text.DecimalFormat must be supported. */ @SpecAssertion(section = "4.4.3", id = "443-A1") @Test(description = "4.4.3 Ensures all Locales defined by DecimalFormat.getAvailableLocales() are available for " + "monetary formatting.") public void testLocalesSupported() { Locale[] jdkDecimalFormatLocales = DecimalFormat.getAvailableLocales(); for (Locale jdkDecimalFormatLocale : jdkDecimalFormatLocales) { MonetaryAmountFormat amountFormat = MonetaryFormats.getAmountFormat(jdkDecimalFormatLocale); AssertJUnit.assertNotNull(amountFormat); assertEquals(jdkDecimalFormatLocale, amountFormat.getContext().getLocale()); } }
/** * Access the format's {@link javax.money.MonetaryAmountFactory} that is used to of new amounts during * parsing. If not set explicitly, the default {@link javax.money.MonetaryAmountFactory} is used. * * @return the {@link javax.money.MonetaryAmountFactory}, never {@code null}. */ public MonetaryAmountFactory<?> getParseFactory() { return get(MonetaryAmountFactory.class); }
private void initPattern(String pattern, List<FormatToken> tokens, AmountFormatContext style) { int index = pattern.indexOf(CURRENCY_SIGN); if (index > 0) { // currency placement after, between String p1 = pattern.substring(0, index); String p2 = pattern.substring(index + 1); if (isLiteralPattern(p1, style)) { tokens.add(new LiteralToken(p1)); tokens.add(new CurrencyToken(style.get(CurrencyStyle.class), style. get(Locale.class))); } else { tokens.add(new AmountNumberToken(style, p1)); tokens.add(new CurrencyToken(style.get(CurrencyStyle.class), style.get(Locale.class))); } if (!p2.isEmpty()) { if (isLiteralPattern(p2, style)) { tokens.add(new LiteralToken(p2)); } else { tokens.add(new AmountNumberToken(style, p2)); } } } else if (index == 0) { // currency placement before tokens.add(new CurrencyToken(style.get(CurrencyStyle.class), style .get(Locale.class))); tokens.add(new AmountNumberToken(style, pattern.substring(1))); } else { // no currency tokens.add(new AmountNumberToken(style, pattern)); } }
private void initDecimalFormats() { Locale locale = amountFormatContext.get(Locale.class); formatFormat = (DecimalFormat) DecimalFormat.getInstance(locale); parseFormat = (DecimalFormat) DecimalFormat.getInstance(locale); DecimalFormatSymbols syms = amountFormatContext.get(DecimalFormatSymbols.class); if (Objects.nonNull(syms)) { formatFormat.setDecimalFormatSymbols(syms); parseFormat.setDecimalFormatSymbols(syms); } formatFormat.applyPattern(removeNBSP(partialNumberPattern)); parseFormat.applyPattern(removeNBSP(partialNumberPattern).trim()); // Fix for https://github.com/JavaMoney/jsr354-ri/issues/151 if ("BG".equals(locale.getCountry())) { formatFormat.setGroupingSize(3); formatFormat.setGroupingUsed(true); syms = formatFormat.getDecimalFormatSymbols(); syms.setDecimalSeparator(','); syms.setGroupingSeparator(' '); formatFormat.setDecimalFormatSymbols(syms); parseFormat.setDecimalFormatSymbols(syms); } }
@Override public void print(Appendable appendable, MonetaryAmount amount) throws IOException { if (amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class) == null || amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class).length == 0) { appendable.append(this.formatFormat.format(amount.getNumber() .numberValue(BigDecimal.class))); } else { if (Objects.isNull(numberGroup)) { char[] groupChars = amountFormatContext.get(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, char[].class); if (groupChars == null || groupChars.length == 0) { groupChars = new char[]{this.formatFormat .getDecimalFormatSymbols().getGroupingSeparator()}; int[] groupSizes = amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class); if (groupSizes == null) { groupSizes = new int[0];