Coverage Report - org.joda.money.DefaultCurrencyUnitDataProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultCurrencyUnitDataProvider
88%
23/26
56%
9/16
6
 
 1  
 /*
 2  
  *  Copyright 2009-2013 Stephen Colebourne
 3  
  *
 4  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 5  
  *  you may not use this file except in compliance with the License.
 6  
  *  You may obtain a copy of 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,
 12  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *  See the License for the specific language governing permissions and
 14  
  *  limitations under the License.
 15  
  */
 16  
 package org.joda.money;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 import java.io.FileNotFoundException;
 20  
 import java.io.InputStream;
 21  
 import java.io.InputStreamReader;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.regex.Matcher;
 25  
 import java.util.regex.Pattern;
 26  
 
 27  
 /**
 28  
  * Provider for available currencies using a file.
 29  
  * <p>
 30  
  * This reads currencies from two files.
 31  
  * Firstly it reads the mandatory resource named {@code /org/joda/money/MoneyData.csv}.
 32  
  * Then it reads the optional resource named {@code /org/joda/money/MoneyDataExtension.csv}.
 33  
  * Both will be read as the first found on the classpath.
 34  
  * The second file may replace entries in the first file.
 35  
  */
 36  1
 class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider {
 37  
 
 38  
     /** Regex format for the csv line. */
 39  1
     private static final Pattern REGEX_LINE = Pattern.compile("([A-Z]{3}),(-1|[0-9]{1,3}),(-1|[0-9]),([A-Z]*)#?.*");
 40  
 
 41  
     /**
 42  
      * Registers all the currencies known by this provider.
 43  
      * 
 44  
      * @throws Exception if an error occurs
 45  
      */ 
 46  
     @Override
 47  
     protected void registerCurrencies() throws Exception {
 48  1
         loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
 49  1
         loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
 50  1
     }
 51  
     
 52  
     /**
 53  
      * Loads Currencies from a file
 54  
      *  
 55  
      * @param fileName  the file to load, not null
 56  
      * @param isNecessary  whether or not the file is necessary
 57  
      * @throws Exception if a necessary file is not found
 58  
      */
 59  
     private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
 60  2
         InputStream in = getClass().getResourceAsStream(fileName);
 61  2
         if (in == null && isNecessary) {
 62  0
             throw new FileNotFoundException("Data file " + fileName + " not found");
 63  2
         } else if (in == null && !isNecessary) {
 64  0
             return; // no extension file found, no problem. just return
 65  
         }
 66  2
         BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
 67  
         String line;
 68  185
         while ((line = reader.readLine()) != null) {
 69  183
             Matcher matcher = REGEX_LINE.matcher(line);
 70  183
             if (matcher.matches()) {
 71  172
                 List<String> countryCodes = new ArrayList<String>();
 72  172
                 String codeStr = matcher.group(4);
 73  172
                 String currencyCode = matcher.group(1);
 74  172
                 if (codeStr.length() % 2 == 1) {
 75  0
                     continue;  // invalid line
 76  
                 }
 77  428
                 for (int i = 0; i < codeStr.length(); i += 2) {
 78  256
                     countryCodes.add(codeStr.substring(i, i + 2));
 79  
                 }
 80  172
                 int numericCode = Integer.parseInt(matcher.group(2));
 81  172
                 int digits = Integer.parseInt(matcher.group(3));
 82  172
                 registerCurrency(currencyCode, numericCode, digits, countryCodes);
 83  
             }
 84  183
         }
 85  2
     }
 86  
 
 87  
 }