Top Description Fields Constructors Methods
org.javamoney.moneta.spi.format

pack-priv final Class DefaultMonetaryAmountFormat

extends Object
implements MonetaryAmountFormat
Class Inheritance
All Implemented Interfaces
javax.money.format.MonetaryAmountFormat, javax.money.MonetaryQuery
Imports
java.io.IOException, java.text.DecimalFormat, .DecimalFormatSymbols, java.util.ArrayList, .List, .Locale, .Objects, java.util.logging.Logger, javax.money.*, javax.money.format.AmountFormatContext, .AmountFormatContextBuilder, .MonetaryAmountFormat, .MonetaryParseException, org.javamoney.moneta.format.AmountFormatParams, .CurrencyStyle, org.javamoney.moneta.spi.MoneyUtils

Formats instances of MonetaryAmount to a String or an Appendable.

Instances of this class are not thread-safe. Basically when using MonetaryAmountFormat instances a new instance should be created on each access. When parsing currencies this format supports an optional currencyProviderName context parameter on the AmountFormatContext. This name will be passed as a currency trarget provider when resolving currency codes and symbols.

Authors
Anatole Tresch, Werner Keil

Field Summary

Modifier and TypeField and Description
private AmountFormatContext
private static final char
CURRENCY_SIGN

The international Unicode currency sign.

private List<FormatToken>
negativeTokens

The tokens to be used for formatting/parsing of negative numbers.

private List<FormatToken>
positiveTokens

The tokens to be used for formatting/parsing of positive and zero numbers.

private static final char
SUBPATTERN_BOUNDARY

Separates positive and negative subpatterns

Constructor Summary

AccessConstructor and Description
pack-priv
DefaultMonetaryAmountFormat(AmountFormatContext
the javax.money.format.AmountFormatContext to be used, not null.
amountFormatContext
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the string printed using the settings of this formatter
format
(MonetaryAmount
the amount to print, not null
amount
)

Overrides default javax.money.format.MonetaryAmountFormat.format.

Formats a value of T to a String.

public AmountFormatContext
private String
private List<FormatToken>
private boolean
private boolean
public MonetaryAmount

Returns:

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

Implements javax.money.format.MonetaryAmountFormat.parse.

Fully parses the text into an instance of MonetaryAmount.

public String
queryFrom(MonetaryAmount amount)

Implements javax.money.MonetaryQuery.queryFrom.

Queries the specified monetary amount.

private String
resolvePattern(AmountFormatContext amountFormatContext)

private void
private String[]
splitIntoPlusMinusPatterns(AmountFormatContext amountFormatContext, String pattern)

Split into (potential) plus, minus patterns

private List<String>

Returns:

the tokenized list.
splitNumberPattern
(List<String>
the token identified so far.
tokens
,
DecimalFormat
the target locale.
format
)

Splits away the number pattern for targeting an AmountToken, if possible.

private List<String>
splitPatternForCurrency(String pattern)

Splits the given pattern into a prefix, a currency token and a postfix token.

private List<String>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

amountFormatContextback to summary
private AmountFormatContext amountFormatContext

The current javax.money.format.AmountFormatContext, never null.

CURRENCY_SIGNback to summary
private static final char CURRENCY_SIGN

The international Unicode currency sign.

negativeTokensback to summary
private List<FormatToken> negativeTokens

The tokens to be used for formatting/parsing of negative numbers.

positiveTokensback to summary
private List<FormatToken> positiveTokens

The tokens to be used for formatting/parsing of positive and zero numbers.

SUBPATTERN_BOUNDARYback to summary
private static final char SUBPATTERN_BOUNDARY

Separates positive and negative subpatterns

Constructor Detail

DefaultMonetaryAmountFormatback to summary
pack-priv DefaultMonetaryAmountFormat(AmountFormatContext amountFormatContext)

Creates a new instance.

Parameters
amountFormatContext:AmountFormatContext

the javax.money.format.AmountFormatContext to be used, not null.

Method Detail

formatback to summary
public String format(MonetaryAmount amount)

Overrides default javax.money.format.MonetaryAmountFormat.format.

Formats a value of T to a String. java.util.Locale passed defines the overall target Locale. This locale state, how the MonetaryAmountFormat should generally behave. The java.util.Locale allows to configure the formatting and parsing in arbitrary details. The attributes that are supported are determined by the according MonetaryAmountFormat implementation:

Parameters
amount:MonetaryAmount

the amount to print, not null

Returns:String

the string printed using the settings of this formatter

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the formatter is unable to print
getContextback to summary
public AmountFormatContext getContext()

Implements javax.money.format.MonetaryAmountFormat.getContext.

Doc from javax.money.format.MonetaryAmountFormat.getContext.

The AmountFormatContext to be applied when a MonetaryAmount is formatted.

Annotations
@Override
getNumberPatternback to summary
private String getNumberPattern(String token, DecimalFormat format)
initPatternback to summary
private List<FormatToken> initPattern(String pattern, AmountFormatContext context)
isCurrencyTokenback to summary
private boolean isCurrencyToken(String token)
isNumberTokenback to summary
private boolean isNumberToken(String token)
parseback to summary
public MonetaryAmount parse(CharSequence text) throws MonetaryParseException

Implements javax.money.format.MonetaryAmountFormat.parse.

Fully parses the text into an instance of MonetaryAmount.

The parse must complete normally and parse the entire text. 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:MonetaryAmount

the parsed value, never null

Annotations
@Override
Exceptions
MonetaryParseException:
if there is a problem while parsing
UnsupportedOperationException:
if the formatter is unable to parse
queryFromback to summary
public String queryFrom(MonetaryAmount amount)

Implements javax.money.MonetaryQuery.queryFrom.

Doc from javax.money.MonetaryQuery.queryFrom.

Queries the specified monetary amount.

This queries the specified monetary amount to return an object using the logic encapsulated in the implementing class. Examples might be a query that checks if the amount is positive, or one that extracts the currency as a symbol.

There are two equivalent ways of using a MonetaryQuery. The first is to invoke the method on this interface. The second is to use MonetaryAmount#query(MonetaryQuery):

// these two lines are equivalent, but the second approach is recommended
monetary = thisQuery.queryFrom(monetary);
monetary = monetary.query(thisQuery);
It is recommended to use the second approach, query(MonetaryQuery), as it is a lot clearer to read in code.

Implementation specification

The implementation must take the input object and query it. The implementation defines the logic of the query and is responsible for documenting that logic. It may use any method on MonetaryAmount to determine the result. The input object must not be altered.

This method may be called from multiple threads in parallel. It must be thread-safe when invoked.

Annotations
@Override
resolvePatternback to summary
private String resolvePattern(AmountFormatContext amountFormatContext)
setAmountFormatContextback to summary
private void setAmountFormatContext(AmountFormatContext amountFormatContext)
splitIntoPlusMinusPatternsback to summary
private String[] splitIntoPlusMinusPatterns(AmountFormatContext amountFormatContext, String pattern)

Split into (potential) plus, minus patterns

splitNumberPatternback to summary
private List<String> splitNumberPattern(List<String> tokens, DecimalFormat format)

Splits away the number pattern for targeting an AmountToken, if possible.

Parameters
tokens:List<String>

the token identified so far.

format:DecimalFormat

the target locale.

Returns:List<String>

the tokenized list.

splitPatternForCurrencyback to summary
private List<String> splitPatternForCurrency(String pattern)

Splits the given pattern into a prefix, a currency token and a postfix token.

tokenizePatternback to summary
private List<String> tokenizePattern(String pattern, DecimalFormat format)