Skip to content
juxeii edited this page Oct 21, 2016 · 5 revisions

This page describes the classes for working with Currencies.

CurrencyFactory

Class: CurrencyFactory / Package: com.jforex.programming.currency

This class provides factory methods for creating ICurrency instances from various sources. Since its public API is static, you must use it in a static way.

Predefined currencies

For all major currencies there already exists a public instance:

ICurrency eur = CurrencyFactory.EUR;
ICurrency usd = CurrencyFactory.USD;
ICurrency jpy = CurrencyFactory.JPY;
//...etc

Creating currencies from strings

If you want to convert currency string into ICurrenc instances you have several overloads to use:

Optional<ICurrency> maybeEUR = CurrencyFactory.maybeFromName("EUR");
Set<ICurrency> currencies = CurrencyFactory.fromNames("EUR", "USD", "ups");

Set<String> currencyNames=new HashSet<>();
currencyNames.add("EUR");
currencyNames.add("USD");
currencyNames.add("ups");
Set<ICurrency> currenciesFromSet = CurrencyFactory.fromNames(currencyNames);

The first example creates a currency from a given string. It returns an Optional<ICurrency> since the string could be invalid.

The second example creates a set of currencies from a list of strings. If one of these strings is invalid(like "ups") they are ignored. If all strings are invalid the method returns an empty set.

The third example is almost the same as the previous, but here you can pass a collection of strings.

Extracting currencies from instruments

Sometimes you want to collect all the currencies of one or more instruments. Here's how:

Set<ICurrency> fromInstrument = CurrencyFactory.fromInstrument(Instrument.EURUSD);
Set<ICurrency> fromInstruments =
		CurrencyFactory.fromInstruments(Instrument.EURUSD, Instrument.GBPUSD);

Set<Instrument> instruments = new HashSet<>();
instruments.add(Instrument.EURUSD);
instruments.add(Instrument.GBPUSD);
Set<ICurrency> currenciesFromSet = CurrencyFactory.fromInstruments(instruments);

CurrencyUtil

Class: CurrencyUtil / Package: com.jforex.programming.currency

This little class provides some methods for checking if a currency string is valid and if a currency is contained in instruments. Since its public API is static, you must use it in a static way. These methods are very simple speak for themselves:

boolean invalidName = CurrencyUtil.isNameValid("ups");
boolean validName = CurrencyUtil.isNameValid("EUR");

boolean nameContainsInInstrument =
		CurrencyUtil.isInInstrument("EUR", Instrument.EURUSD);
boolean containsInInstrument =
		CurrencyUtil.isInInstrument(CurrencyFactory.EUR, Instrument.EURUSD);