- java.lang.Object
-
- org.joda.money.format.MoneyFormatter
-
- All Implemented Interfaces:
Serializable
public final class MoneyFormatter extends Object implements Serializable
Formats instances of money to and from a String.Instances of
MoneyFormatter
can be created byMoneyFormatterBuilder
.This class is immutable and thread-safe.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Locale
getLocale()
Gets the locale to use.boolean
isParser()
Checks whether this formatter can parse.boolean
isPrinter()
Checks whether this formatter can print.MoneyParseContext
parse(CharSequence text, int startIndex)
Parses the text extracting monetary information.BigMoney
parseBigMoney(CharSequence text)
Fully parses the text into aBigMoney
.Money
parseMoney(CharSequence text)
Fully parses the text into aMoney
requiring that the parsed amount has the correct number of decimal places.void
print(Appendable appendable, BigMoneyProvider moneyProvider)
Prints a monetary value to anAppendable
converting anyIOException
to aMoneyFormatException
.String
print(BigMoneyProvider moneyProvider)
Prints a monetary value to aString
.void
printIO(Appendable appendable, BigMoneyProvider moneyProvider)
Prints a monetary value to anAppendable
potentially throwing anIOException
.String
toString()
Gets a string summary of the formatter.MoneyFormatter
withLocale(Locale locale)
Returns a copy of this instance with the specified locale.
-
-
-
Method Detail
-
getLocale
public Locale getLocale()
Gets the locale to use.- Returns:
- the locale, never null
-
withLocale
public MoneyFormatter withLocale(Locale locale)
Returns a copy of this instance with the specified locale.Changing the locale may change the style of output depending on how the formatter has been configured.
- Parameters:
locale
- the locale, not null- Returns:
- the new instance, never null
-
isPrinter
public boolean isPrinter()
Checks whether this formatter can print.If the formatter cannot print, an UnsupportedOperationException will be thrown from the print methods.
- Returns:
- true if the formatter can print
-
isParser
public boolean isParser()
Checks whether this formatter can parse.If the formatter cannot parse, an UnsupportedOperationException will be thrown from the parse methods.
- Returns:
- true if the formatter can parse
-
print
public String print(BigMoneyProvider moneyProvider)
Prints a monetary value to aString
.- Parameters:
moneyProvider
- the money to print, not null- Returns:
- the string printed using the settings of this formatter
- Throws:
UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printing
-
print
public void print(Appendable appendable, BigMoneyProvider moneyProvider)
Prints a monetary value to anAppendable
converting anyIOException
to aMoneyFormatException
.Example implementations of
Appendable
areStringBuilder
,StringBuffer
orWriter
. Note thatStringBuilder
andStringBuffer
never throw anIOException
.- Parameters:
appendable
- the appendable to add to, not nullmoneyProvider
- the money to print, not null- Throws:
UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printing
-
printIO
public void printIO(Appendable appendable, BigMoneyProvider moneyProvider) throws IOException
Prints a monetary value to anAppendable
potentially throwing anIOException
.Example implementations of
Appendable
areStringBuilder
,StringBuffer
orWriter
. Note thatStringBuilder
andStringBuffer
never throw anIOException
.- Parameters:
appendable
- the appendable to add to, not nullmoneyProvider
- the money to print, not null- Throws:
UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printingIOException
- if an IO error occurs
-
parseBigMoney
public BigMoney parseBigMoney(CharSequence text)
Fully parses the text into aBigMoney
.The parse must complete normally and parse the entire text (currency and amount). If the parse completes without reading the entire length of the text, an exception is thrown. If any other problem occurs during parsing, an exception is thrown.
- Parameters:
text
- the text to parse, not null- Returns:
- the parsed monetary value, never null
- Throws:
UnsupportedOperationException
- if the formatter is unable to parseMoneyFormatException
- if there is a problem while parsing
-
parseMoney
public Money parseMoney(CharSequence text)
Fully parses the text into aMoney
requiring that the parsed amount has the correct number of decimal places.The parse must complete normally and parse the entire text (currency and amount). If the parse completes without reading the entire length of the text, an exception is thrown. If any other problem occurs during parsing, an exception is thrown.
- Parameters:
text
- the text to parse, not null- Returns:
- the parsed monetary value, never null
- Throws:
UnsupportedOperationException
- if the formatter is unable to parseMoneyFormatException
- if there is a problem while parsingArithmeticException
- if the scale of the parsed money exceeds the scale of the currency
-
parse
public MoneyParseContext parse(CharSequence text, int startIndex)
Parses the text extracting monetary information.This method parses the input providing low-level access to the parsing state. The resulting context contains the parsed text, indicator of error, position following the parse and the parsed currency and amount. Together, these provide enough information for higher level APIs to use.
- Parameters:
text
- the text to parse, not nullstartIndex
- the start index to parse from- Returns:
- the parsed monetary value, null only if the parse results in an error
- Throws:
IndexOutOfBoundsException
- if the start index is invalidUnsupportedOperationException
- if this formatter cannot parse
-
-