public interface MonetaryAmountFactory<T extends MonetaryAmount>
Factory for
MonetaryAmount instances for a given type. It can be accessed, by
- calling
MonetaryAmount.getFactory(), returning aMonetaryAmountFactorycreating amounts of the same implementation type, which also provided the factory instance. - calling
Monetary.getAmountFactory(Class)accessing aMonetaryAmountFactoryfor a concrete typeClass<T>. - calling
Monetary.getDefaultAmountFactory()accessing a defaultMonetaryAmountFactory.
Implementations of this interface allow to get MonetaryAmount instances providing
different data as required:
- the
CurrencyUnit, or the corresponding currency code (must be solvable byMonetary). - the number part
- the
MonetaryContext - by passing any
MonetaryAmountinstance, it is possible to convert an arbitrary amount implementation to the implementation provided by this factory. If the current factory cannot support the precision/scale as required by the currentNumberValueaMonetaryExceptionmust be thrown.
MonetaryContext is used, which can also be configured by adding
configuration to a file /javamoney.properties to the classpath.
Hereby the entries. e.g. for a class MyMoney should start with a.b.MyMoney.ctx. The entries valid
must be documented
on the according implementation class, where the following entries are defined for all implementation types
(example below given for a class a.b.MyMoney:
a.b.MyMoney.ctx.precisionto define the maximal supported precision.a.b.MyMoney.ctx.maxScaleto define the maximal supported scale.a.b.MyMoney.ctx.fixedScaleto define the scale to be fixed (constant).
Implementation specification
Instances of this interface are not required to be thread-safe!- Version:
- 1.0.0
- Author:
- Anatole Tresch, Werner Keil
-
Method Summary
Modifier and Type Method Description Tcreate()Creates a new instance ofMonetaryAmount, using the current data set on this factory.java.lang.Class<? extends MonetaryAmount>getAmountType()Access theMonetaryAmountimplementation type.MonetaryContextgetDefaultMonetaryContext()Returns the defaultMonetaryContextused, when noMonetaryContextis provided.default MonetaryContextgetMaximalMonetaryContext()Returns the maximalMonetaryContextsupported, for requests that exceed these maximal capabilities, anArithmeticExceptionmust be thrown.NumberValuegetMaxNumber()Get the maximum possible number that this type can represent.NumberValuegetMinNumber()Get the minimum possible number that this type can represent.default MonetaryAmountFactory<T>setAmount(MonetaryAmount amount)Uses an arbitraryMonetaryAmountto initialize this factory.MonetaryAmountFactory<T>setContext(MonetaryContext monetaryContext)Sets theMonetaryContextto be used.default MonetaryAmountFactory<T>setCurrency(java.lang.String currencyCode)Sets theCurrencyUnitto be used.MonetaryAmountFactory<T>setCurrency(CurrencyUnit currency)Sets theCurrencyUnitto be used.MonetaryAmountFactory<T>setNumber(double number)Sets the number to be used.MonetaryAmountFactory<T>setNumber(long number)Sets the number to be used.MonetaryAmountFactory<T>setNumber(java.lang.Number number)Sets the number to be used.
-
Method Details
-
getAmountType
java.lang.Class<? extends MonetaryAmount> getAmountType()Access theMonetaryAmountimplementation type.- Returns:
- the
MonetaryAmountimplementation type, nevernull.
-
setCurrency
Sets theCurrencyUnitto be used.- Parameters:
currencyCode- the currencyCode of the currency to be used, notnull. The currency code will be resolved usingMonetary.getCurrency(String, String...).- Returns:
- This factory instance, for chaining.
- Throws:
UnknownCurrencyException- if thecurrencyCodeis not resolvable.
-
setCurrency
Sets theCurrencyUnitto be used.- Parameters:
currency- theCurrencyUnitto be used, notnull- Returns:
- This factory instance, for chaining.
-
setNumber
Sets the number to be used.- Parameters:
number- the number to be used- Returns:
- This factory instance, for chaining.
-
setNumber
Sets the number to be used.- Parameters:
number- the number to be used- Returns:
- This factory instance, for chaining.
-
setNumber
Sets the number to be used.- Parameters:
number- the number to be used, notnull.- Returns:
- This factory instance, for chaining.
-
getMaxNumber
NumberValue getMaxNumber()Get the maximum possible number that this type can represent. If the numeric model has no limitations on the numeric range, null should be returned. IfMonetaryContext.getPrecision()returns a value > 0 this method is required to provide a maximal amount.- Returns:
- the maximum possible number, or null.
-
getMinNumber
NumberValue getMinNumber()Get the minimum possible number that this type can represent. If the numeric model has no limitations on the numeric range, null should be returned. IfMonetaryContext.getPrecision()returns a value > 0 this method is required to provide a maximal amount.- Returns:
- the minimum possible number, or null.
-
setContext
Sets theMonetaryContextto be used.- Parameters:
monetaryContext- theMonetaryContextto be used, notnull.- Returns:
- This factory instance, for chaining.
- Throws:
MonetaryException- when theMonetaryContextgiven exceeds the capabilities supported by this factory type.- See Also:
getMaximalMonetaryContext()
-
setAmount
Uses an arbitraryMonetaryAmountto initialize this factory. Properties reused are:- CurrencyUnit,
- Number value,
- MonetaryContext.
- Parameters:
amount- the amount to be used, notnull.- Returns:
- this factory instance, for chaining.
- Throws:
MonetaryException- when theMonetaryContextimplied byamount.getContext()exceeds the capabilities supported by this factory type.
-
create
T create()Creates a new instance ofMonetaryAmount, using the current data set on this factory.- Returns:
- the corresponding
MonetaryAmount. - See Also:
getAmountType()
-
getDefaultMonetaryContext
MonetaryContext getDefaultMonetaryContext()Returns the defaultMonetaryContextused, when noMonetaryContextis provided.The default context is not allowed to exceed the capabilities of the maximal
MonetaryContextsupported.- Returns:
- the default
MonetaryContext, nevernull. - See Also:
getMaximalMonetaryContext()
-
getMaximalMonetaryContext
Returns the maximalMonetaryContextsupported, for requests that exceed these maximal capabilities, anArithmeticExceptionmust be thrown.- Returns:
- the maximal
MonetaryContextsupported, nevernull
-