Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 6bf0f49

Browse files
authored
Merge pull request #56 from acelopezco/bp-marschall-search-by-currency-code
Backport of #310.
2 parents 9c147ef + 8209b19 commit 6bf0f49

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

src/main/java/org/javamoney/moneta/internal/ConfigurableCurrencyUnitProvider.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class ConfigurableCurrencyUnitProvider extends BaseCurrencyProviderSpi {
3131
* The currency units, identified by currency code.
3232
*/
3333
private static final Map<String, CurrencyUnit> currencyUnits = new ConcurrentHashMap<>();
34+
/**
35+
* The currency units, identified by numeric code.
36+
*/
37+
private static final Map<Integer, CurrencyUnit> currencyUnitsByNumericCode = new ConcurrentHashMap<>();
3438
/**
3539
* The currency units identified by Locale.
3640
*/
@@ -65,23 +69,37 @@ public Set<CurrencyUnit> getCurrencies(CurrencyQuery currencyQuery) {
6569
}
6670
return result;
6771
}
72+
if (!currencyQuery.getNumericCodes().isEmpty()) {
73+
for (Integer numericCode : currencyQuery.getNumericCodes()) {
74+
CurrencyUnit cu = currencyUnitsByNumericCode.get(numericCode);
75+
if (cu != null) {
76+
result.add(cu);
77+
}
78+
}
79+
return result;
80+
}
6881
result.addAll(currencyUnits.values());
6982
return result;
7083
}
7184

7285
/**
73-
* Registers a bew currency unit under its currency code.
86+
* Registers a new currency unit under its currency code and potentially numeric code.
7487
*
7588
* @param currencyUnit the new currency to be registered, not null.
7689
* @return any unit instance registered previously by this instance, or null.
7790
*/
7891
public static CurrencyUnit registerCurrencyUnit(CurrencyUnit currencyUnit) {
7992
Objects.requireNonNull(currencyUnit);
80-
return ConfigurableCurrencyUnitProvider.currencyUnits.put(currencyUnit.getCurrencyCode(), currencyUnit);
93+
CurrencyUnit registered = ConfigurableCurrencyUnitProvider.currencyUnits.put(currencyUnit.getCurrencyCode(), currencyUnit);
94+
int numericCode = currencyUnit.getNumericCode();
95+
if (numericCode != -1) {
96+
ConfigurableCurrencyUnitProvider.currencyUnitsByNumericCode.put(numericCode, currencyUnit);
97+
}
98+
return registered;
8199
}
82100

83101
/**
84-
* Registers a bew currency unit under the given Locale.
102+
* Registers a new currency unit under the given Locale.
85103
*
86104
* @param currencyUnit the new currency to be registered, not null.
87105
* @param locale the Locale, not null.
@@ -101,7 +119,14 @@ public static CurrencyUnit registerCurrencyUnit(CurrencyUnit currencyUnit, Local
101119
*/
102120
public static CurrencyUnit removeCurrencyUnit(String currencyCode) {
103121
Objects.requireNonNull(currencyCode);
104-
return ConfigurableCurrencyUnitProvider.currencyUnits.remove(currencyCode);
122+
CurrencyUnit removed = ConfigurableCurrencyUnitProvider.currencyUnits.remove(currencyCode);
123+
if (removed != null) {
124+
int numericCode = removed.getNumericCode();
125+
if (numericCode != -1) {
126+
ConfigurableCurrencyUnitProvider.currencyUnitsByNumericCode.remove(numericCode);
127+
}
128+
}
129+
return removed;
105130
}
106131

107132
/**
@@ -122,8 +147,9 @@ public static CurrencyUnit removeCurrencyUnit(Locale locale) {
122147
*/
123148
@Override
124149
public String toString() {
125-
return "ConfigurableCurrencyUnitProvider [currencyUnits=" + currencyUnits + ", currencyUnitsByLocale=" +
126-
currencyUnitsByLocale + ']';
150+
return "ConfigurableCurrencyUnitProvider [currencyUnits=" + currencyUnits
151+
+ ", currencyUnitsByNumericCode=" + currencyUnitsByNumericCode
152+
+ ", currencyUnitsByLocale=" + currencyUnitsByLocale + ']';
127153
}
128154

129155
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.javamoney.moneta.internal;
17+
18+
import static org.testng.Assert.assertEquals;
19+
20+
import javax.money.CurrencyQuery;
21+
import javax.money.CurrencyQueryBuilder;
22+
import javax.money.CurrencyUnit;
23+
import javax.money.Monetary;
24+
25+
import org.javamoney.moneta.CurrencyUnitBuilder;
26+
import org.testng.annotations.Test;
27+
28+
/**
29+
* Tests for ConfigurableCurrencyUnitProvider.
30+
* @author Philippe Marschall
31+
*/
32+
public class ConfigurableCurrencyUnitProviderTest {
33+
34+
/**
35+
* Tests that searching by numeric code is supported by {@link ConfigurableCurrencyUnitProvider}.
36+
*/
37+
@Test
38+
public void testSearchByNumericCurrencyCode() {
39+
CurrencyUnit usd = CurrencyUnitBuilder.of("USD", "search-test")
40+
.setNumericCode(840)
41+
.setDefaultFractionDigits(2)
42+
.build(false);
43+
CurrencyUnit eur = CurrencyUnitBuilder.of("EUR", "search-test")
44+
.setNumericCode(978)
45+
.setDefaultFractionDigits(2)
46+
.build(false);
47+
48+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(usd);
49+
ConfigurableCurrencyUnitProvider.registerCurrencyUnit(eur);
50+
try {
51+
CurrencyQuery query = CurrencyQueryBuilder.of()
52+
.setProviderName(ConfigurableCurrencyUnitProvider.class.getSimpleName())
53+
.setNumericCodes(840)
54+
.build();
55+
CurrencyUnit currency = Monetary.getCurrency(query);
56+
assertEquals(usd, currency);
57+
} finally {
58+
ConfigurableCurrencyUnitProvider.removeCurrencyUnit(usd.getCurrencyCode());
59+
ConfigurableCurrencyUnitProvider.removeCurrencyUnit(eur.getCurrencyCode());
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)