< prev index next >

src/java.base/share/classes/sun/util/locale/provider/NumberFormatProviderImpl.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  *
  28  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  29  * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
  30  *
  31  * The original version of this source code and documentation
  32  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  33  * subsidiary of IBM. These materials are provided under terms
  34  * of a License Agreement between Taligent and Sun. This technology
  35  * is protected by multiple US and International patents.
  36  *
  37  * This notice and attribution to Taligent may not be removed.
  38  * Taligent is a registered trademark of Taligent, Inc.
  39  */
  40 
  41 package sun.util.locale.provider;
  42 

  43 import java.text.DecimalFormat;
  44 import java.text.DecimalFormatSymbols;
  45 import java.text.NumberFormat;
  46 import java.text.spi.NumberFormatProvider;
  47 import java.util.Currency;

  48 import java.util.Locale;

  49 import java.util.Set;
  50 
  51 /**
  52  * Concrete implementation of the  {@link java.text.spi.NumberFormatProvider
  53  * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
  54  *
  55  * @author Naoto Sato
  56  * @author Masayoshi Okutsu
  57  */
  58 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
  59 
  60     // Constants used by factory methods to specify a style of format.
  61     private static final int NUMBERSTYLE = 0;
  62     private static final int CURRENCYSTYLE = 1;
  63     private static final int PERCENTSTYLE = 2;
  64     private static final int SCIENTIFICSTYLE = 3;
  65     private static final int INTEGERSTYLE = 4;
  66 
  67     private final LocaleProviderAdapter.Type type;
  68     private final Set<String> langtags;


 206             try {
 207                 currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
 208             } catch (IllegalArgumentException e) {
 209             }
 210         }
 211         if (currency != null) {
 212             int digits = currency.getDefaultFractionDigits();
 213             if (digits != -1) {
 214                 int oldMinDigits = format.getMinimumFractionDigits();
 215                 // Common patterns are "#.##", "#.00", "#".
 216                 // Try to adjust all of them in a reasonable way.
 217                 if (oldMinDigits == format.getMaximumFractionDigits()) {
 218                     format.setMinimumFractionDigits(digits);
 219                     format.setMaximumFractionDigits(digits);
 220                 } else {
 221                     format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
 222                     format.setMaximumFractionDigits(digits);
 223                 }
 224             }
 225         }











































 226     }
 227 
 228     @Override
 229     public Set<String> getAvailableLanguageTags() {
 230         return langtags;
 231     }
 232 }
   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  *
  28  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  29  * (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved
  30  *
  31  * The original version of this source code and documentation
  32  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  33  * subsidiary of IBM. These materials are provided under terms
  34  * of a License Agreement between Taligent and Sun. This technology
  35  * is protected by multiple US and International patents.
  36  *
  37  * This notice and attribution to Taligent may not be removed.
  38  * Taligent is a registered trademark of Taligent, Inc.
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.text.CompactNumberFormat;
  44 import java.text.DecimalFormat;
  45 import java.text.DecimalFormatSymbols;
  46 import java.text.NumberFormat;
  47 import java.text.spi.NumberFormatProvider;
  48 import java.util.Currency;
  49 import java.util.List;
  50 import java.util.Locale;
  51 import java.util.Objects;
  52 import java.util.Set;
  53 
  54 /**
  55  * Concrete implementation of the  {@link java.text.spi.NumberFormatProvider
  56  * NumberFormatProvider} class for the JRE LocaleProviderAdapter.
  57  *
  58  * @author Naoto Sato
  59  * @author Masayoshi Okutsu
  60  */
  61 public class NumberFormatProviderImpl extends NumberFormatProvider implements AvailableLanguageTags {
  62 
  63     // Constants used by factory methods to specify a style of format.
  64     private static final int NUMBERSTYLE = 0;
  65     private static final int CURRENCYSTYLE = 1;
  66     private static final int PERCENTSTYLE = 2;
  67     private static final int SCIENTIFICSTYLE = 3;
  68     private static final int INTEGERSTYLE = 4;
  69 
  70     private final LocaleProviderAdapter.Type type;
  71     private final Set<String> langtags;


 209             try {
 210                 currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
 211             } catch (IllegalArgumentException e) {
 212             }
 213         }
 214         if (currency != null) {
 215             int digits = currency.getDefaultFractionDigits();
 216             if (digits != -1) {
 217                 int oldMinDigits = format.getMinimumFractionDigits();
 218                 // Common patterns are "#.##", "#.00", "#".
 219                 // Try to adjust all of them in a reasonable way.
 220                 if (oldMinDigits == format.getMaximumFractionDigits()) {
 221                     format.setMinimumFractionDigits(digits);
 222                     format.setMaximumFractionDigits(digits);
 223                 } else {
 224                     format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
 225                     format.setMaximumFractionDigits(digits);
 226                 }
 227             }
 228         }
 229     }
 230     
 231     /**
 232      * Returns a new {@code NumberFormat} instance which formats
 233      * a number in its compact form for the specified
 234      * {@code locale} and {@code formatStyle}.
 235      *
 236      * @param locale the desired locale
 237      * @param formatStyle the style for formatting a number
 238      * @throws NullPointerException if {@code locale} or {@code formatStyle}
 239      *     is {@code null}
 240      * @throws IllegalArgumentException if {@code locale} isn't
 241      *     one of the locales returned from
 242      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 243      *     getAvailableLocales()}.
 244      * @return a compact number formatter
 245      *
 246      * @see java.text.NumberFormat#getCompactNumberInstance(Locale,
 247      *                      NumberFormat.Style)
 248      * @since 12
 249      */
 250     @Override
 251     public NumberFormat getCompactNumberInstance(Locale locale,
 252             NumberFormat.Style formatStyle) {
 253 
 254         Objects.requireNonNull(locale);
 255         Objects.requireNonNull(formatStyle);
 256 
 257         // Check for region override
 258         Locale override = locale.getUnicodeLocaleType("nu") == null
 259                 ? CalendarDataUtility.findRegionOverride(locale)
 260                 : locale;
 261 
 262         LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
 263         LocaleResources resource = adapter.getLocaleResources(override);
 264 
 265         String[] numberPatterns = resource.getNumberPatterns();
 266         DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(override);
 267         List<String> cnPatterns = resource.getCNPatterns(formatStyle);
 268 
 269         CompactNumberFormat format = new CompactNumberFormat(numberPatterns[0],
 270                 symbols, cnPatterns.toArray(new String[cnPatterns.size()]));
 271         return format;
 272     }
 273 
 274     @Override
 275     public Set<String> getAvailableLanguageTags() {
 276         return langtags;
 277     }
 278 }
< prev index next >