/** * Obtain a MonetaryAmountFormat for the given locale. * <p>The default implementation simply calls * {@link javax.money.format.MonetaryFormats#getAmountFormat} * with either the configured format name or the given locale. * @param locale the current locale * @return the MonetaryAmountFormat (never {@code null}) * @see #setFormatName */ protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { if (this.formatName != null) { return MonetaryFormats.getAmountFormat(this.formatName); } else { return MonetaryFormats.getAmountFormat(locale); } }
/** * Print several amounts, created using the default factory, but * also a test instance, provided by the TCK, to ensure no * implementation * dependencies on the implementation. */ @SpecAssertion(section = "4.4.1", id = "441-A2") @Test(description = "4.4.1 Formats amounts using all available locales.") public void testFormattingIsIndependentOfImplementation() { for (Locale locale : MonetaryFormats.getAvailableLocales()) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); Set<String> formatsProcuced = new HashSet<>(); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) { continue; } MonetaryAmount amt = fact.setCurrency("USD").setNumber(10.5).create(); String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." + "getMonetaryFormat(Locale,String...) with supported Locale: " + locale); assertFalse(formatProduced.isEmpty(), "MonetaryAmountFormat returned empty String for " + amt); formatsProcuced.add(formatProduced); } assertFalse(formatsProcuced.isEmpty(), "No formatted amount available. Are there no amount?"); assertFalse(formatsProcuced.size() > 1, "Formatter produces different output for different amount classes(+" + format.getClass() + "): " + formatsProcuced); } }
/** * Get all available locales. This equals to {@link MonetaryAmountFormatProviderSpi#getAvailableLocales()}. * * @param providers The providers to be used, if not set the providers as defined by #getDefaultCurrencyProviderChain() are * used. * @return all available locales, never {@code null}. */ public static Set<Locale> getAvailableLocales(String... providers) { return getMonetaryFormatsSpi().getAvailableLocales(providers); }
/** * Test MonetaryFormats.getAvailableLocales, all locales available also from java.text.DecimalFormat must be * supported (using AmountFormatContext.of(Locale)), more locales are possible. */ @Test(description = "4.4.3 Ensures for each locale defined by DecimalFormat.getAvailableLocales() a " + "MonetaryFormats.isAvailable(Locale) is true.") @SpecAssertion(section = "4.4.3", id = "443-A3") public void testGetAvailableLocales() { Set<Locale> locales = MonetaryFormats.getAvailableLocales(); for (Locale locale : DecimalFormat.getAvailableLocales()) { if (Locale.ROOT.equals(locale)) { continue; } AssertJUnit.assertTrue( "MonetaryFormats.getAvailableLocales(); Locale supported by JDKs DecimalFormat is not available: " + locale, locales.contains(locale)); } }
private void applyJavaMoneyHack() { //fixes https://github.com/commercetools/commercetools-sunrise-java/issues/404 //exception play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.IllegalArgumentException: java.util.concurrent.CompletionException: io.sphere.sdk.json.JsonException: detailMessage: com.fasterxml.jackson.databind.JsonMappingException: Operator failed: javax.money.DefaultMonetaryRoundingsSingletonSpi$DefaultCurrencyRounding@1655879e (through reference chain: io.sphere.sdk.payments.PaymentDraftImpl["amountPlanned"]) Monetary.getDefaultRounding(); Monetary.getDefaultRounding().apply(MoneyImpl.ofCents(123, "EUR")); Monetary.getDefaultAmountType(); MonetaryFormats.getDefaultFormatProviderChain(); Monetary.getDefaultCurrencyProviderChain(); } }
"Locale.") public void testParseIsIndependentOfImplementation() { for (Locale locale : MonetaryFormats.getAvailableLocales()) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) {
/** * Get the default provider chain, identified by the unique provider names in order as evaluated and used. * * @return the default provider chain, never null. */ public static List<String> getDefaultFormatProviderChain() { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .getDefaultProviderChain(); }
/** * Obtain a MonetaryAmountFormat for the given locale. * <p>The default implementation simply calls * {@link javax.money.format.MonetaryFormats#getAmountFormat} * with either the configured format name or the given locale. * @param locale the current locale * @return the MonetaryAmountFormat (never {@code null}) * @see #setFormatName */ protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { if (this.formatName != null) { return MonetaryFormats.getAmountFormat(this.formatName); } else { return MonetaryFormats.getAmountFormat(locale); } }
"using different format queries.") public void testParseDifferentStyles() { for (Locale locale : MonetaryFormats.getAvailableLocales()) { for (Class clazz : Monetary.getAmountTypes()) { if (clazz.equals(TestAmount.class)) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(query); String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." +
/** * Get the names of the currently registered format providers. * * @return the provider names, never null. */ public static Collection<String> getFormatProviderNames() { Collection<String> providers = Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow( () -> new MonetaryException( "No MonetaryFormatsSingletonSpi loaded, query functionality is not available.")) .getProviderNames(); if (Objects.isNull(providers)) { Logger.getLogger(MonetaryFormats.class.getName()).warning( "No supported rate/conversion providers returned by SPI: " + getMonetaryFormatsSpi().getClass().getName()); return Collections.emptySet(); } return providers; }
@Override public Object createFromString(DeserializationContext context, String value) throws IOException { return Money.parse(value, MonetaryFormats.getAmountFormat(LocaleContextHolder.getLocale())); } }
" checks results for different currencies") public void testParseWithDifferentCurrencies() { for (Locale locale : MonetaryFormats.getAvailableLocales()) { MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(locale); for (MonetaryAmountFactory fact : Monetary.getAmountFactories()) { if (fact.getAmountType().equals(TestAmount.class)) {
/** * Checks if a {@link MonetaryAmountFormat} is available for the given {@link Locale} and providers. * * @param locale the target {@link Locale}, not {@code null}. * @param providers The providers to be queried, if not set the providers as defined by #getDefaultCurrencyProviderChain() * are queried. * @return true, if a corresponding {@link MonetaryAmountFormat} is accessible. */ public static boolean isAvailable(Locale locale, String... providers) { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .isAvailable(locale, providers); }
@Override public void serialize(MonetaryAmount value, JsonGenerator jgen, SerializerProvider provider) throws IOException { if (value != null) { jgen.writeString(MonetaryFormats.getAmountFormat(LocaleContextHolder.getLocale()).format(value)); } else { jgen.writeNull(); } }
/** * Checks if a {@link MonetaryAmountFormat} is available for the given {@link javax.money.format.AmountFormatQuery}. * * @param formatQuery the required {@link AmountFormatQuery}, not {@code null}. If the query does not define * any explicit provider chain, the providers as defined by #getDefaultCurrencyProviderChain() * are used. * @return true, if a corresponding {@link MonetaryAmountFormat} is accessible. */ public static boolean isAvailable(AmountFormatQuery formatQuery) { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .isAvailable(formatQuery); }
@Override public MonetaryAmountFormat create(final Locale defaultLocale) { return MonetaryFormats.getAmountFormat(defaultLocale); }
/** * Access all {@link MonetaryAmountFormat} instances that match the given a {@link AmountFormatQuery}. * * @param formatQuery the required {@link AmountFormatQuery}, not {@code null}. If the query does not define * any explicit provider chain, the providers as defined by #getDefaultCurrencyProviderChain() * are used. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ public static Collection<MonetaryAmountFormat> getAmountFormats(AmountFormatQuery formatQuery) { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .getAmountFormats(formatQuery); }
@Override public MonetaryAmountFormat create(final Locale defaultLocale) { return MonetaryFormats.getAmountFormat(defaultLocale); }
/** * Access the default {@link MonetaryAmountFormat} given a {@link Locale}. * * @param formatQuery the required {@link AmountFormatQuery}, not {@code null}. If the query does not define * any explicit provider chain, the providers as defined by #getDefaultCurrencyProviderChain() * are used. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ public static MonetaryAmountFormat getAmountFormat(AmountFormatQuery formatQuery) { return Optional.ofNullable(getMonetaryFormatsSpi()).orElseThrow(() -> new MonetaryException( "No MonetaryFormatsSingletonSpi " + "loaded, query functionality is not available.")) .getAmountFormat(formatQuery); }
/** * Obtain a MonetaryAmountFormat for the given locale. * <p>The default implementation simply calls * {@link javax.money.format.MonetaryFormats#getAmountFormat} * with either the configured format name or the given locale. * @param locale the current locale * @return the MonetaryAmountFormat (never {@code null}) * @see #setFormatName */ protected MonetaryAmountFormat getMonetaryAmountFormat(Locale locale) { if (this.formatName != null) { return MonetaryFormats.getAmountFormat(this.formatName); } else { return MonetaryFormats.getAmountFormat(locale); } }