Skip to content

Commit fdb95b5

Browse files
committed
Added additional tests.
1 parent 7170226 commit fdb95b5

28 files changed

+1952
-423
lines changed

src/main/java/javax/money/MonetaryCurrencies.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
package javax.money;
1414

1515
import java.util.Locale;
16-
import java.util.Map;
17-
import java.util.concurrent.ConcurrentHashMap;
1816
import java.util.logging.Level;
1917
import java.util.logging.Logger;
2018

@@ -32,12 +30,6 @@
3230
*/
3331
public final class MonetaryCurrencies {
3432

35-
/**
36-
* Internal shared cache of {@link CustomCurrency} instances, registered
37-
* using {@link AbstractBuilder} instances.
38-
*/
39-
private static final Map<String, CurrencyUnit> REGISTERED = new ConcurrentHashMap<String, CurrencyUnit>();
40-
4133
/**
4234
* Required for deserialization only.
4335
*/
@@ -81,9 +73,6 @@ public static CurrencyUnit getCurrency(String currencyCode) {
8173
+ spi.getClass().getName(), e);
8274
}
8375
}
84-
if (cu == null) {
85-
cu = REGISTERED.get(currencyCode);
86-
}
8776
if (cu == null) {
8877
throw new UnknownCurrencyException(currencyCode);
8978
}
@@ -104,9 +93,7 @@ public static CurrencyUnit getCurrency(String currencyCode) {
10493
*/
10594
public static CurrencyUnit getCurrency(Locale locale) {
10695
CurrencyUnit cu = null;
107-
for (CurrencyProviderSpi spi : Bootstrap
108-
.getServices(
109-
CurrencyProviderSpi.class)) {
96+
for (CurrencyProviderSpi spi : Bootstrap.getServices(CurrencyProviderSpi.class)) {
11097
try {
11198
cu = spi.getCurrencyUnit(locale);
11299
if (cu != null) {

src/main/java/javax/money/MonetaryRoundings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public static MonetaryOperator getRounding(String customRoundingId) {
260260
* @return the set of custom rounding ids, never {@code null}.
261261
*/
262262
public static Set<String> getCustomRoundingIds() {
263-
Set<String> result = new HashSet<String>();
263+
Set<String> result = new HashSet<>();
264264
for (RoundingProviderSpi prov : Bootstrap
265265
.getServices(
266266
RoundingProviderSpi.class)) {
@@ -278,7 +278,7 @@ public static Set<String> getCustomRoundingIds() {
278278

279279
/**
280280
* Default Rounding that rounds a {@link MonetaryAmount} based on the
281-
* amount's {@link CurrencyUnit} {@link Currency}.
281+
* amount's {@link CurrencyUnit}.
282282
*
283283
* @author Anatole Tresch
284284
*/

src/main/java/javax/money/convert/MonetaryConversions.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
*/
1111
package javax.money.convert;
1212

13-
import java.util.Collection;
14-
import java.util.Collections;
15-
import java.util.List;
16-
import java.util.Objects;
17-
import java.util.ServiceLoader;
13+
import java.util.*;
1814
import java.util.logging.Logger;
1915

2016
import javax.money.CurrencyUnit;
@@ -158,9 +154,7 @@ public static CurrencyConversion getConversion(String termCurrencyCode,
158154
* Access an instance of {@link CurrencyConversion} using the given
159155
* providers as a provider chain. Use {@link #isProviderAvailable(String)}
160156
* to check, which are available.
161-
*
162-
* @param conversionContext
163-
* the {@link ConversionContext} required, not {@code null}.
157+
*
164158
* @return the exchange rate provider.
165159
* @throws IllegalArgumentException
166160
* if no such {@link ExchangeRateProvider} is available.
@@ -172,7 +166,11 @@ public static ExchangeRateProvider getExchangeRateProvider(
172166
.getExchangeRateProvider(getDefaultProviderChain().toArray(
173167
new String[0]));
174168
}
175-
return MONETARY_CONVERSION_SPI.getExchangeRateProvider(providers);
169+
ExchangeRateProvider provider = MONETARY_CONVERSION_SPI.getExchangeRateProvider(providers);
170+
if(provider==null){
171+
throw new MonetaryException("No such rate provider: " + Arrays.toString(providers));
172+
}
173+
return provider;
176174
}
177175

178176
/**
@@ -204,7 +202,11 @@ public static Collection<String> getProviderNames() {
204202
* if no such provider is registered.
205203
*/
206204
public static ProviderContext getProviderContext(String provider) {
207-
return MONETARY_CONVERSION_SPI.getProviderContext(provider);
205+
ProviderContext ctx = MONETARY_CONVERSION_SPI.getProviderContext(provider);
206+
if(ctx==null){
207+
throw new MonetaryException("No such rate provider: " + provider);
208+
}
209+
return ctx;
208210
}
209211

210212
/**

src/main/java/javax/money/convert/ProviderContext.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,6 @@ public Set<RateType> getRateTypes() {
8181
return getNamedAttribute(Set.class, ProviderAttribute.RATE_TYPES);
8282
}
8383

84-
/**
85-
* Add the given {@link RateType} to this context builder.
86-
*
87-
* @param rateTypes
88-
* the rate type, not {@code null}
89-
*/
90-
public void addRateTypes(RateType... rateTypes) {
91-
Objects.requireNonNull(rateTypes);
92-
Set<RateType> types = getNamedAttribute(Set.class,
93-
ProviderAttribute.RATE_TYPES, null);
94-
if (types == null) {
95-
types = new HashSet<>();
96-
set(types, ProviderAttribute.RATE_TYPES, Set.class);
97-
}
98-
for (RateType rateType : rateTypes) {
99-
types.add(rateType);
100-
}
101-
}
102-
10384
/**
10485
* Returns the starting date/time this rate is valid. The result can also be
10586
* {@code null}, since it is possible, that an {@link ExchangeRate} does not
@@ -178,7 +159,7 @@ public Long getValidToMillis() {
178159
public boolean isInScope(long timestamp) {
179160
Long validTo = getValidTo(Long.class);
180161
Long validFrom = getValidFrom(Long.class);
181-
if (validTo != null && validTo.longValue() < timestamp) {
162+
if (validTo != null && validTo.longValue() <= timestamp) {
182163
return false;
183164
}
184165
if (validFrom != null && validFrom.longValue() > timestamp) {
@@ -368,7 +349,7 @@ public ProviderContext create() {
368349
public static ProviderContext from(ConversionContext conversionContext) {
369350
return new Builder(conversionContext.getProvider())
370351
.setRateTypes(conversionContext.getRateType())
371-
.setAll(conversionContext).create();
352+
.setAll(conversionContext).setProviderName(conversionContext.getProvider()).create();
372353
}
373354

374355
}

src/main/java/javax/money/format/AmountStyle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public boolean equals(Object obj) {
248248
*/
249249
@Override
250250
public String toString() {
251-
return "FormatStyle [locale=" + locale + ", formatPattern="
251+
return "AmountStyle [locale=" + locale + ", formatPattern="
252252
+ pattern
253253
+ ", currencyStyle="
254254
+ currencyStyle

src/main/java/javax/money/format/MonetaryFormats.java

Lines changed: 0 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -95,194 +95,4 @@ public static final Set<Locale> getAvailableLocales() {
9595
return AmountStyle.getAvailableLocales();
9696
}
9797

98-
// /**
99-
// * Builder for creating new instances of {@link MonetaryAmountFormat}.
100-
// */
101-
// public static final class Builder {
102-
// /** The default {@link CurrencyUnit}, may be null. */
103-
// private CurrencyUnit defaultCurrency;
104-
// /** The required {@link MonetaryContext}, may be null. */
105-
// private MonetaryContext monetaryContext;
106-
// /** The {@link AmountStyle} to be used, may be null, when an {@link Locale} is set. */
107-
// private AmountStyle.Builder styleBuilder;
108-
// /** The format's name (optional). */
109-
// private String name;
110-
//
111-
// /**
112-
// * Creates a new {@link Builder}, hereby the {@link AmountStyle} is determined by the
113-
// * {@link Locale} given.
114-
// *
115-
// * @param locale
116-
// * the target {@link Locale}.
117-
// */
118-
// public Builder(Locale locale) {
119-
// Objects.requireNonNull(locale, "Locale required.");
120-
// this.styleBuilder = new AmountStyle.Builder(locale);
121-
// }
122-
//
123-
// /**
124-
// * Sets the format's name.
125-
// *
126-
// * @param name
127-
// * the name, not null.
128-
// * @return the {@link Builder}, for chaining.
129-
// */
130-
// public Builder setName(String name) {
131-
// this.name = name;
132-
// return this;
133-
// }
134-
//
135-
// /**
136-
// * Sets the default {@link CurrencyUnit} to be used, when parsing amounts where no currency
137-
// * is available on the input.
138-
// *
139-
// * @param defaultCurrency
140-
// * the default {@link CurrencyUnit}
141-
// * @return the {@link Builder}, for chaining.
142-
// */
143-
// public Builder setDefaultCurrency(CurrencyUnit defaultCurrency) {
144-
// this.defaultCurrency = defaultCurrency;
145-
// return this;
146-
// }
147-
//
148-
// /**
149-
// * Sets the {@link CurrencyStyle} to be used.
150-
// *
151-
// * @param currencyFormat
152-
// * the {@link CurrencyStyle}, not null.
153-
// * @return the {@link Builder}, for chaining.
154-
// */
155-
// public Builder setCurrencyStyle(CurrencyStyle currencyStyle) {
156-
// this.styleBuilder.setCurrencyStyle(currencyStyle);
157-
// return this;
158-
// }
159-
//
160-
// /**
161-
// * Sets the {@link CurrencyStyle} to be used.
162-
// *
163-
// * @param currencyFormat
164-
// * the {@link CurrencyStyle}, not null.
165-
// * @return the {@link Builder}, for chaining.
166-
// */
167-
// public Builder setAmountStyle(AmountStyle style) {
168-
// this.styleBuilder.setCurrencyStyle(style.getCurrencyStyle());
169-
// this.styleBuilder
170-
// .setDisplayConversion(style.getDisplayConversion());
171-
// this.styleBuilder.setGroupingSizes(style.getGroupingSizes());
172-
// this.styleBuilder.setParseConversion(style.getParseConversion());
173-
// this.styleBuilder.setPattern(style.getPattern());
174-
// this.styleBuilder.setSymbols(style.getSymbols());
175-
// return this;
176-
// }
177-
//
178-
// /**
179-
// * Sets the {@link MonetaryOperator} to be used as display conversion before formatting the
180-
// * amount.
181-
// *
182-
// * @param conversion
183-
// * the {@link MonetaryOperator}, or null.
184-
// * @return the {@link Builder}, for chaining.
185-
// */
186-
// public Builder setDisplayConversion(MonetaryOperator conversion) {
187-
// this.styleBuilder.setDisplayConversion(conversion);
188-
// return this;
189-
// }
190-
//
191-
// /**
192-
// * Sets the {@link MonetaryOperator} to be used as parse conversion after parsing the
193-
// * amount.
194-
// *
195-
// * @param conversion
196-
// * the {@link MonetaryOperator}, or null.
197-
// * @return the {@link Builder}, for chaining.
198-
// */
199-
// public Builder setParseConversion(MonetaryOperator conversion) {
200-
// this.styleBuilder.setParseConversion(conversion);
201-
// return this;
202-
// }
203-
//
204-
// /**
205-
// * Sets the customized number group sizes to be used for formatting. Hereby each value in
206-
// * the array represents a group size, starting from the decimal point and going up the
207-
// * significant digits. The last entry in the array is used as a default group size for all
208-
// * subsequent groupings.
209-
// *
210-
// * @param groupSizes
211-
// * the group sizes, not null.
212-
// * @return the {@link Builder}, for chaining.
213-
// */
214-
// public Builder setNumberGroupSizes(int... groupSizes) {
215-
// this.styleBuilder.setGroupingSizes(groupSizes);
216-
// return this;
217-
// }
218-
//
219-
// /**
220-
// * Sets the formats pattern, similar as in {@link java.text.DecimalFormat}.
221-
// *
222-
// * @param pattern
223-
// * the pattern, not null.
224-
// * @return the {@link Builder}, for chaining.
225-
// */
226-
// public Builder setPattern(String pattern) {
227-
// this.styleBuilder.setPattern(pattern);
228-
// return this;
229-
// }
230-
//
231-
// /**
232-
// * Sets the formats {@link AmountFormatSymbols}, similar as in
233-
// * {@link java.text.DecimalFormatSymbols}.
234-
// *
235-
// * @param symbols
236-
// * the symbols, not null.
237-
// * @return the {@link Builder}, for chaining.
238-
// */
239-
// public Builder setSymbols(AmountFormatSymbols symbols) {
240-
// this.styleBuilder.setSymbols(symbols);
241-
// return this;
242-
// }
243-
//
244-
// /**
245-
// * Sets the {@link MonetaryContext} that determines the amount implementation class returned
246-
// * from parsing.
247-
// *
248-
// * @param monetaryContext
249-
// * the {@link MonetaryContext} to be used, or {@code null} for using the default
250-
// * amount type.
251-
// * @return the {@link Builder}, for chaining.
252-
// * @see javax.money.MonetaryAmounts#queryAmountType(MonetaryContext)
253-
// * @see javax.money.MonetaryAmounts#getDefaultAmountType()
254-
// * @see javax.money.MonetaryAmounts#getDefaultAmountFactory()
255-
// */
256-
// public Builder setMonetaryContext(MonetaryContext monetaryContext) {
257-
// this.monetaryContext = monetaryContext;
258-
// return this;
259-
// }
260-
//
261-
// /**
262-
// * Access a new {@link MonetaryAmountFormat}, matching the properties set.
263-
// *
264-
// * @return a new {@link MonetaryAmountFormat} instance, never {@code null}.
265-
// * @throws MonetaryException
266-
// * if no registered {@link MonetaryAmountFormatProviderSpi} can provide a
267-
// * corresponding {@link MonetaryAmountFormat} instance.
268-
// */
269-
// public MonetaryAmountFormat create() {
270-
// AmountStyle style = styleBuilder.build();
271-
// for (MonetaryAmountFormatProviderSpi spi : Bootstrap
272-
// .getServices(
273-
// MonetaryAmountFormatProviderSpi.class)) {
274-
// MonetaryAmountFormat f = spi.getFormat(style);
275-
// if (f != null) {
276-
// f.setMonetaryContext(monetaryContext);
277-
// f.setDefaultCurrency(defaultCurrency);
278-
// return f;
279-
// }
280-
// }
281-
// throw new MonetaryException(
282-
// "No MonetaryAmountFormat found for amountStyle=" + style
283-
// + ", defaultCurrency=" + defaultCurrency
284-
// + ", monetaryContext=" + monetaryContext);
285-
// }
286-
// }
287-
28898
}

src/main/java/javax/money/format/MonetaryParseException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class MonetaryParseException extends MonetaryException {
5050
public MonetaryParseException(String message, CharSequence parsedData,
5151
int errorIndex) {
5252
super(message);
53+
if(errorIndex > parsedData.length()){
54+
throw new IllegalArgumentException("Invalid error index > input.length");
55+
}
5356
this.data = parsedData;
5457
this.errorIndex = errorIndex;
5558
}
@@ -66,6 +69,9 @@ public MonetaryParseException(String message, CharSequence parsedData,
6669
public MonetaryParseException(CharSequence parsedData,
6770
int errorIndex) {
6871
super("Parse Error");
72+
if(errorIndex > parsedData.length()){
73+
throw new IllegalArgumentException("Invalid error index > input.length");
74+
}
6975
this.data = parsedData;
7076
this.errorIndex = errorIndex;
7177
}

0 commit comments

Comments
 (0)