Skip to content

Commit 510a831

Browse files
committed
https://java.net/jira/browse/JAVAMONEY-77
Replaced create() with of() in builders. Removed exposure of BuildableCurrencyUnit and DefaultExchangeRate in RI by refactoring builder pattern similar to RI.
1 parent c477587 commit 510a831

39 files changed

+2792
-2985
lines changed

pom.xml

Lines changed: 567 additions & 563 deletions
Large diffs are not rendered by default.

src/main/java/org/javamoney/moneta/BuildableCurrencyUnit.java

Lines changed: 3 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@
1515
*/
1616
package org.javamoney.moneta;
1717

18-
import org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider;
19-
2018
import javax.money.*;
2119
import javax.money.CurrencyContext;
2220

23-
import java.util.Locale;
2421
import java.util.Objects;
2522

2623
/**
27-
* Implementation of {@link javax.money.CurrencyUnit} that allows to create new instances using a fluent API.
24+
* Implementation of {@link javax.money.CurrencyUnit} that allows to of new instances using a fluent API.
2825
* Instances created also can be added to the {@link org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider}
2926
* singleton, which publishes the instances, so they are visible from the {@link javax.money.MonetaryCurrencies}
3027
* singleton.
3128
*/
32-
public final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit>{
29+
final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit>{
3330

3431
/**
3532
* The unique currency code.
@@ -53,7 +50,7 @@ public final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<Cur
5350
*
5451
* @param builder the builder, never null.
5552
*/
56-
private BuildableCurrencyUnit(Builder builder){
53+
BuildableCurrencyUnit(CurrencyUnitBuilder builder){
5754
Objects.requireNonNull(builder.currencyCode, "currencyCode required");
5855
if(builder.numericCode < -1){
5956
throw new MonetaryException("numericCode must be >= -1");
@@ -129,130 +126,4 @@ public String toString(){
129126
}
130127

131128

132-
/**
133-
* Builder for constructing new instances o{@link org.javamoney.moneta.BuildableCurrencyUnit} using a fluent
134-
* API.
135-
*/
136-
public static final class Builder{
137-
/**
138-
* The currency code.
139-
*/
140-
private String currencyCode;
141-
/**
142-
* The (optional) numeric code.
143-
*/
144-
private int numericCode = -1;
145-
/**
146-
* The default fraction digits.
147-
*/
148-
private int defaultFractionDigits = 2;
149-
/**
150-
* THe currency's context.
151-
*/
152-
private javax.money.CurrencyContext currencyContext;
153-
154-
/**
155-
* Creats a new Builder.
156-
*
157-
* @param currencyCode the (unique) and identifying currency code, not null.
158-
*/
159-
public Builder(String currencyCode, javax.money.CurrencyContext currencyContext){
160-
Objects.requireNonNull(currencyCode, "currencyCode required");
161-
this.currencyCode = currencyCode;
162-
Objects.requireNonNull(currencyContext, "currencyContext required");
163-
this.currencyContext = currencyContext;
164-
}
165-
166-
/**
167-
* Allows to set the currency code, for creating multiple instances, using one Builder.
168-
*
169-
* @param currencyCode the (unique) and identifying currency code, not null.
170-
* @return the Builder, for chaining.
171-
* @see javax.money.CurrencyUnit#getCurrencyCode()
172-
*/
173-
public Builder setCurrencyCode(String currencyCode){
174-
Objects.requireNonNull(currencyCode, "currencyCode required");
175-
this.currencyCode = currencyCode;
176-
this.currencyContext = CurrencyContextBuilder.create(getClass().getSimpleName()).build();
177-
return this;
178-
}
179-
180-
/**
181-
* Set the numeric code (optional).
182-
*
183-
* @param numericCode The numeric currency code, &gt;= -1. .1 hereby means <i>undefined</i>.
184-
* @return the Builder, for chaining.
185-
* @see javax.money.CurrencyUnit#getNumericCode()
186-
*/
187-
public Builder setNumericCode(int numericCode){
188-
if(numericCode < -1){
189-
throw new IllegalArgumentException("numericCode must be >= -1");
190-
}
191-
this.numericCode = numericCode;
192-
return this;
193-
}
194-
195-
/**
196-
* Set the default fraction digits.
197-
*
198-
* @param defaultFractionDigits the default fraction digits, &gt;= 0.
199-
* @return the Builder, for chaining.
200-
* @see javax.money.CurrencyUnit#getDefaultFractionDigits()
201-
*/
202-
public Builder setDefaultFractionDigits(int defaultFractionDigits){
203-
if(defaultFractionDigits < 0){
204-
throw new IllegalArgumentException("defaultFractionDigits must be >= 0");
205-
}
206-
this.defaultFractionDigits = defaultFractionDigits;
207-
return this;
208-
}
209-
210-
/**
211-
* Returns a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit}.
212-
*
213-
* @return the new CurrencyUnit instance.
214-
* @throws MonetaryException if creation fails
215-
*/
216-
public BuildableCurrencyUnit build(){
217-
return build(false);
218-
}
219-
220-
/**
221-
* Returns a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit} and publishes it so it is
222-
* accessible from the {@code MonetaryCurrencies} singleton.
223-
*
224-
* @param register if {@code true} the instance created is published so it is accessible from
225-
* the {@code MonetaryCurrencies} singleton.
226-
* @return the new CurrencyUnit instance.
227-
* @see javax.money.MonetaryCurrencies#getCurrency(String, String...)
228-
*/
229-
public BuildableCurrencyUnit build(boolean register){
230-
BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this);
231-
if(register){
232-
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu);
233-
}
234-
return cu;
235-
}
236-
237-
/**
238-
* Returns a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit} and publishes it so it is
239-
* accessible from the {@code MonetaryCurrencies} singleton.
240-
*
241-
* @param register if {@code true} the instance created is published so it is accessible from
242-
* the {@code MonetaryCurrencies} singleton.
243-
* @param locale country Locale for making the currency for the given country.
244-
* @return the new CurrencyUnit instance.
245-
* @see javax.money.MonetaryCurrencies#getCurrency(String, String...)
246-
* @see javax.money.MonetaryCurrencies#getCurrency(Locale, String...)
247-
*/
248-
public BuildableCurrencyUnit build(boolean register, Locale locale){
249-
BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this);
250-
if(register){
251-
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu);
252-
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu, locale);
253-
}
254-
return cu;
255-
}
256-
}
257-
258129
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package org.javamoney.moneta;
2+
3+
import org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider;
4+
5+
import javax.money.CurrencyContextBuilder;
6+
import javax.money.CurrencyUnit;
7+
import java.util.Locale;
8+
import java.util.Objects;
9+
10+
/**
11+
* Builder for constructing new instances o{@link BuildableCurrencyUnit} using a fluent
12+
* API.
13+
*/
14+
public final class CurrencyUnitBuilder{
15+
/**
16+
* The currency code.
17+
*/
18+
String currencyCode;
19+
/**
20+
* The (optional) numeric code.
21+
*/
22+
int numericCode = -1;
23+
/**
24+
* The default fraction digits.
25+
*/
26+
int defaultFractionDigits = 2;
27+
/**
28+
* THe currency's context.
29+
*/
30+
javax.money.CurrencyContext currencyContext;
31+
32+
/**
33+
* Creats a new CurrencyUnitBuilder.
34+
*
35+
* @param currencyCode the (unique) and identifying currency code, not null.
36+
* @param currencyContext The currency context to be used.
37+
*/
38+
public static CurrencyUnitBuilder of(String currencyCode, javax.money.CurrencyContext currencyContext){
39+
return new CurrencyUnitBuilder(currencyCode, currencyContext);
40+
}
41+
42+
/**
43+
* Creats a new CurrencyUnitBuilder, creates a simple CurrencyContext using the given provider name.
44+
*
45+
* @param currencyCode the (unique) and identifying currency code, not null.
46+
* @param providerName the currency provider, not null.
47+
*/
48+
public static CurrencyUnitBuilder of(String currencyCode, String providerName){
49+
return new CurrencyUnitBuilder(currencyCode, CurrencyContextBuilder.of(providerName).build());
50+
}
51+
52+
/**
53+
* Creats a new Builder.
54+
*
55+
* @param currencyCode the (unique) and identifying currency code, not null.
56+
*/
57+
private CurrencyUnitBuilder(String currencyCode, javax.money.CurrencyContext currencyContext){
58+
Objects.requireNonNull(currencyCode, "currencyCode required");
59+
this.currencyCode = currencyCode;
60+
Objects.requireNonNull(currencyContext, "currencyContext required");
61+
this.currencyContext = currencyContext;
62+
}
63+
64+
/**
65+
* Allows to set the currency code, for creating multiple instances, using one Builder.
66+
*
67+
* @param currencyCode the (unique) and identifying currency code, not null.
68+
* @return the Builder, for chaining.
69+
* @see javax.money.CurrencyUnit#getCurrencyCode()
70+
*/
71+
public CurrencyUnitBuilder setCurrencyCode(String currencyCode){
72+
Objects.requireNonNull(currencyCode, "currencyCode required");
73+
this.currencyCode = currencyCode;
74+
this.currencyContext = CurrencyContextBuilder.of(getClass().getSimpleName()).build();
75+
return this;
76+
}
77+
78+
/**
79+
* Set the numeric code (optional).
80+
*
81+
* @param numericCode The numeric currency code, &gt;= -1. .1 hereby means <i>undefined</i>.
82+
* @return the Builder, for chaining.
83+
* @see javax.money.CurrencyUnit#getNumericCode()
84+
*/
85+
public CurrencyUnitBuilder setNumericCode(int numericCode){
86+
if(numericCode < -1){
87+
throw new IllegalArgumentException("numericCode must be >= -1");
88+
}
89+
this.numericCode = numericCode;
90+
return this;
91+
}
92+
93+
/**
94+
* Set the default fraction digits.
95+
*
96+
* @param defaultFractionDigits the default fraction digits, &gt;= 0.
97+
* @return the Builder, for chaining.
98+
* @see javax.money.CurrencyUnit#getDefaultFractionDigits()
99+
*/
100+
public CurrencyUnitBuilder setDefaultFractionDigits(int defaultFractionDigits){
101+
if(defaultFractionDigits < 0){
102+
throw new IllegalArgumentException("defaultFractionDigits must be >= 0");
103+
}
104+
this.defaultFractionDigits = defaultFractionDigits;
105+
return this;
106+
}
107+
108+
/**
109+
* Returns a new instance of {@link BuildableCurrencyUnit}.
110+
*
111+
* @return the new CurrencyUnit instance.
112+
* @throws javax.money.MonetaryException if creation fails
113+
*/
114+
public CurrencyUnit build(){
115+
return build(false);
116+
}
117+
118+
/**
119+
* Returns a new instance of {@link BuildableCurrencyUnit} and publishes it so it is
120+
* accessible from the {@code MonetaryCurrencies} singleton.
121+
*
122+
* @param register if {@code true} the instance created is published so it is accessible from
123+
* the {@code MonetaryCurrencies} singleton.
124+
* @return the new CurrencyUnit instance.
125+
* @see javax.money.MonetaryCurrencies#getCurrency(String, String...)
126+
*/
127+
public CurrencyUnit build(boolean register){
128+
BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this);
129+
if(register){
130+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu);
131+
}
132+
return cu;
133+
}
134+
135+
/**
136+
* Returns a new instance of {@link BuildableCurrencyUnit} and publishes it so it is
137+
* accessible from the {@code MonetaryCurrencies} singleton.
138+
*
139+
* @param register if {@code true} the instance created is published so it is accessible from
140+
* the {@code MonetaryCurrencies} singleton.
141+
* @param locale country Locale for making the currency for the given country.
142+
* @return the new CurrencyUnit instance.
143+
* @see javax.money.MonetaryCurrencies#getCurrency(String, String...)
144+
* @see javax.money.MonetaryCurrencies#getCurrency(java.util.Locale, String...)
145+
*/
146+
public CurrencyUnit build(boolean register, Locale locale){
147+
BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this);
148+
if(register){
149+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu);
150+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu, locale);
151+
}
152+
return cu;
153+
}
154+
}

0 commit comments

Comments
 (0)