Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13 from timmolter/master

Allow adding a currency extension file for addition non-default currencies
  • Loading branch information...
commit 6e3d1546e23959fa83265cbb9a9c03264d1f488a 2 parents 031d66f + fd3e956
Stephen Colebourne jodastephen authored
5 pom.xml
View
@@ -35,6 +35,11 @@
<timezone>0</timezone>
</developer>
</developers>
+ <contributors>
+ <contributor>
+ <name>Tim Molter</name>
+ </contributor>
+ </contributors>
<licenses>
<license>
<name>Apache 2</name>
65 src/main/java/org/joda/money/DefaultCurrencyUnitDataProvider.java
View
@@ -34,38 +34,51 @@
/** Regex format for the csv line. */
private static final Pattern REGEX_LINE = Pattern.compile("([A-Z]{3}),(-1|[0-9]{1,3}),(-1|0|1|2|3),([A-Z]*)#?.*");
+
/**
* Registers all the currencies known by this provider.
- * <p>
- * This reads the first resource named '/org/joda/money/MoneyData.csv' on the classpath.
*
* @throws Exception if an error occurs
- */
+ */
@Override
protected void registerCurrencies() throws Exception {
- InputStream in = getClass().getResourceAsStream("/org/joda/money/MoneyData.csv");
- if (in == null) {
- throw new FileNotFoundException("Data file /org/joda/money/MoneyData.csv not found");
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
- String line;
- while ((line = reader.readLine()) != null) {
- Matcher matcher = REGEX_LINE.matcher(line);
- if (matcher.matches()) {
- List<String> countryCodes = new ArrayList<String>();
- String codeStr = matcher.group(4);
- String currencyCode = matcher.group(1);
- if (codeStr.length() % 2 == 1) {
- continue; // invalid line
- }
- for (int i = 0; i < codeStr.length(); i += 2) {
- countryCodes.add(codeStr.substring(i, i + 2));
- }
- int numericCode = Integer.parseInt(matcher.group(2));
- int digits = Integer.parseInt(matcher.group(3));
- registerCurrency(currencyCode, numericCode, digits, countryCodes);
- }
- }
+ loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
+ loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
+ }
+
+ /**
+ * Loads Currencies from a file
+ *
+ * @param fileName the file to load
+ * @param isNecessary whether or not the file is necessary
+ * @throws Exception if a necessary file isn't found
+ */
+ private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
+ InputStream in = getClass().getResourceAsStream(fileName);
+ if (in == null && isNecessary) {
+ throw new FileNotFoundException("Data file " + fileName + " not found");
+ }else if (in == null && !isNecessary) {
+ return; // no extension file found, no problem. just return
+ }
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ Matcher matcher = REGEX_LINE.matcher(line);
+ if (matcher.matches()) {
+ List<String> countryCodes = new ArrayList<String>();
+ String codeStr = matcher.group(4);
+ String currencyCode = matcher.group(1);
+ if (codeStr.length() % 2 == 1) {
+ continue; // invalid line
+ }
+ for (int i = 0; i < codeStr.length(); i += 2) {
+ countryCodes.add(codeStr.substring(i, i + 2));
+ }
+ int numericCode = Integer.parseInt(matcher.group(2));
+ int digits = Integer.parseInt(matcher.group(3));
+ registerCurrency(currencyCode, numericCode, digits, countryCodes);
+ }
+ }
}
}
2  src/test/java/org/joda/money/MoneyDataExtension.csv
View
@@ -0,0 +1,2 @@
+#Code,Numeric,DecPlaces,CountryCodes
+BTC,-1,-1,
77 src/test/java/org/joda/money/TestCurrencyUnitExtension.java
View
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2009-2013 Stephen Colebourne
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.joda.money;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InvalidObjectException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Currency;
+import java.util.List;
+import java.util.Locale;
+
+import org.testng.annotations.Test;
+
+/**
+ * Test CurrencyUnit.
+ */
+@Test
+public class TestCurrencyUnitExtension {
+
+
+ public void test_CurrencyFromMoneyData() {
+ List<CurrencyUnit> curList = CurrencyUnit.registeredCurrencies();
+ boolean found = false;
+ for (CurrencyUnit currencyUnit : curList) {
+ if (currencyUnit.getCode().equals("GBP")) {
+ found = true;
+ break;
+ }
+ }
+ assertEquals(found, true);
+ }
+
+ public void test_CurrencyFromMoneyDataExtension() {
+ List<CurrencyUnit> curList = CurrencyUnit.registeredCurrencies();
+ boolean found = false;
+ for (CurrencyUnit currencyUnit : curList) {
+ if (currencyUnit.getCode().equals("BTC")) {
+ found = true;
+ break;
+ }
+ }
+ assertEquals(found, true);
+ }
+
+ public void test_CurrencyMissing() {
+ List<CurrencyUnit> curList = CurrencyUnit.registeredCurrencies();
+ boolean found = false;
+ for (CurrencyUnit currencyUnit : curList) {
+ if (currencyUnit.getCode().equals("NMC")) {
+ found = true;
+ break;
+ }
+ }
+ assertEquals(found, false);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.