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