Class MoneyFormatter
- All Implemented Interfaces:
Serializable
Instances of MoneyFormatter can be created by
MoneyFormatterBuilder.
This class is immutable and thread-safe.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LocaleThe locale to use.private final MultiPrinterParserThe printer/parser.private static final longSerialization version. -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)MoneyFormatter(Locale locale, MoneyPrinter[] printers, MoneyParser[] parsers) Constructor, creating a new formatter.privateMoneyFormatter(Locale locale, MultiPrinterParser printerParser) Constructor, creating a new formatter. -
Method Summary
Modifier and TypeMethodDescription(package private) static voidcheckNotNull(Object object, String message) Validates that the object specified is not nullGets the locale to use.(package private) MultiPrinterParserGets the printer/parser.booleanisParser()Checks whether this formatter can parse.booleanChecks whether this formatter can print.parse(CharSequence text, int startIndex) Parses the text extracting monetary information.parseBigMoney(CharSequence text) Fully parses the text into aBigMoney.parseMoney(CharSequence text) Fully parses the text into aMoneyrequiring that the parsed amount has the correct number of decimal places.voidprint(Appendable appendable, BigMoneyProvider moneyProvider) Prints a monetary value to anAppendableconverting anyIOExceptionto aMoneyFormatException.print(BigMoneyProvider moneyProvider) Prints a monetary value to aString.voidprintIO(Appendable appendable, BigMoneyProvider moneyProvider) Prints a monetary value to anAppendablepotentially throwing anIOException.toString()Gets a string summary of the formatter.withLocale(Locale locale) Returns a copy of this instance with the specified locale.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version.- See Also:
-
locale
The locale to use. -
printerParser
The printer/parser.
-
-
Constructor Details
-
MoneyFormatter
MoneyFormatter(Locale locale, MoneyPrinter[] printers, MoneyParser[] parsers) Constructor, creating a new formatter.- Parameters:
locale- the locale to use, not nullprinters- the printers, not nullparsers- the parsers, not null
-
MoneyFormatter
Constructor, creating a new formatter.- Parameters:
locale- the locale to use, not nullprinterParser- the printer/parser, not null
-
-
Method Details
-
checkNotNull
Validates that the object specified is not null- Parameters:
object- the object to check, null throws exceptionmessage- the message to use in the exception, not null- Throws:
NullPointerException- if the input value is null
-
getPrinterParser
MultiPrinterParser getPrinterParser()Gets the printer/parser.- Returns:
- the printer/parser, never null
-
getLocale
-
withLocale
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
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
Prints a monetary value to anAppendableconverting anyIOExceptionto aMoneyFormatException.Example implementations of
AppendableareStringBuilder,StringBufferorWriter. Note thatStringBuilderandStringBuffernever 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
Prints a monetary value to anAppendablepotentially throwing anIOException.Example implementations of
AppendableareStringBuilder,StringBufferorWriter. Note thatStringBuilderandStringBuffernever 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
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
Fully parses the text into aMoneyrequiring 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
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
-
toString
-