1 /*
2  *******************************************************************************
3  * Copyright (C) 2009-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.impl;
8 
9 import java.util.Collections;
10 import java.util.Map;
11 
12 import com.ibm.icu.text.CurrencyDisplayNames;
13 import com.ibm.icu.util.ULocale;
14 
15 public class CurrencyData {
16     public static final CurrencyDisplayInfoProvider provider;
17 
18     public static interface CurrencyDisplayInfoProvider {
getInstance(ULocale locale, boolean withFallback)19         CurrencyDisplayInfo getInstance(ULocale locale, boolean withFallback);
hasData()20         boolean hasData();
21     }
22 
23     public static abstract class CurrencyDisplayInfo extends CurrencyDisplayNames {
getUnitPatterns()24         public abstract Map<String, String> getUnitPatterns();
getFormatInfo(String isoCode)25         public abstract CurrencyFormatInfo getFormatInfo(String isoCode);
getSpacingInfo()26         public abstract CurrencySpacingInfo getSpacingInfo();
27     }
28 
29     public static final class CurrencyFormatInfo {
30         public final String currencyPattern;
31         public final char monetarySeparator;
32         public final char monetaryGroupingSeparator;
33 
CurrencyFormatInfo(String currencyPattern, char monetarySeparator, char monetaryGroupingSeparator)34         public CurrencyFormatInfo(String currencyPattern, char monetarySeparator,
35                 char monetaryGroupingSeparator) {
36             this.currencyPattern = currencyPattern;
37             this.monetarySeparator = monetarySeparator;
38             this.monetaryGroupingSeparator = monetaryGroupingSeparator;
39         }
40     }
41 
42     public static final class CurrencySpacingInfo {
43         public final String beforeCurrencyMatch;
44         public final String beforeContextMatch;
45         public final String beforeInsert;
46         public final String afterCurrencyMatch;
47         public final String afterContextMatch;
48         public final String afterInsert;
49 
CurrencySpacingInfo( String beforeCurrencyMatch, String beforeContextMatch, String beforeInsert, String afterCurrencyMatch, String afterContextMatch, String afterInsert)50         public CurrencySpacingInfo(
51                 String beforeCurrencyMatch, String beforeContextMatch, String beforeInsert,
52                 String afterCurrencyMatch, String afterContextMatch, String afterInsert) {
53             this.beforeCurrencyMatch = beforeCurrencyMatch;
54             this.beforeContextMatch = beforeContextMatch;
55             this.beforeInsert = beforeInsert;
56             this.afterCurrencyMatch = afterCurrencyMatch;
57             this.afterContextMatch = afterContextMatch;
58             this.afterInsert = afterInsert;
59         }
60 
61 
62         private static final String DEFAULT_CUR_MATCH = "[:letter:]";
63         private static final String DEFAULT_CTX_MATCH = "[:digit:]";
64         private static final String DEFAULT_INSERT = " ";
65 
66         public static final CurrencySpacingInfo DEFAULT = new CurrencySpacingInfo(
67                 DEFAULT_CUR_MATCH, DEFAULT_CTX_MATCH, DEFAULT_INSERT,
68                 DEFAULT_CUR_MATCH, DEFAULT_CTX_MATCH, DEFAULT_INSERT);
69     }
70 
71     static {
72         CurrencyDisplayInfoProvider temp = null;
73         try {
74             Class<?> clzz = Class.forName("com.ibm.icu.impl.ICUCurrencyDisplayInfoProvider");
75             temp = (CurrencyDisplayInfoProvider) clzz.newInstance();
76         } catch (Throwable t) {
77             temp = new CurrencyDisplayInfoProvider() {
78                 public CurrencyDisplayInfo getInstance(ULocale locale, boolean withFallback) {
79                     return DefaultInfo.getWithFallback(withFallback);
80                 }
81 
82                 public boolean hasData() {
83                     return false;
84                 }
85             };
86         }
87         provider = temp;
88     }
89 
90     public static class DefaultInfo extends CurrencyDisplayInfo {
91         private final boolean fallback;
92 
DefaultInfo(boolean fallback)93         private DefaultInfo(boolean fallback) {
94             this.fallback = fallback;
95         }
96 
getWithFallback(boolean fallback)97         public static final CurrencyDisplayInfo getWithFallback(boolean fallback) {
98             return fallback ? FALLBACK_INSTANCE : NO_FALLBACK_INSTANCE;
99         }
100 
101         @Override
getName(String isoCode)102         public String getName(String isoCode) {
103             return fallback ? isoCode : null;
104         }
105 
106         @Override
getPluralName(String isoCode, String pluralType)107         public String getPluralName(String isoCode, String pluralType) {
108             return fallback ? isoCode : null;
109         }
110 
111         @Override
getSymbol(String isoCode)112         public String getSymbol(String isoCode) {
113             return fallback ? isoCode : null;
114         }
115 
116         @Override
symbolMap()117         public Map<String, String> symbolMap() {
118             return Collections.emptyMap();
119         }
120 
121         @Override
nameMap()122         public Map<String, String> nameMap() {
123             return Collections.emptyMap();
124         }
125 
126         @Override
getULocale()127         public ULocale getULocale() {
128             return ULocale.ROOT;
129         }
130 
131         @Override
getUnitPatterns()132         public Map<String, String> getUnitPatterns() {
133             if (fallback) {
134                 return Collections.emptyMap();
135             }
136             return null;
137         }
138 
139         @Override
getFormatInfo(String isoCode)140         public CurrencyFormatInfo getFormatInfo(String isoCode) {
141             return null;
142         }
143 
144         @Override
getSpacingInfo()145         public CurrencySpacingInfo getSpacingInfo() {
146             return fallback ? CurrencySpacingInfo.DEFAULT : null;
147         }
148 
149         private static final CurrencyDisplayInfo FALLBACK_INSTANCE = new DefaultInfo(true);
150         private static final CurrencyDisplayInfo NO_FALLBACK_INSTANCE = new DefaultInfo(false);
151     }
152 }
153