com.opengamma.util.money.StandardCurrencyPairs.java Source code

Java tutorial

Introduction

Here is the source code for com.opengamma.util.money.StandardCurrencyPairs.java

Source

/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.util.money;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.bytecode.opencsv.CSVReader;

import com.opengamma.util.tuple.Pair;

/**
 * Look up whether you a pair of currencies is in the standard order
 */
public class StandardCurrencyPairs {

    private static final Logger s_logger = LoggerFactory.getLogger(StandardCurrencyPairs.class);
    private static Set<Pair<Currency, Currency>> s_currencyPairs = new HashSet<Pair<Currency, Currency>>();

    static {
        InputStream is = StandardCurrencyPairs.class.getClassLoader()
                .getResourceAsStream("com/opengamma/util/money/standard-currency-pairs.csv");
        try {
            parseCSV("standard-currency-pairs.csv", is);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    private static void parseCSV(String filename, InputStream is) {
        CSVReader reader = new CSVReader(new InputStreamReader(new BufferedInputStream(is)));
        List<String[]> rows;
        try {
            rows = reader.readAll();
            int line = 1;
            for (String[] row : rows) {
                String numerator = row[0].trim();
                String denominator = row[1].trim();
                try {
                    s_currencyPairs.add(Pair.of(Currency.of(numerator), Currency.of(denominator)));
                } catch (IllegalArgumentException iae) {
                    s_logger.warn("Couldn't create currency from " + filename + ":" + line);
                }
                line++;
            }
        } catch (IOException ex) {
            s_logger.warn("Couldn't read " + filename);
        }
    }

    public static boolean isStandardPair(Currency numerator, Currency denominator) {
        return s_currencyPairs.contains(Pair.of(numerator, denominator));
    }

    public static boolean isSingleCurrencyNumerator(Currency ccy) {
        return s_currencyPairs.contains(Pair.of(ccy, Currency.USD));
    }
}