Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/providersrc/NumberFormatProviderImpl.java
47311 views
/*1* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23*24*/2526package com.foo;2728import java.text.*;29import java.text.spi.*;30import java.util.*;3132import com.foobar.Utils;3334public class NumberFormatProviderImpl extends NumberFormatProvider {3536static Locale[] avail = {37Locale.JAPAN,38new Locale("ja", "JP", "osaka"),39new Locale("ja", "JP", "kyoto"),40new Locale("zz")};4142static String[] dialect = {43"\u3067\u3059\u3002",44"\u3084\u3002",45"\u3069\u3059\u3002",46"-zz"47};4849static String[] patterns = {50"#,##0.###{0};-#,##0.###{1}", // decimal pattern51"#{0};(#){1}", // integer pattern52"\u00A4#,##0{0};-\u00A4#,##0{1}", // currency pattern53"#,##0%{0}" // percent pattern54};55// Constants used by factory methods to specify a style of format.56static final int NUMBERSTYLE = 0;57static final int INTEGERSTYLE = 1;58static final int CURRENCYSTYLE = 2;59static final int PERCENTSTYLE = 3;6061public Locale[] getAvailableLocales() {62return avail;63}6465public NumberFormat getCurrencyInstance(Locale locale) {66for (int i = 0; i < avail.length; i ++) {67if (Utils.supportsLocale(avail[i], locale)) {68String pattern =69MessageFormat.format(patterns[CURRENCYSTYLE],70dialect[i],71dialect[i]);72FooNumberFormat nf = new FooNumberFormat(pattern,73DecimalFormatSymbols.getInstance(locale));74adjustForCurrencyDefaultFractionDigits(nf);75return nf;76}77}78throw new IllegalArgumentException("locale is not supported: "+locale);79}8081public NumberFormat getIntegerInstance(Locale locale) {82for (int i = 0; i < avail.length; i ++) {83if (Utils.supportsLocale(avail[i], locale)) {84String pattern =85MessageFormat.format(patterns[INTEGERSTYLE],86dialect[i],87dialect[i]);88FooNumberFormat nf = new FooNumberFormat(pattern,89DecimalFormatSymbols.getInstance(locale));90nf.setMaximumFractionDigits(0);91nf.setDecimalSeparatorAlwaysShown(false);92nf.setParseIntegerOnly(true);93return nf;94}95}96throw new IllegalArgumentException("locale is not supported: "+locale);97}9899public NumberFormat getNumberInstance(Locale locale) {100for (int i = 0; i < avail.length; i ++) {101if (Utils.supportsLocale(avail[i], locale)) {102String pattern =103MessageFormat.format(patterns[NUMBERSTYLE],104dialect[i],105dialect[i]);106return new FooNumberFormat(pattern,107DecimalFormatSymbols.getInstance(locale));108}109}110throw new IllegalArgumentException("locale is not supported: "+locale);111}112113public NumberFormat getPercentInstance(Locale locale) {114for (int i = 0; i < avail.length; i ++) {115if (Utils.supportsLocale(avail[i], locale)) {116String pattern =117MessageFormat.format(patterns[PERCENTSTYLE],118dialect[i]);119return new FooNumberFormat(pattern,120DecimalFormatSymbols.getInstance(locale));121}122}123throw new IllegalArgumentException("locale is not supported: "+locale);124}125126/**127* Adjusts the minimum and maximum fraction digits to values that128* are reasonable for the currency's default fraction digits.129*/130void adjustForCurrencyDefaultFractionDigits(FooNumberFormat nf) {131DecimalFormatSymbols dfs = nf.getDecimalFormatSymbols();132Currency currency = dfs.getCurrency();133if (currency == null) {134try {135currency = Currency.getInstance(dfs.getInternationalCurrencySymbol());136} catch (IllegalArgumentException e) {137}138}139if (currency != null) {140int digits = currency.getDefaultFractionDigits();141if (digits != -1) {142int oldMinDigits = nf.getMinimumFractionDigits();143// Common patterns are "#.##", "#.00", "#".144// Try to adjust all of them in a reasonable way.145if (oldMinDigits == nf.getMaximumFractionDigits()) {146nf.setMinimumFractionDigits(digits);147nf.setMaximumFractionDigits(digits);148} else {149nf.setMinimumFractionDigits(Math.min(digits, oldMinDigits));150nf.setMaximumFractionDigits(digits);151}152}153}154}155}156157158