@Test public void testParse_pattern_without_currency_sign() { AmountFormatContextBuilder builder = AmountFormatContextBuilder.of(US); builder.set("pattern", "0.00"); AmountFormatContext context = builder.build(); DefaultMonetaryAmountFormat format = new DefaultMonetaryAmountFormat(context); try { format.parse("1,000.42"); } catch (MonetaryParseException e) { assertEquals(e.getMessage(), "Failed to parse currency. Is currency sign ¤ present in pattern?"); assertEquals(e.getErrorIndex(), -1); } }
@Override public MonetaryAmount parse(CharSequence text) throws MonetaryParseException { text = MoneyUtils.replaceNbspWithSpace(text.toString()).trim(); ParseContext ctx = new ParseContext(text); try { for (FormatToken token : this.positiveTokens) { token.parse(ctx); } } catch (Exception e) { Logger log = Logger.getLogger(getClass().getName()); if (log.isLoggable(WARNING)) { log.log(WARNING, "Failed to parse positive pattern, trying negative for: " + text, e); } for (FormatToken token : this.negativeTokens) { token.parse(ctx); } } CurrencyUnit unit = ctx.getParsedCurrency(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); } if (Objects.isNull(unit)) { throw new MonetaryParseException("Failed to parse currency. Is currency sign ¤ present in pattern?", text.toString(), -1); } Number num = ctx.getParsedNumber(); if (Objects.isNull(num)) { throw new MonetaryParseException("Failed to parse amount", text.toString(), -1); } MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory(); } return factory.setCurrency(unit).setNumber(num).create(); }
DefaultMonetaryAmountFormat implements MonetaryAmountFormat { @Override public MonetaryAmount parse(CharSequence text) throws MonetaryParseException { text = MoneyUtils.replaceNbspWithSpace(text.toString()).trim(); ParseContext ctx = new ParseContext(text); try { for (FormatToken token : this.positiveTokens) { token.parse(ctx); } } catch (Exception e) { Logger log = Logger.getLogger(getClass().getName()); if (log.isLoggable(WARNING)) { log.log(WARNING, "Failed to parse positive pattern, trying negative for: " + text, e); } for (FormatToken token : this.negativeTokens) { token.parse(ctx); } } CurrencyUnit unit = ctx.getParsedCurrency(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); } if (Objects.isNull(unit)) { throw new MonetaryParseException("Failed to parse currency. Is currency sign ¤ present in pattern?", text.toString(), -1); } Number num = ctx.getParsedNumber(); if (Objects.isNull(num)) { throw new MonetaryParseException("Failed to parse amount", text.toString(), -1); } MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory(); } return factory.setCurrency(unit).setNumber(num).create(); } }
DefaultMonetaryAmountFormat implements MonetaryAmountFormat { @Override public MonetaryAmount parse(CharSequence text) throws MonetaryParseException { text = MoneyUtils.replaceNbspWithSpace(text.toString()).trim(); ParseContext ctx = new ParseContext(text); try { for (FormatToken token : this.positiveTokens) { token.parse(ctx); } } catch (Exception e) { Logger log = Logger.getLogger(getClass().getName()); if (log.isLoggable(WARNING)) { log.log(WARNING, "Failed to parse positive pattern, trying negative for: " + text, e); } for (FormatToken token : this.negativeTokens) { token.parse(ctx); } } CurrencyUnit unit = ctx.getParsedCurrency(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); } if (Objects.isNull(unit)) { throw new MonetaryParseException("Failed to parse currency. Is currency sign ¤ present in pattern?", text.toString(), -1); } Number num = ctx.getParsedNumber(); if (Objects.isNull(num)) { throw new MonetaryParseException("Failed to parse amount", text.toString(), -1); } MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory(); } return factory.setCurrency(unit).setNumber(num).create(); } DefaultMonetaryAmountFormat(AmountFormatContext amountFormatContext); }
DefaultMonetaryAmountFormat implements MonetaryAmountFormat { @Override public MonetaryAmount parse(CharSequence text) throws MonetaryParseException { text = MoneyUtils.replaceNbspWithSpace(text.toString()).trim(); ParseContext ctx = new ParseContext(text); try { for (FormatToken token : this.positiveTokens) { token.parse(ctx); } } catch (Exception e) { Logger log = Logger.getLogger(getClass().getName()); if (log.isLoggable(WARNING)) { log.log(WARNING, "Failed to parse positive pattern, trying negative for: " + text, e); } for (FormatToken token : this.negativeTokens) { token.parse(ctx); } } CurrencyUnit unit = ctx.getParsedCurrency(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); } if (Objects.isNull(unit)) { throw new MonetaryParseException("Failed to parse currency. Is currency sign ¤ present in pattern?", text.toString(), -1); } Number num = ctx.getParsedNumber(); if (Objects.isNull(num)) { throw new MonetaryParseException("Failed to parse amount", text.toString(), -1); } MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory(); } return factory.setCurrency(unit).setNumber(num).create(); } DefaultMonetaryAmountFormat(AmountFormatContext amountFormatContext); @Override String format(MonetaryAmount amount); @Override void print(Appendable appendable, MonetaryAmount amount); @Override MonetaryAmount parse(CharSequence text); @Override String queryFrom(MonetaryAmount amount); @Override AmountFormatContext getContext(); }
DefaultMonetaryAmountFormat implements MonetaryAmountFormat { @Override public MonetaryAmount parse(CharSequence text) throws MonetaryParseException { text = MoneyUtils.replaceNbspWithSpace(text.toString()).trim(); ParseContext ctx = new ParseContext(text); try { for (FormatToken token : this.positiveTokens) { token.parse(ctx); } } catch (Exception e) { Logger log = Logger.getLogger(getClass().getName()); if (log.isLoggable(WARNING)) { log.log(WARNING, "Failed to parse positive pattern, trying negative for: " + text, e); } for (FormatToken token : this.negativeTokens) { token.parse(ctx); } } CurrencyUnit unit = ctx.getParsedCurrency(); if (Objects.isNull(unit)) { unit = this.amountFormatContext.get(CurrencyUnit.class); } if (Objects.isNull(unit)) { throw new MonetaryParseException("Failed to parse currency. Is currency sign ¤ present in pattern?", text.toString(), -1); } Number num = ctx.getParsedNumber(); if (Objects.isNull(num)) { throw new MonetaryParseException("Failed to parse amount", text.toString(), -1); } MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory(); if (factory == null) { factory = Monetary.getDefaultAmountFactory(); } return factory.setCurrency(unit).setNumber(num).create(); } DefaultMonetaryAmountFormat(AmountFormatContext amountFormatContext); @Override String format(MonetaryAmount amount); @Override void print(Appendable appendable, MonetaryAmount amount); @Override MonetaryAmount parse(CharSequence text); @Override String queryFrom(MonetaryAmount amount); @Override AmountFormatContext getContext();  }