Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding a currency extension file for addition non-default currencies #13

Merged
merged 2 commits into from Jan 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -35,6 +35,11 @@
<timezone>0</timezone> <timezone>0</timezone>
</developer> </developer>
</developers> </developers>
<contributors>
<contributor>
<name>Tim Molter</name>
</contributor>
</contributors>
<licenses> <licenses>
<license> <license>
<name>Apache 2</name> <name>Apache 2</name>
Expand Down
65 changes: 39 additions & 26 deletions src/main/java/org/joda/money/DefaultCurrencyUnitDataProvider.java
Expand Up @@ -34,38 +34,51 @@ class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider {
/** Regex format for the csv line. */ /** 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]*)#?.*"); 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. * 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 * @throws Exception if an error occurs
*/ */
@Override @Override
protected void registerCurrencies() throws Exception { protected void registerCurrencies() throws Exception {
InputStream in = getClass().getResourceAsStream("/org/joda/money/MoneyData.csv"); loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
if (in == null) { loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
throw new FileNotFoundException("Data file /org/joda/money/MoneyData.csv not found"); }
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); /**
String line; * Loads Currencies from a file
while ((line = reader.readLine()) != null) { *
Matcher matcher = REGEX_LINE.matcher(line); * @param fileName the file to load
if (matcher.matches()) { * @param isNecessary whether or not the file is necessary
List<String> countryCodes = new ArrayList<String>(); * @throws Exception if a necessary file isn't found
String codeStr = matcher.group(4); */
String currencyCode = matcher.group(1); private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
if (codeStr.length() % 2 == 1) { InputStream in = getClass().getResourceAsStream(fileName);
continue; // invalid line if (in == null && isNecessary) {
} throw new FileNotFoundException("Data file " + fileName + " not found");
for (int i = 0; i < codeStr.length(); i += 2) { }else if (in == null && !isNecessary) {
countryCodes.add(codeStr.substring(i, i + 2)); return; // no extension file found, no problem. just return
} }
int numericCode = Integer.parseInt(matcher.group(2)); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
int digits = Integer.parseInt(matcher.group(3)); String line;
registerCurrency(currencyCode, numericCode, digits, countryCodes); 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 changes: 2 additions & 0 deletions src/test/java/org/joda/money/MoneyDataExtension.csv
@@ -0,0 +1,2 @@
#Code,Numeric,DecPlaces,CountryCodes
BTC,-1,-1,
77 changes: 77 additions & 0 deletions src/test/java/org/joda/money/TestCurrencyUnitExtension.java
@@ -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);
}

}