@Test public void checkNumberFormatException() { final String result = formatCurrencyId.apply("IDR 557400.00000000"); log.debug("Result: '{}'", result); }
@Override @Nullable public String apply(@Nullable String input) { if (!Strings.isNullOrEmpty(input)) { try { final Matcher matcher = embeddedPattern.matcher(input); final CurrencyUnit currency; final BigDecimal scaled; if (matcher.matches()) { final String currencyCode = matcher.group(1); currency = Monetary.getCurrency(currencyCode); scaled = new BigDecimal(matcher.group(2)).setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } else { currency = Preconditions.checkNotNull(defaultCurrency, "input string '%s' does not embed currency code, either defaultCurrency must be specified or fix your input", input); scaled = new BigDecimal(input); } final String symbol = MoneyUtils.getSymbol(locale, currency); final Money money = Money.of(scaled, currency); return symbol + formatter.format(money).replace(currency.getCurrencyCode(), ""); } catch (Exception e) { final List<Throwable> causes = Throwables.getCausalChain(e); if (causes.stream().anyMatch((it) -> it instanceof NumberFormatException)) { log.warn(String.format("Cannot formatCurrency from '%s'", input), e); } else { log.error(String.format("Cannot formatCurrency from '%s'", input), e); } return input; } } else { return ""; } }
FormatCurrency implements
		Function<String, String> { @Override @Nullable public String apply(@Nullable String input) { if (!Strings.isNullOrEmpty(input)) { try { final Matcher matcher = embeddedPattern.matcher(input); final CurrencyUnit currency; final BigDecimal scaled; if (matcher.matches()) { final String currencyCode = matcher.group(1); currency = Monetary.getCurrency(currencyCode); scaled = new BigDecimal(matcher.group(2)).setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } else { currency = Preconditions.checkNotNull(defaultCurrency, "input string '%s' does not embed currency code, either defaultCurrency must be specified or fix your input", input); scaled = new BigDecimal(input); } final String symbol = MoneyUtils.getSymbol(locale, currency); final Money money = Money.of(scaled, currency); return symbol + formatter.format(money).replace(currency.getCurrencyCode(), ""); } catch (Exception e) { final List<Throwable> causes = Throwables.getCausalChain(e); if (causes.stream().anyMatch((it) -> it instanceof NumberFormatException)) { log.warn(String.format("Cannot formatCurrency from '%s'", input), e); } else { log.error(String.format("Cannot formatCurrency from '%s'", input), e); } return input; } } else { return ""; } } }
FormatCurrency implements
		Function<String, String> { @Override @Nullable public String apply(@Nullable String input) { if (!Strings.isNullOrEmpty(input)) { try { final Matcher matcher = embeddedPattern.matcher(input); final CurrencyUnit currency; final BigDecimal scaled; if (matcher.matches()) { final String currencyCode = matcher.group(1); currency = Monetary.getCurrency(currencyCode); scaled = new BigDecimal(matcher.group(2)).setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } else { currency = Preconditions.checkNotNull(defaultCurrency, "input string '%s' does not embed currency code, either defaultCurrency must be specified or fix your input", input); scaled = new BigDecimal(input); } final String symbol = MoneyUtils.getSymbol(locale, currency); final Money money = Money.of(scaled, currency); return symbol + formatter.format(money).replace(currency.getCurrencyCode(), ""); } catch (Exception e) { final List<Throwable> causes = Throwables.getCausalChain(e); if (causes.stream().anyMatch((it) -> it instanceof NumberFormatException)) { log.warn(String.format("Cannot formatCurrency from '%s'", input), e); } else { log.error(String.format("Cannot formatCurrency from '%s'", input), e); } return input; } } else { return ""; } } @Deprecated  FormatCurrency(); @Deprecated  FormatCurrency(@Nullable CurrencyUnit defaultCurrency);  FormatCurrency(Locale locale);  FormatCurrency(Locale locale, @Nullable CurrencyUnit defaultCurrency); }
FormatCurrency implements
		Function<String, String> { @Override @Nullable public String apply(@Nullable String input) { if (!Strings.isNullOrEmpty(input)) { try { final Matcher matcher = embeddedPattern.matcher(input); final CurrencyUnit currency; final BigDecimal scaled; if (matcher.matches()) { final String currencyCode = matcher.group(1); currency = Monetary.getCurrency(currencyCode); scaled = new BigDecimal(matcher.group(2)).setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } else { currency = Preconditions.checkNotNull(defaultCurrency, "input string '%s' does not embed currency code, either defaultCurrency must be specified or fix your input", input); scaled = new BigDecimal(input); } final String symbol = MoneyUtils.getSymbol(locale, currency); final Money money = Money.of(scaled, currency); return symbol + formatter.format(money).replace(currency.getCurrencyCode(), ""); } catch (Exception e) { final List<Throwable> causes = Throwables.getCausalChain(e); if (causes.stream().anyMatch((it) -> it instanceof NumberFormatException)) { log.warn(String.format("Cannot formatCurrency from '%s'", input), e); } else { log.error(String.format("Cannot formatCurrency from '%s'", input), e); } return input; } } else { return ""; } } @Deprecated  FormatCurrency(); @Deprecated  FormatCurrency(@Nullable CurrencyUnit defaultCurrency);  FormatCurrency(Locale locale);  FormatCurrency(Locale locale, @Nullable CurrencyUnit defaultCurrency); @Override @Nullable String apply(@Nullable String input); }
FormatCurrency implements
		Function<String, String> { @Override @Nullable public String apply(@Nullable String input) { if (!Strings.isNullOrEmpty(input)) { try { final Matcher matcher = embeddedPattern.matcher(input); final CurrencyUnit currency; final BigDecimal scaled; if (matcher.matches()) { final String currencyCode = matcher.group(1); currency = Monetary.getCurrency(currencyCode); scaled = new BigDecimal(matcher.group(2)).setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_EVEN); } else { currency = Preconditions.checkNotNull(defaultCurrency, "input string '%s' does not embed currency code, either defaultCurrency must be specified or fix your input", input); scaled = new BigDecimal(input); } final String symbol = MoneyUtils.getSymbol(locale, currency); final Money money = Money.of(scaled, currency); return symbol + formatter.format(money).replace(currency.getCurrencyCode(), ""); } catch (Exception e) { final List<Throwable> causes = Throwables.getCausalChain(e); if (causes.stream().anyMatch((it) -> it instanceof NumberFormatException)) { log.warn(String.format("Cannot formatCurrency from '%s'", input), e); } else { log.error(String.format("Cannot formatCurrency from '%s'", input), e); } return input; } } else { return ""; } } @Deprecated  FormatCurrency(); @Deprecated  FormatCurrency(@Nullable CurrencyUnit defaultCurrency);  FormatCurrency(Locale locale);  FormatCurrency(Locale locale, @Nullable CurrencyUnit defaultCurrency); @Override @Nullable String apply(@Nullable String input);  }