java.io.Serializable
public final class MoneyFormatter
extends java.lang.Object
implements java.io.Serializable
Instances of MoneyFormatter
can be created by
MoneyFormatterBuilder
.
This class is immutable and thread-safe.
Modifier and Type | Method | Description |
---|---|---|
java.util.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(java.lang.CharSequence text,
int startIndex) |
Parses the text extracting monetary information.
|
BigMoney |
parseBigMoney(java.lang.CharSequence text) |
Fully parses the text into a
BigMoney . |
Money |
parseMoney(java.lang.CharSequence text) |
Fully parses the text into a
Money requiring that the parsed
amount has the correct number of decimal places. |
void |
print(java.lang.Appendable appendable,
BigMoneyProvider moneyProvider) |
Prints a monetary value to an
Appendable converting
any IOException to a MoneyFormatException . |
java.lang.String |
print(BigMoneyProvider moneyProvider) |
Prints a monetary value to a
String . |
void |
printIO(java.lang.Appendable appendable,
BigMoneyProvider moneyProvider) |
Prints a monetary value to an
Appendable potentially
throwing an IOException . |
java.lang.String |
toString() |
Gets a string summary of the formatter.
|
MoneyFormatter |
withLocale(java.util.Locale locale) |
Returns a copy of this instance with the specified locale.
|
public java.util.Locale getLocale()
public MoneyFormatter withLocale(java.util.Locale locale)
Changing the locale may change the style of output depending on how the formatter has been configured.
locale
- the locale, not nullpublic boolean isPrinter()
If the formatter cannot print, an UnsupportedOperationException will be thrown from the print methods.
public boolean isParser()
If the formatter cannot parse, an UnsupportedOperationException will be thrown from the print methods.
public java.lang.String print(BigMoneyProvider moneyProvider)
String
.moneyProvider
- the money to print, not nulljava.lang.UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printingpublic void print(java.lang.Appendable appendable, BigMoneyProvider moneyProvider)
Appendable
converting
any IOException
to a MoneyFormatException
.
Example implementations of Appendable
are StringBuilder
,
StringBuffer
or Writer
. Note that StringBuilder
and StringBuffer
never throw an IOException
.
appendable
- the appendable to add to, not nullmoneyProvider
- the money to print, not nulljava.lang.UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printingpublic void printIO(java.lang.Appendable appendable, BigMoneyProvider moneyProvider) throws java.io.IOException
Appendable
potentially
throwing an IOException
.
Example implementations of Appendable
are StringBuilder
,
StringBuffer
or Writer
. Note that StringBuilder
and StringBuffer
never throw an IOException
.
appendable
- the appendable to add to, not nullmoneyProvider
- the money to print, not nulljava.lang.UnsupportedOperationException
- if the formatter is unable to printMoneyFormatException
- if there is a problem while printingjava.io.IOException
- if an IO error occurspublic BigMoney parseBigMoney(java.lang.CharSequence text)
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.
text
- the text to parse, not nulljava.lang.UnsupportedOperationException
- if the formatter is unable to parseMoneyFormatException
- if there is a problem while parsingpublic Money parseMoney(java.lang.CharSequence text)
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.
text
- the text to parse, not nulljava.lang.UnsupportedOperationException
- if the formatter is unable to parseMoneyFormatException
- if there is a problem while parsingjava.lang.ArithmeticException
- if the scale of the parsed money exceeds the scale of the currencypublic MoneyParseContext parse(java.lang.CharSequence text, int startIndex)
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.
text
- the text to parse, not nullstartIndex
- the start index to parse fromjava.lang.IndexOutOfBoundsException
- if the start index is invalidjava.lang.UnsupportedOperationException
- if this formatter cannot parsepublic java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2009–2018 Joda.org. All rights reserved.