public interface MonetaryAmountFactory<T extends MonetaryAmount>
Factory for
MonetaryAmount
instances for a given type. It can be accessed, by
- calling
MonetaryAmount.getFactory()
, returning aMonetaryAmountFactory
creating amounts of the same implementation type, which also provided the factory instance. - calling
Monetary.getAmountFactory(Class)
accessing aMonetaryAmountFactory
for 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
MonetaryAmount
instance, 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 currentNumberValue
aMonetaryException
must 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.precision
to define the maximal supported precision.a.b.MyMoney.ctx.maxScale
to define the maximal supported scale.a.b.MyMoney.ctx.fixedScale
to 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 T
create()
Creates a new instance ofMonetaryAmount
, using the current data set on this factory.java.lang.Class<? extends MonetaryAmount>
getAmountType()
Access theMonetaryAmount
implementation type.MonetaryContext
getDefaultMonetaryContext()
Returns the defaultMonetaryContext
used, when noMonetaryContext
is provided.default MonetaryContext
getMaximalMonetaryContext()
Returns the maximalMonetaryContext
supported, for requests that exceed these maximal capabilities, anArithmeticException
must be thrown.NumberValue
getMaxNumber()
Get the maximum possible number that this type can represent.NumberValue
getMinNumber()
Get the minimum possible number that this type can represent.default MonetaryAmountFactory<T>
setAmount(MonetaryAmount amount)
Uses an arbitraryMonetaryAmount
to initialize this factory.MonetaryAmountFactory<T>
setContext(MonetaryContext monetaryContext)
Sets theMonetaryContext
to be used.default MonetaryAmountFactory<T>
setCurrency(java.lang.String currencyCode)
Sets theCurrencyUnit
to be used.MonetaryAmountFactory<T>
setCurrency(CurrencyUnit currency)
Sets theCurrencyUnit
to 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 theMonetaryAmount
implementation type.- Returns:
- the
MonetaryAmount
implementation type, nevernull
.
-
setCurrency
Sets theCurrencyUnit
to 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 thecurrencyCode
is not resolvable.
-
setCurrency
Sets theCurrencyUnit
to be used.- Parameters:
currency
- theCurrencyUnit
to 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 theMonetaryContext
to be used.- Parameters:
monetaryContext
- theMonetaryContext
to be used, notnull
.- Returns:
- This factory instance, for chaining.
- Throws:
MonetaryException
- when theMonetaryContext
given exceeds the capabilities supported by this factory type.- See Also:
getMaximalMonetaryContext()
-
setAmount
Uses an arbitraryMonetaryAmount
to 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 theMonetaryContext
implied 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 defaultMonetaryContext
used, when noMonetaryContext
is provided.The default context is not allowed to exceed the capabilities of the maximal
MonetaryContext
supported.- Returns:
- the default
MonetaryContext
, nevernull
. - See Also:
getMaximalMonetaryContext()
-
getMaximalMonetaryContext
Returns the maximalMonetaryContext
supported, for requests that exceed these maximal capabilities, anArithmeticException
must be thrown.- Returns:
- the maximal
MonetaryContext
supported, nevernull
-