1 /*
2  *******************************************************************************
3  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.impl.javaspi.text;
8 
9 import java.text.NumberFormat;
10 import java.text.spi.NumberFormatProvider;
11 import java.util.Locale;
12 
13 import com.ibm.icu.impl.javaspi.ICULocaleServiceProvider;
14 import com.ibm.icu.impl.jdkadapter.DecimalFormatICU;
15 import com.ibm.icu.impl.jdkadapter.NumberFormatICU;
16 import com.ibm.icu.util.ULocale;
17 
18 public class NumberFormatProviderICU extends NumberFormatProvider {
19 
20     private final static int NUMBER   = 0;
21     private final static int INTEGER  = 1;
22     private final static int CURRENCY = 2;
23     private final static int PERCENT  = 3;
24 
25     @Override
getCurrencyInstance(Locale locale)26     public NumberFormat getCurrencyInstance(Locale locale) {
27         return getInstance(CURRENCY, locale);
28     }
29 
30     @Override
getIntegerInstance(Locale locale)31     public NumberFormat getIntegerInstance(Locale locale) {
32         return getInstance(INTEGER, locale);
33     }
34 
35     @Override
getNumberInstance(Locale locale)36     public NumberFormat getNumberInstance(Locale locale) {
37         return getInstance(NUMBER, locale);
38     }
39 
40     @Override
getPercentInstance(Locale locale)41     public NumberFormat getPercentInstance(Locale locale) {
42         return getInstance(PERCENT, locale);
43     }
44 
45     @Override
getAvailableLocales()46     public Locale[] getAvailableLocales() {
47         return ICULocaleServiceProvider.getAvailableLocales();
48     }
49 
getInstance(int type, Locale locale)50     private NumberFormat getInstance(int type, Locale locale) {
51         com.ibm.icu.text.NumberFormat icuNfmt;
52         ULocale actual = ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale);
53         switch (type) {
54         case NUMBER:
55             icuNfmt = com.ibm.icu.text.NumberFormat.getNumberInstance(actual);
56             break;
57         case INTEGER:
58             icuNfmt = com.ibm.icu.text.NumberFormat.getIntegerInstance(actual);
59             break;
60         case CURRENCY:
61             icuNfmt = com.ibm.icu.text.NumberFormat.getCurrencyInstance(actual);
62             break;
63         case PERCENT:
64             icuNfmt = com.ibm.icu.text.NumberFormat.getPercentInstance(actual);
65             break;
66         default:
67             return null;
68         }
69 
70         if (!(icuNfmt instanceof com.ibm.icu.text.DecimalFormat)) {
71             // icuNfmt must be always DecimalFormat
72             return null;
73         }
74 
75         NumberFormat nf = null;
76         if (ICULocaleServiceProvider.useDecimalFormat()) {
77             nf = DecimalFormatICU.wrap((com.ibm.icu.text.DecimalFormat)icuNfmt);
78         } else {
79             nf = NumberFormatICU.wrap(icuNfmt);
80         }
81 
82         return nf;
83     }
84 }
85