com.phloc.masterdata.currency.CurrencyUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of phloc-masterdata Show documentation
Show all versions of phloc-masterdata Show documentation
Library with support for certain re-ocurring standard business objects
/**
* Copyright (C) 2006-2014 phloc systems
* http://www.phloc.com
* office[at]phloc[dot]com
*
* 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.
*/
package com.phloc.masterdata.currency;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.locale.LocaleCache;
import com.phloc.commons.locale.LocaleFormatter;
/**
* Some currency helper methods.
*
* @author Philip Helger
*/
@Immutable
public final class CurrencyUtils
{
// Sorted set of all available currencies
private static SortedSet s_aAllCurrencies = new TreeSet (new ComparatorCurrencyCode ());
private static Map s_aLocaleToCurrency = new HashMap ();
static
{
// For all locales
for (final Locale aLocale : LocaleCache.getAllLocales ())
{
try
{
final Currency aCurrency = Currency.getInstance (aLocale);
if (aCurrency != null)
{
s_aAllCurrencies.add (aCurrency);
s_aLocaleToCurrency.put (aLocale, aCurrency);
}
}
catch (final IllegalArgumentException ex)
{
// No currency present for locale
}
}
}
private CurrencyUtils ()
{}
@Nonnull
@ReturnsMutableCopy
public static Set getAllSupportedCurrencies ()
{
return new TreeSet (s_aAllCurrencies);
}
public static boolean isSupportedCurrency (@Nullable final Currency aCurrency)
{
return aCurrency != null && s_aAllCurrencies.contains (aCurrency);
}
public static boolean isSupportedCurrency (@Nullable final ECurrency eCurrency)
{
return eCurrency != null && isSupportedCurrency (eCurrency.getAsCurrency ());
}
public static boolean isSupportedCurrencyCode (@Nonnull final String sCurrencyCode)
{
try
{
return isSupportedCurrency (Currency.getInstance (sCurrencyCode));
}
catch (final IllegalArgumentException ex)
{
// No such currency code
return false;
}
}
/**
* Check if a currency could be available for the given locale.
*
* @param aLocale
* The locale to check.
* @return true
if a currency is available for the given locale,
* false
otherwise.
*/
public static boolean localeSupportsCurrencyRetrieval (@Nullable final Locale aLocale)
{
return aLocale != null && aLocale.getCountry () != null && aLocale.getCountry ().length () == 2;
}
@Nullable
public static Currency getCurrencyOfLocale (@Nonnull final Locale aContentLocale)
{
if (!localeSupportsCurrencyRetrieval (aContentLocale))
throw new IllegalArgumentException ("Cannot get currency of locale " + aContentLocale);
return s_aLocaleToCurrency.get (aContentLocale);
}
/**
* @return A map from {@link Locale} to {@link Currency} as offered by the
* JDK.
*/
@Nonnull
@ReturnsMutableCopy
public static Map getLocaleToCurrencyMap ()
{
return ContainerHelper.newMap (s_aLocaleToCurrency);
}
/**
* Parse a currency value from string using the currency default rounding
* mode.
* Source:
* http://wheelworkshop.blogspot.com/2006/02/parsing-currency-into-bigdecimal.html
*
* @param sStr
* The string to be parsed.
* @param aFormat
* The formatting object to be used. May not be null
.
* @param aDefault
* The default value to be returned, if parsing failed.
* @return Either default value or the {@link BigDecimal} value with the
* correct scaling.
*/
@Nullable
public static BigDecimal parseCurrency (@Nullable final String sStr,
@Nonnull final DecimalFormat aFormat,
@Nullable final BigDecimal aDefault)
{
return parseCurrency (sStr, aFormat, aDefault, ECurrency.DEFAULT_ROUNDING_MODE);
}
/**
* Parse a currency value from string using a custom rounding mode.
*
* @param sStr
* The string to be parsed.
* @param aFormat
* The formatting object to be used. May not be null
.
* @param aDefault
* The default value to be returned, if parsing failed.
* @param eRoundingMode
* The rounding mode to be used. May not be null
.
* @return Either default value or the {@link BigDecimal} value with the
* correct scaling.
*/
@Nullable
public static BigDecimal parseCurrency (@Nullable final String sStr,
@Nonnull final DecimalFormat aFormat,
@Nullable final BigDecimal aDefault,
@Nonnull final RoundingMode eRoundingMode)
{
// So that the call to "parse" returns a BigDecimal
aFormat.setParseBigDecimal (true);
aFormat.setRoundingMode (eRoundingMode);
// Parse as double
final BigDecimal aNum = LocaleFormatter.parseBigDecimal (sStr, aFormat);
if (aNum == null)
return aDefault;
// And finally do the correct scaling, depending of the decimal format
// fraction
return aNum.setScale (aFormat.getMaximumFractionDigits (), eRoundingMode);
}
}