Skip to content

Commit b23964d

Browse files
committed
2 parents 60a160f + 822784f commit b23964d

File tree

3 files changed

+93
-62
lines changed

3 files changed

+93
-62
lines changed

README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ See the home page for more details:
55
http://jcp.org/en/jsr/detail?id=354
66

77
This is the API module of JSR 354.
8+
9+
See also:
10+
http://javamoney.github.io/jsr354-api/
11+

src/main/java/javax/money/MonetaryAdjuster.java

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@
1515
/**
1616
* Strategy for adjusting a monetary amount.
1717
* <p>
18-
* Adjusters are a key tool for modifying monetary amounts.
19-
* They match the strategy design pattern, allowing different types of
20-
* adjustment to be easily captured.
21-
* Examples might be an adjuster that rounds the amount to the nearest 1000,
22-
* or one that performs currency conversion.
18+
* Adjusters are a key tool for modifying monetary amounts. They match the
19+
* strategy design pattern, allowing different types of adjustment to be easily
20+
* captured. Examples might be an adjuster that rounds the amount to the nearest
21+
* 1000, or one that performs currency conversion.
2322
* <p>
24-
* There are two equivalent ways of using a {@code MonetaryAdjuster}.
25-
* The first is to invoke the method on this interface.
26-
* The second is to use {@link MonetaryAmount#with(MonetaryAdjuster)}:
23+
* There are two equivalent ways of using a {@code MonetaryAdjuster}. The first
24+
* is to invoke the method on this interface. The second is to use
25+
* {@link MonetaryAmount#with(MonetaryAdjuster)}, whereas implementations of
26+
* {@link MonetaryAmount#with(MonetaryAdjuster)} must return the concrete type,
27+
* instead of the interface to support a fluent style:
28+
*
2729
* <pre>
28-
* // these two lines are equivalent, but the second approach is recommended
29-
* monetary = thisAdjuster.adjustInto(monetary);
30-
* monetary = monetary.with(thisAdjuster);
30+
* // these two lines are equivalent, but the second approach is recommended
31+
* monetary = thisAdjuster.adjustInto(monetary);
32+
* monetary = anotherAdjuster.adjustInto(monetary);
33+
*
34+
* // second approach, using a fluent API
35+
* monetary = monetary.with(thisAdjuster).with(anotherAdjuster);
3136
* </pre>
37+
*
3238
* It is recommended to use the second approach, {@code with(MonetaryAdjuster)},
3339
* as it is a lot clearer to read in code.
3440
*
@@ -40,42 +46,46 @@
4046
public interface MonetaryAdjuster {
4147

4248
/**
43-
* Adjusts the specified monetary object.
44-
* <p>
45-
* This adjusts the specified monetary object using the logic
46-
* encapsulated in the implementing class.
47-
* Examples might be an adjuster that rounds the amount to the nearest 1000,
48-
* or one that performs currency conversion.
49-
* <p>
50-
* There are two equivalent ways of using a {@code MonetaryAdjuster}.
51-
* The first is to invoke the method on this interface.
52-
* The second is to use {@link MonetaryAmount#with(MonetaryAdjuster)}:
53-
* <pre>
54-
* // these two lines are equivalent, but the second approach is recommended
55-
* monetary = thisAdjuster.adjustInto(monetary);
56-
* monetary = monetary.with(thisAdjuster);
57-
* </pre>
58-
* It is recommended to use the second approach, {@code with(MonetaryAdjuster)},
59-
* as it is a lot clearer to read in code.
60-
*
61-
* <h4>Implementation specification</h4>
62-
* The implementation must take the input object and adjust it.
63-
* The implementation defines the logic of the adjustment and is responsible for
64-
* documenting that logic.
65-
* It may use any method on {@code MonetaryAmount} to determine the result.
66-
* <p>
67-
* The input object must not be altered.
68-
* Instead, an adjusted copy of the original must be returned.
69-
* This provides equivalent, safe behavior for immutable and mutable monetary amounts.
70-
* <p>
71-
* This method may be called from multiple threads in parallel.
72-
* It must be thread-safe when invoked.
73-
*
74-
* @param amount the amount to adjust, not null
75-
* @return a monetary amount with the adjustment made, not null
76-
* @throws MonetaryException if unable to make the adjustment
77-
* @throws ArithmeticException if numeric overflow occurs
78-
*/
49+
* Adjusts the specified monetary object.
50+
* <p>
51+
* This adjusts the specified monetary object using the logic encapsulated
52+
* in the implementing class. Examples might be an adjuster that rounds the
53+
* amount to the nearest 1000, or one that performs currency conversion.
54+
* <p>
55+
* There are two equivalent ways of using a {@code MonetaryAdjuster}. The
56+
* first is to invoke the method on this interface. The second is to use
57+
* {@link MonetaryAmount#with(MonetaryAdjuster)}:
58+
*
59+
* <pre>
60+
* // these two lines are equivalent, but the second approach is recommended
61+
* monetary = thisAdjuster.adjustInto(monetary);
62+
* monetary = monetary.with(thisAdjuster);
63+
* </pre>
64+
*
65+
* It is recommended to use the second approach,
66+
* {@code with(MonetaryAdjuster)}, as it is a lot clearer to read in code.
67+
*
68+
* <h4>Implementation specification</h4>
69+
* The implementation must take the input object and adjust it. The
70+
* implementation defines the logic of the adjustment and is responsible for
71+
* documenting that logic. It may use any method on {@code MonetaryAmount}
72+
* to determine the result.
73+
* <p>
74+
* The input object must not be altered. Instead, an adjusted copy of the
75+
* original must be returned. This provides equivalent, safe behavior for
76+
* immutable and mutable monetary amounts.
77+
* <p>
78+
* This method may be called from multiple threads in parallel. It must be
79+
* thread-safe when invoked.
80+
*
81+
* @param amount
82+
* the amount to adjust, not null
83+
* @return a monetary amount with the adjustment made, not null
84+
* @throws MonetaryException
85+
* if unable to make the adjustment
86+
* @throws ArithmeticException
87+
* if numeric overflow occurs
88+
*/
7989
public MonetaryAmount adjustInto(MonetaryAmount amount);
8090

8191
}

src/main/java/javax/money/MonetaryAmount.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,30 @@
2626
* This JSR additionally defines a couple of interoperability rules that each
2727
* implementation must follow:
2828
* <ul>
29+
* <li>The numeric values on the MonetaryAmount interface are for
30+
* interoperability only. They should not used for calculations. Instead of each
31+
* implementation of this interface must provide a static method
32+
* {@code T from(MonetaryAmount)} to create an instance of a concrete
33+
* implementation type {@code T} based on a given amount instance.</li>
34+
* <li>If the numeric representation of a {@code MonetaryAmount} exceeds the
35+
* numeric capabilities of the concrete type {@code T from(MonetaryAmount)} must
36+
* throw an {@code ArithemticOperationException}.</li>
37+
* <li>On the other hand, when the numeric value can not be mapped into the
38+
* numeric exchange format defined by this interface, by default also an
39+
* {@code ArithmeticException} must be thrown. Never should truncation be
40+
* performed implicitly.</li>
41+
* <li>Nevertheless truncation is possible, but must be enabled explicitly by
42+
* passing an additional {@code boolean} parameter as {@code true} to allow it.</li>
2943
* <li>Rounding is never done automatically, exception internal rounding implied
3044
* by the numeric implementation type.</li>
31-
* <li>Each implementation must at least provide two constructors, one taking
32-
* {@code (BigDecimal, CurrencyUnit)}, one taking
33-
* {@code (BigDecimal, CurrencyUnit)}.</li>
34-
* <li>External rounding must be applied, when accessing the numeric part of an
35-
* amount, if implied by the required target type. E.g. externalizing to a
36-
* double may truncate precision. If the target type supports the current
37-
* scale/precision, it is required that no external rounding is performed.</li>
38-
* <li>It is required that each implementation supports externalization to
39-
* {@link BigDecimal}.</li>
4045
* <li>Since implementations are required to be immutable, an operation must
4146
* never change any internal state of an instance. Given an instance, all
4247
* operations are required to be fully reproducible.</li>
4348
* <li>Finally the result of calling {@link #with(MonetaryAdjuster)} must be of
4449
* the same type as type on which {@code with} was called. The {@code with}
4550
* method also defines additional interoperability requirements.</li>
4651
* </ul>
47-
* Nevertheless basically an amount provides a functionality similar to
48-
* {@link BigDecimal}. Also it is required that implementations of this
49-
* interface are
52+
* It is required that implementations of this interface are
5053
* <ul>
5154
* <li>immutable</li>
5255
* <li>thread-safe</li>
@@ -130,8 +133,8 @@ public interface MonetaryAmount {
130133
public <R> R query(MonetaryQuery<R> query);
131134

132135
/**
133-
* Returns an adjusted object of the same type as this object with the
134-
* adjustment made.
136+
* Returns an adjusted object <b>of the same type</b> as this object with
137+
* the adjustment made.
135138
* <p>
136139
* This adjusts this monetary amount according to the rules of the specified
137140
* adjuster. A typical adjuster will change the amount and leave the
@@ -145,11 +148,25 @@ public interface MonetaryAmount {
145148
* date = date.with(amountRoundedToNearestWholeUnit());
146149
* </pre>
147150
*
151+
* Hereby also the method signatur on the implementation type must return
152+
* the concrete type, to enable a fluent API, e.g.
153+
*
154+
* <pre>
155+
* public final class MM implements MonetaryAmount{
156+
* ...
157+
* public MM with(MonetaryAdjuster adjuster){
158+
* ...
159+
* }
160+
*
161+
* ...
162+
* }
163+
* </pre>
164+
*
148165
* @param adjuster
149166
* the adjuster to use, not null
150167
* @return an object of the same type with the specified adjustment made,
151168
* not null
152169
*/
153170
public MonetaryAmount with(MonetaryAdjuster adjuster);
154-
171+
155172
}

0 commit comments

Comments
 (0)