org.javamoney.moneta.spi.base.BaseMonetaryAmountFormat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moneta-bp Show documentation
Show all versions of moneta-bp Show documentation
JSR 354 provides an API for representing, transporting, and performing comprehensive calculations with
Money and Currency.
This module implements JSR 354.
/*
* CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE CONDITION THAT YOU
* ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT. PLEASE READ THE TERMS AND CONDITIONS OF THIS
* AGREEMENT CAREFULLY. BY DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF
* THE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE" BUTTON AT THE
* BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency API ("Specification") Copyright
* (c) 2012-2013, Credit Suisse All rights reserved.
*/
package org.javamoney.moneta.spi.base;
import javax.money.MonetaryAmount;
import javax.money.format.MonetaryAmountFormat;
import java.io.IOException;
/**
*
* Formats instances of {@code MonetaryAmount} to a {@link String} or an {@link Appendable}.
*
*
* To obtain a MonetaryAmountFormat
for a specific locale, including the default
* locale, call {@link javax.money.format.MonetaryFormats#getAmountFormat(java.util.Locale, String...)}.
*
* More complex formatting scenarios can be implemented by registering instances of {@link javax.money.spi
* .MonetaryAmountFormatProviderSpi}.
* The spi implementation creates new instances of {@link BaseMonetaryAmountFormat} based on the
* styleId and (arbitrary) attributes passed within the {@link javax.money.format.AmountFormatContext}.
*
* In general, do prefer
* accessing MonetaryAmountFormat
instances from the {@link javax.money.format.MonetaryFormats} singleton,
* instead of instantiating implementations directly, since the MonetaryFormats
factory
* method may return different subclasses or may implement contextual behaviour (in a EE context).
* If you need to customize the format object, do something like this:
*
*
*
*
* MonetaryAmountFormat f = MonetaryFormats.getInstance(loc);
* f.setStyle(f.getStyle().toBuilder().setPattern("###.##;(###.##)").build());
*
*
*
*
* Special Values
*
*
* Negative zero ("-0"
) should always parse to
*
* 0
*
*
* Synchronization
*
*
* Instances of this class are not required to be thread-safe. It is recommended to of separate
* format instances for each thread. If multiple threads access a format concurrently, it must be
* synchronized externally.
*
* Example
*
* {@code
* // Print out a number using the localized number, currency,
* // for each locale
* Locale[] locales = MonetaryFormats.getAvailableLocales();
* MonetaryAmount amount = ...;
* MonetaryAmountFormat form;
* System.out.println("FORMAT");
* for (int i = 0; i < locales.length; ++i) {
* if (locales[i].getCountry().length() == 0) {
* continue; // Skip language-only locales
* }
* System.out.print(locales[i].getDisplayName());
* form = MonetaryFormats.getInstance(locales[i]);
* System.out.print(": " + form.getStyle().getPattern());
* String myAmount = form.format(amount);
* System.out.print(" -> " + myAmount);
* try {
* System.out.println(" -> " + form.parse(form.format(myAmount)));
* } catch (ParseException e) {}
* }
* }
* }
*/
public abstract class BaseMonetaryAmountFormat implements MonetaryAmountFormat{
/**
* Formats the given {@link javax.money.MonetaryAmount} to a String.
*
* @param amount the amount to format, not {@code null}
* @return the string printed using the settings of this formatter
* @throws UnsupportedOperationException if the formatter is unable to print
*/
public String format(MonetaryAmount amount){
StringBuilder b = new StringBuilder();
try{
print(b, amount);
}
catch(IOException e){
throw new IllegalStateException("Formatting error.", e);
}
return b.toString();
}
}