Top Description Fields Constructors Methods
org.joda.money.format

public final Class MoneyFormatter

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .Serializable, java.util.Locale, org.joda.money.BigMoney, .BigMoneyProvider, .Money

Formats instances of money to and from a String.

Instances of MoneyFormatter can be created by MoneyFormatterBuilder.

This class is immutable and thread-safe.

Field Summary

Modifier and TypeField and Description
private final Locale
locale

The locale to use.

private final MultiPrinterParser
printerParser

The printer/parser.

private static final long
serialVersionUID

Serialization version.

Constructor Summary

AccessConstructor and Description
pack-priv
MoneyFormatter(Locale
the locale to use, not null
locale
,
MoneyPrinter[]
the printers, not null
printers
,
MoneyParser[]
the parsers, not null
parsers
)

Constructor, creating a new formatter.

private
MoneyFormatter(Locale
the locale to use, not null
locale
,
MultiPrinterParser
the printer/parser, not null
printerParser
)

Constructor, creating a new formatter.

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
checkNotNull(Object
the object to check, null throws exception
object
,
String
the message to use in the exception, not null
message
)

Validates that the object specified is not null

public Locale

Returns:

the locale, never null
getLocale
()

Gets the locale to use.

pack-priv MultiPrinterParser

Returns:

the printer/parser, never null
getPrinterParser
()

Gets the printer/parser.

public boolean

Returns:

true if the formatter can parse
isParser
()

Checks whether this formatter can parse.

public boolean

Returns:

true if the formatter can print
isPrinter
()

Checks whether this formatter can print.

public MoneyParseContext

Returns:

the parsed monetary value, null only if the parse results in an error
parse
(CharSequence
the text to parse, not null
text
,
int
the start index to parse from
startIndex
)

Parses the text extracting monetary information.

public BigMoney

Returns:

the parsed monetary value, never null
parseBigMoney
(CharSequence
the text to parse, not null
text
)

Fully parses the text into a BigMoney.

public Money

Returns:

the parsed monetary value, never null
parseMoney
(CharSequence
the text to parse, not null
text
)

Fully parses the text into a Money requiring that the parsed amount has the correct number of decimal places.

public void
printIO(Appendable
the appendable to add to, not null
appendable
,
BigMoneyProvider
the money to print, not null
moneyProvider
)

Prints a monetary value to an Appendable potentially throwing an IOException.

public String

Returns:

a string summarising the formatter, never null
toString
()

Overrides java.lang.Object.toString.

Gets a string summary of the formatter.

public MoneyFormatter

Returns:

the new instance, never null
withLocale
(Locale
the locale, not null
locale
)

Returns a copy of this instance with the specified locale.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

localeback to summary
private final Locale locale

The locale to use.

printerParserback to summary
private final MultiPrinterParser printerParser

The printer/parser.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

Constructor Detail

MoneyFormatterback to summary
pack-priv MoneyFormatter(Locale locale, MoneyPrinter[] printers, MoneyParser[] parsers)

Constructor, creating a new formatter.

Parameters
locale:Locale

the locale to use, not null

printers:MoneyPrinter[]

the printers, not null

parsers:MoneyParser[]

the parsers, not null

MoneyFormatterback to summary
private MoneyFormatter(Locale locale, MultiPrinterParser printerParser)

Constructor, creating a new formatter.

Parameters
locale:Locale

the locale to use, not null

printerParser:MultiPrinterParser

the printer/parser, not null

Method Detail

checkNotNullback to summary
pack-priv static void checkNotNull(Object object, String message)

Validates that the object specified is not null

Parameters
object:Object

the object to check, null throws exception

message:String

the message to use in the exception, not null

Exceptions
NullPointerException:
if the input value is null
getLocaleback to summary
public Locale getLocale()

Gets the locale to use.

Returns:Locale

the locale, never null

getPrinterParserback to summary
pack-priv MultiPrinterParser getPrinterParser()

Gets the printer/parser.

Returns:MultiPrinterParser

the printer/parser, never null

isParserback to summary
public boolean isParser()

Checks whether this formatter can parse.

If the formatter cannot parse, an UnsupportedOperationException will be thrown from the parse methods.

Returns:boolean

true if the formatter can parse

isPrinterback to summary
public boolean isPrinter()

Checks whether this formatter can print.

If the formatter cannot print, an UnsupportedOperationException will be thrown from the print methods.

Returns:boolean

true if the formatter can print

parseback to summary
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:CharSequence

the text to parse, not null

startIndex:int

the start index to parse from

Returns:MoneyParseContext

the parsed monetary value, null only if the parse results in an error

Exceptions
IndexOutOfBoundsException:
if the start index is invalid
UnsupportedOperationException:
if this formatter cannot parse
parseBigMoneyback to summary
public BigMoney parseBigMoney(CharSequence text)

Fully parses the text into a BigMoney.

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:CharSequence

the text to parse, not null

Returns:BigMoney

the parsed monetary value, never null

Exceptions
UnsupportedOperationException:
if the formatter is unable to parse
MoneyFormatException:
if there is a problem while parsing
parseMoneyback to summary
public Money parseMoney(CharSequence text)

Fully parses the text into a Money 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:CharSequence

the text to parse, not null

Returns:Money

the parsed monetary value, never null

Exceptions
UnsupportedOperationException:
if the formatter is unable to parse
MoneyFormatException:
if there is a problem while parsing
ArithmeticException:
if the scale of the parsed money exceeds the scale of the currency
printIOback to summary
public void printIO(Appendable appendable, BigMoneyProvider moneyProvider) throws IOException

Prints a monetary value to an Appendable potentially throwing an IOException.

Example implementations of Appendable are StringBuilder, StringBuffer or Writer. Note that StringBuilder and StringBuffer never throw an IOException.

Parameters
appendable:Appendable

the appendable to add to, not null

moneyProvider:BigMoneyProvider

the money to print, not null

Exceptions
IOException:
if an IO error occurs
UnsupportedOperationException:
if the formatter is unable to print
MoneyFormatException:
if there is a problem while printing
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets a string summary of the formatter.

Returns:String

a string summarising the formatter, never null

Annotations
@Override
withLocaleback to summary
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:Locale

the locale, not null

Returns:MoneyFormatter

the new instance, never null