/** * 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); } }
@Override public String print(MonetaryAmount object, Locale locale) { return getMonetaryAmountFormat(locale).format(object); }
@Override public MonetaryAmount parse(String text, Locale locale) { return getMonetaryAmountFormat(locale).parse(text); }
/** * Access the default {@link MonetaryAmountFormat} given a {@link Locale}. * * @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 the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ public static MonetaryAmountFormat getAmountFormat(Locale locale, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(locale).setProviderNames(providers).setLocale(locale).build()); }
@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(); } }
/** * Creates a simple format query based on a singlet Locale, similar to {@link java.text.DecimalFormat#getInstance * (java.util.Locale)}. * * @param locale the target locale, not null. * @param providers the providers to be used, not null. * @return a new query instance */ public static AmountFormatQuery of(Locale locale, String... providers) { return AmountFormatQueryBuilder.of(locale).setProviderNames(providers).build(); }
/** * Sets a Locale to be applied. * * @param locale the locale, not null. * @return the Builder, for chaining. */ public AmountFormatQueryBuilder setLocale(Locale locale) { return set(locale); }
/** * Access the context's Locale. * * @return the Locale, or null. */ public Locale getLocale() { return get(Locale.class); }
/** * Creates a new {@link AmountFormatContextBuilder}. * * @param formatQuery the base {@link AmountFormatQuery}, not {@code null}. */ private AmountFormatContextBuilder(AmountFormatQuery formatQuery) { importContext(formatQuery); }
/** * Creates a new {@link AmountFormatContextBuilder}. * * @param formatName the target formatName {@link String}, not {@code null}. * @return a new {@link javax.money.format.AmountFormatContextBuilder} instance, never null. */ public static AmountFormatContextBuilder of(String formatName) { return new AmountFormatContextBuilder(formatName); }
/** * Creates a new {@link AmountFormatQueryBuilder} and initializes it with the values from {@code formatQuery. * * @param formatQuery the base {@link AmountFormatContext}, not {@code null}. * @return a new {@link javax.money.format.AmountFormatQueryBuilder} instance, never null. */ public static AmountFormatQueryBuilder of(AmountFormatQuery formatQuery) { return new AmountFormatQueryBuilder(formatQuery); }
/** * Gets a Locale to be applied. * * @return the style's locale, or null. */ public Locale getLocale() { return get(Locale.class); }
/** * Access the a {@link MonetaryAmountFormat} given its styleId. * * @param formatName the target format name, not {@code null}. * @param providers The providers to be used, if not set 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(String formatName, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(formatName).setProviderNames(providers).build()); }
/** * 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); } }
@Override public String print(MonetaryAmount object, Locale locale) { return getMonetaryAmountFormat(locale).format(object); }
@Override public MonetaryAmount parse(String text, Locale locale) { return getMonetaryAmountFormat(locale).parse(text); }
/** * 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); }
/** * Creates a new default {@link AmountFormatContextBuilder} for a formatter based on the locale specific * defaults. * * @param locale the target {@link java.util.Locale}, not {@code null}. * @return a new {@link javax.money.format.AmountFormatContextBuilder} instance, never null. */ public static AmountFormatContextBuilder of(Locale locale) { return new AmountFormatContextBuilder(locale); } }
@Override public Object createFromString(DeserializationContext context, String value) throws IOException { return Money.parse(value, MonetaryFormats.getAmountFormat(LocaleContextHolder.getLocale())); } }
/** * Creates a new {@link AmountFormatContextBuilder}. * * @param formatQuery the base {@link AmountFormatContext}, not {@code null}. * @return a new {@link javax.money.format.AmountFormatContextBuilder} instance, never null. */ public static AmountFormatContextBuilder create(AmountFormatQuery formatQuery) { return new AmountFormatContextBuilder(formatQuery); }