001/* 002 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE 003 * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT. 004 * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY 005 * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE 006 * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE" 007 * BUTTON AT THE BOTTOM OF THIS PAGE. Specification: JSR-354 Money and Currency 008 * API ("Specification") Copyright (c) 2012-2013, Credit Suisse All rights 009 * reserved. 010 */ 011package org.javamoney.moneta.internal; 012 013import javax.money.CurrencyUnit; 014import javax.money.spi.CurrencyProviderSpi; 015import java.util.Locale; 016import java.util.Map; 017import java.util.Objects; 018import java.util.concurrent.ConcurrentHashMap; 019 020/** 021 * This class provides a programmatic singleton for globally registering new {@link java.util.Currency} into the 022 * {@link javax.money.MonetaryCurrencies} singleton either by currency code, locale, or both. 023 */ 024public class ConfigurableCurrencyUnitProvider implements CurrencyProviderSpi{ 025 /** The currency units, identified by currency code. */ 026 private static Map<String,CurrencyUnit> currencyUnits = new ConcurrentHashMap<>(); 027 /** The currency units identified by Locale. */ 028 private static Map<Locale,CurrencyUnit> currencyUnitsByLocale = new ConcurrentHashMap<>(); 029 030 @Override 031 public CurrencyUnit getCurrencyUnit(String currencyCode){ 032 return currencyUnits.get(currencyCode); 033 } 034 035 @Override 036 public CurrencyUnit getCurrencyUnit(Locale locale){ 037 return currencyUnitsByLocale.get(locale); 038 } 039 040 /** 041 * Registers a bew currency unit under its currency code. 042 * @param currencyUnit the new currency to be registered, not null. 043 * @return any unit instance registered previously by this instance, or null. 044 */ 045 public static CurrencyUnit registerCurrencyUnit(CurrencyUnit currencyUnit){ 046 Objects.requireNonNull(currencyUnit); 047 return ConfigurableCurrencyUnitProvider.currencyUnits.put(currencyUnit.getCurrencyCode(), currencyUnit); 048 } 049 050 /** 051 * Registers a bew currency unit under the given Locale. 052 * @param currencyUnit the new currency to be registered, not null. 053 * @param locale 054 * @return any unit instance registered previously by this instance, or null. 055 */ 056 public static CurrencyUnit registerCurrencyUnit(CurrencyUnit currencyUnit, Locale locale){ 057 Objects.requireNonNull(locale); 058 Objects.requireNonNull(currencyUnit); 059 return ConfigurableCurrencyUnitProvider.currencyUnitsByLocale.put(locale, currencyUnit); 060 } 061 062 /** 063 * Removes a CurrencyUnit. 064 * @param currencyCode the currency code, not null. 065 * @return any unit instance removed, or null. 066 */ 067 public static CurrencyUnit removeCurrencyUnit(String currencyCode){ 068 Objects.requireNonNull(currencyCode); 069 return ConfigurableCurrencyUnitProvider.currencyUnits.remove(currencyCode); 070 } 071 072 /** 073 * Removes a CurrencyUnit. 074 * @param locale the Locale, not null. 075 * @return any unit instance removed, or null. 076 */ 077 public static CurrencyUnit removeCurrencyUnit(Locale locale){ 078 Objects.requireNonNull(locale); 079 return ConfigurableCurrencyUnitProvider.currencyUnitsByLocale.remove(locale); 080 } 081 082 /* 083 * (non-Javadoc) 084 * 085 * @see java.lang.Object#toString() 086 */ 087 @Override 088 public String toString(){ 089 return "ConfigurableCurrencyUnitProvider [currencyUnits=" + currencyUnits + ", currencyUnitsByLocale=" + 090 currencyUnitsByLocale + "]"; 091 } 092 093}