public interface MonetaryAdjuster
Adjusters are a key tool for modifying monetary amounts. They match the strategy design pattern, allowing different types of adjustment to be easily captured. Examples might be an adjuster that rounds the amount to the nearest 1000, or one that performs currency conversion.
There are two equivalent ways of using a MonetaryAdjuster
. The first
is to invoke the method on this interface. The second is to use
MonetaryAmount.with(MonetaryAdjuster)
, whereas implementations of
MonetaryAmount.with(MonetaryAdjuster)
must return the concrete type,
instead of the interface to support a fluent style:
// these two variants are equivalent monetary = thisAdjuster.adjustInto(monetary); monetary = anotherAdjuster.adjustInto(monetary); // second, recommended, approach, using a fluent API monetary = monetary.with(thisAdjuster).with(anotherAdjuster);It is recommended to use the second approach,
with(MonetaryAdjuster)
,
as it is a lot clearer to read in code.
Modifier and Type | Method and Description |
---|---|
MonetaryAmount |
adjustInto(MonetaryAmount amount)
Adjusts the specified monetary object.
|
MonetaryAmount adjustInto(MonetaryAmount amount)
This adjusts the specified monetary object using the logic encapsulated in the implementing class. Examples might be an adjuster that rounds the amount to the nearest 1000, or one that performs currency conversion.
There are two equivalent ways of using a MonetaryAdjuster
. The
first is to invoke the method on this interface. The second is to use
MonetaryAmount.with(MonetaryAdjuster)
:
// these two lines are equivalent, but the second approach is recommended monetary = thisAdjuster.adjustInto(monetary); monetary = monetary.with(thisAdjuster);It is recommended to use the second approach,
with(MonetaryAdjuster)
, as it is a lot clearer to read in code.
MonetaryAmount
to determine the result.
The input object must not be altered. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable monetary amounts.
This method may be called from multiple threads in parallel. It must be thread-safe when invoked.
amount
- the amount to adjust, not nullMonetaryException
- if unable to make the adjustmentArithmeticException
- if numeric overflow occursCopyright © 2012–2013 JSR 354 - Expert Group. All rights reserved.