Java Number Format Pattern getNumberFormatFromCache(Locale locale, Currency currency)

Here you can find the source of getNumberFormatFromCache(Locale locale, Currency currency)

Description

Provides a cached approach for creating NumberFormat instances.

License

Apache License

Parameter

Parameter Description
locale the Locale
currency the Currency

Return

either a new NumberFormat instance, or one taken from the cache

Declaration

public static NumberFormat getNumberFormatFromCache(Locale locale,
        Currency currency) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j av  a2  s  . co m
 * #%L
 * BroadleafCommerce Common Libraries
 * %%
 * Copyright (C) 2009 - 2013 Broadleaf Commerce
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    protected static final Map<String, NumberFormat> FORMAT_CACHE = new ConcurrentHashMap<String, NumberFormat>();

    /**
     * Provides a cached approach for creating NumberFormat instances. More performant
     * than creating a new one each time.
     *
     * @param locale the Locale
     * @param currency the Currency
     * @return either a new NumberFormat instance, or one taken from the cache
     */
    public static NumberFormat getNumberFormatFromCache(Locale locale,
            Currency currency) {
        String key = locale.toString() + currency.getCurrencyCode();
        if (!FORMAT_CACHE.containsKey(key)) {
            NumberFormat format = NumberFormat.getCurrencyInstance(locale);
            format.setCurrency(currency);
            FORMAT_CACHE.put(key, format);
        }
        return FORMAT_CACHE.get(key);
    }
}

Related

  1. getNumberFormat(double d)
  2. getNumberFormat(final String pattern)
  3. getNumberFormat(int maximumFractionDigits)
  4. getNumberFormat(String format, Locale locale)
  5. getNumberFormat(String localeStr)
  6. getNumberFormatInstanceFor2DecimalDigits()
  7. getNumberFormatter()
  8. getNumberFormatter(final String format)
  9. getNumberFraction2Format(final Locale locale)