1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.currencyconverter.util;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.webcache.WebCacheItem;
20  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
21  import com.liferay.portlet.currencyconverter.model.Currency;
22  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.TreeMap;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.PageContext;
33  
34  /**
35   * <a href="CurrencyUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class CurrencyUtil {
40  
41      public static Map<String, String> getAllSymbols(PageContext pageContext) {
42          HttpServletRequest request =
43              (HttpServletRequest)pageContext.getRequest();
44  
45          Locale locale = request.getLocale();
46  
47          String key = locale.toString();
48  
49          Map<String, String> symbols = _symbolsPool.get(key);
50  
51          if (symbols != null) {
52              return symbols;
53          }
54  
55          symbols = new TreeMap<String, String>();
56  
57          Iterator<String> itr = _instance._currencyIds.iterator();
58  
59          while (itr.hasNext()) {
60              String symbol = itr.next();
61  
62              symbols.put(LanguageUtil.get(pageContext, symbol), symbol);
63          }
64  
65          _symbolsPool.put(key, symbols);
66  
67          return symbols;
68      }
69  
70      public static Currency getCurrency(String symbol) {
71          WebCacheItem wci = new CurrencyWebCacheItem(symbol);
72  
73          return (Currency)WebCachePoolUtil.get(
74              CurrencyUtil.class.getName() + StringPool.PERIOD + symbol, wci);
75      }
76  
77      public static boolean isCurrency(String symbol) {
78          return _instance._currencyIds.contains(symbol);
79      }
80  
81      private CurrencyUtil() {
82          _currencyIds = new HashSet<String>();
83  
84          _currencyIds.add("ALL");
85          _currencyIds.add("DZD");
86          _currencyIds.add("XAL");
87          _currencyIds.add("ARS");
88          _currencyIds.add("AWG");
89          _currencyIds.add("AUD");
90          _currencyIds.add("BSD");
91          _currencyIds.add("BHD");
92          _currencyIds.add("BDT");
93          _currencyIds.add("BBD");
94          _currencyIds.add("BYR");
95          _currencyIds.add("BZD");
96          _currencyIds.add("BMD");
97          _currencyIds.add("BTN");
98          _currencyIds.add("BOB");
99          _currencyIds.add("BRL");
100         _currencyIds.add("GBP");
101         _currencyIds.add("BND");
102         _currencyIds.add("BGN");
103         _currencyIds.add("BIF");
104         _currencyIds.add("KHR");
105         _currencyIds.add("CAD");
106         _currencyIds.add("KYD");
107         _currencyIds.add("XOF");
108         _currencyIds.add("XAF");
109         _currencyIds.add("CLP");
110         _currencyIds.add("CNY");
111         _currencyIds.add("COP");
112         _currencyIds.add("KMF");
113         _currencyIds.add("XCP");
114         _currencyIds.add("CRC");
115         _currencyIds.add("HRK");
116         _currencyIds.add("CUP");
117         _currencyIds.add("CYP");
118         _currencyIds.add("CZK");
119         _currencyIds.add("DKK");
120         _currencyIds.add("DJF");
121         _currencyIds.add("DOP");
122         _currencyIds.add("XCD");
123         _currencyIds.add("ECS");
124         _currencyIds.add("EGP");
125         _currencyIds.add("SVC");
126         _currencyIds.add("ERN");
127         _currencyIds.add("EEK");
128         _currencyIds.add("ETB");
129         _currencyIds.add("EUR");
130         _currencyIds.add("FKP");
131         _currencyIds.add("GMD");
132         _currencyIds.add("GHC");
133         _currencyIds.add("GIP");
134         _currencyIds.add("XAU");
135         _currencyIds.add("GTQ");
136         _currencyIds.add("GNF");
137         _currencyIds.add("HTG");
138         _currencyIds.add("HNL");
139         _currencyIds.add("HKD");
140         _currencyIds.add("HUF");
141         _currencyIds.add("ISK");
142         _currencyIds.add("INR");
143         _currencyIds.add("IDR");
144         _currencyIds.add("IRR");
145         _currencyIds.add("ILS");
146         _currencyIds.add("JMD");
147         _currencyIds.add("JPY");
148         _currencyIds.add("JOD");
149         _currencyIds.add("KZT");
150         _currencyIds.add("KES");
151         _currencyIds.add("KRW");
152         _currencyIds.add("KWD");
153         _currencyIds.add("LAK");
154         _currencyIds.add("LVL");
155         _currencyIds.add("LBP");
156         _currencyIds.add("LSL");
157         _currencyIds.add("LYD");
158         _currencyIds.add("LTL");
159         _currencyIds.add("MOP");
160         _currencyIds.add("MKD");
161         _currencyIds.add("MGF");
162         _currencyIds.add("MWK");
163         _currencyIds.add("MYR");
164         _currencyIds.add("MVR");
165         _currencyIds.add("MTL");
166         _currencyIds.add("MRO");
167         _currencyIds.add("MUR");
168         _currencyIds.add("MXN");
169         _currencyIds.add("MDL");
170         _currencyIds.add("MNT");
171         _currencyIds.add("MAD");
172         _currencyIds.add("MZM");
173         _currencyIds.add("NAD");
174         _currencyIds.add("NPR");
175         _currencyIds.add("ANG");
176         _currencyIds.add("TRY");
177         _currencyIds.add("NZD");
178         _currencyIds.add("NIO");
179         _currencyIds.add("NGN");
180         _currencyIds.add("NOK");
181         _currencyIds.add("OMR");
182         _currencyIds.add("XPF");
183         _currencyIds.add("PKR");
184         _currencyIds.add("XPD");
185         _currencyIds.add("PAB");
186         _currencyIds.add("PGK");
187         _currencyIds.add("PYG");
188         _currencyIds.add("PEN");
189         _currencyIds.add("PHP");
190         _currencyIds.add("XPT");
191         _currencyIds.add("PLN");
192         _currencyIds.add("QAR");
193         _currencyIds.add("ROL");
194         _currencyIds.add("RON");
195         _currencyIds.add("RUB");
196         _currencyIds.add("RWF");
197         _currencyIds.add("WST");
198         _currencyIds.add("STD");
199         _currencyIds.add("SAR");
200         _currencyIds.add("SCR");
201         _currencyIds.add("SLL");
202         _currencyIds.add("XAG");
203         _currencyIds.add("SGD");
204         _currencyIds.add("SKK");
205         _currencyIds.add("SIT");
206         _currencyIds.add("SOS");
207         _currencyIds.add("ZAR");
208         _currencyIds.add("LKR");
209         _currencyIds.add("SHP");
210         _currencyIds.add("SDD");
211         _currencyIds.add("SRG");
212         _currencyIds.add("SZL");
213         _currencyIds.add("SEK");
214         _currencyIds.add("CHF");
215         _currencyIds.add("SYP");
216         _currencyIds.add("TWD");
217         _currencyIds.add("TZS");
218         _currencyIds.add("THB");
219         _currencyIds.add("TOP");
220         _currencyIds.add("TTD");
221         _currencyIds.add("TND");
222         _currencyIds.add("USD");
223         _currencyIds.add("AED");
224         _currencyIds.add("UGX");
225         _currencyIds.add("UAH");
226         _currencyIds.add("UYU");
227         _currencyIds.add("VUV");
228         _currencyIds.add("VEB");
229         _currencyIds.add("VND");
230         _currencyIds.add("YER");
231         _currencyIds.add("ZMK");
232         _currencyIds.add("ZWD");
233     }
234 
235     private static CurrencyUtil _instance = new CurrencyUtil();
236 
237     private static Map<String, Map<String, String>> _symbolsPool =
238         new ConcurrentHashMap<String, Map<String, String>>();
239 
240     private Set<String> _currencyIds;
241 
242 }