Search in sources :

Example 1 with MonetaryException

use of javax.money.MonetaryException in project jsr354-ri by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method getProvidersToUse.

private Collection<String> getProvidersToUse(ConversionQuery query) {
    List<String> providersToUse = new ArrayList<>();
    List<String> providerNames = query.getProviderNames();
    if (providerNames.isEmpty()) {
        providerNames = getDefaultProviderChain();
        if (providerNames.isEmpty()) {
            throw new IllegalStateException("No default provider chain available.");
        }
    }
    for (String provider : providerNames) {
        ExchangeRateProvider prov = this.conversionProviders.get(provider);
        if (prov == null) {
            throw new MonetaryException("Invalid ExchangeRateProvider (not found): " + provider);
        }
        providersToUse.add(provider);
    }
    return providersToUse;
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) MonetaryException(javax.money.MonetaryException)

Example 2 with MonetaryException

use of javax.money.MonetaryException in project jsr354-ri by JavaMoney.

the class MoneyUtils method checkAmountParameter.

/**
 * Method to check if a currency is compatible with this amount instance.
 *
 * @param amount       The monetary amount to be compared to, never null.
 * @param currencyUnit the currency unit to compare, never null.
 * @throws MonetaryException If the amount is null, or the amount's {@link CurrencyUnit} is not
 *                           compatible, meaning has a different value of
 *                           {@link CurrencyUnit#getCurrencyCode()}).
 */
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
    Objects.requireNonNull(amount, "Amount must not be null.");
    final CurrencyUnit amountCurrency = amount.getCurrency();
    if (!(currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode()))) {
        throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
    }
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryException(javax.money.MonetaryException)

Example 3 with MonetaryException

use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.

the class CurrencyToken method parse.

/**
 * Parses a currency from the given {@link ParseContext}. Depending on the
 * current {@link CurrencyStyle} it interprets the next non empty token,
 * either as
 * <ul>
 * <li>currency code
 * <li>currency symbol
 * </ul>
 * Parsing of localized currency names or numeric code is not supported.
 *
 * @throws UnsupportedOperationException if the {@link CurrencyStyle} is configured to us currency
 *                                       names, or numeric codes for formatting.
 */
@Override
public void parse(ParseContext context) throws MonetaryParseException {
    String token = context.lookupNextToken();
    while (token != null) {
        if (!token.trim().isEmpty()) {
            break;
        }
        context.consume(token);
        token = context.lookupNextToken();
    }
    if (token == null) {
        throw new MonetaryException("Error parsing CurrencyUnit: token expected");
    }
    try {
        CurrencyUnit cur;
        switch(style) {
            case CODE:
                if (!Monetary.isCurrencyAvailable(token)) {
                    // Perhaps blank is missing between currency code and number...
                    String subCurrency = parseCurrencyCode(token);
                    cur = Monetary.getCurrency(subCurrency);
                    context.consume(subCurrency);
                } else {
                    cur = Monetary.getCurrency(token);
                    context.consume(token);
                }
                break;
            case SYMBOL:
                if (token.startsWith("$")) {
                    throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex());
                } else if (token.startsWith("€")) {
                    cur = Monetary.getCurrency("EUR");
                    context.consume("€");
                } else if (token.startsWith("£")) {
                    cur = Monetary.getCurrency("GBP");
                    context.consume("£");
                } else {
                    cur = Monetary.getCurrency(token);
                    context.consume(token);
                }
                context.setParsedCurrency(cur);
                break;
            case NAME:
            case NUMERIC_CODE:
            default:
                throw new UnsupportedOperationException("Not yet implemented");
        }
        if (cur != null) {
            context.setParsedCurrency(cur);
        }
    } catch (Exception e) {
        throw new MonetaryException("Error parsing CurrencyUnit.", e);
    }
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryParseException(javax.money.format.MonetaryParseException) MonetaryException(javax.money.MonetaryException) IOException(java.io.IOException) MonetaryParseException(javax.money.format.MonetaryParseException) MonetaryException(javax.money.MonetaryException)

Example 4 with MonetaryException

use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(ConversionQuery query) {
    Collection<String> providers = getProvidersToUse(query);
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    for (String provName : providers) {
        ExchangeRateProvider prov = this.conversionProviders.get(provName);
        if (prov == null) {
            throw new MonetaryException("Unsupported conversion/rate provider: " + provName);
        }
        provInstances.add(prov);
    }
    if (provInstances.isEmpty()) {
        throw new MonetaryException("No such providers: " + query);
    }
    if (provInstances.size() == 1) {
        return provInstances.get(0);
    }
    return new CompoundRateProvider(provInstances);
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) CompoundRateProvider(org.javamoney.moneta.spi.CompoundRateProvider) MonetaryException(javax.money.MonetaryException)

Example 5 with MonetaryException

use of javax.money.MonetaryException in project jsr354-ri-bp by JavaMoney.

the class DefaultMonetaryConversionsSingletonSpi method getExchangeRateProvider.

@Override
public ExchangeRateProvider getExchangeRateProvider(String... providers) {
    List<ExchangeRateProvider> provInstances = new ArrayList<>();
    for (String provName : providers) {
        ExchangeRateProvider prov = this.conversionProviders.get(provName);
        if (prov == null) {
            throw new MonetaryException("Unsupported conversion/rate provider: " + provName);
        }
        provInstances.add(prov);
    }
    if (provInstances.size() == 1) {
        return provInstances.get(0);
    }
    return new CompoundRateProvider(provInstances);
}
Also used : ExchangeRateProvider(javax.money.convert.ExchangeRateProvider) ArrayList(java.util.ArrayList) CompoundRateProvider(org.javamoney.moneta.spi.CompoundRateProvider) MonetaryException(javax.money.MonetaryException)

Aggregations

MonetaryException (javax.money.MonetaryException)18 ArrayList (java.util.ArrayList)8 CurrencyUnit (javax.money.CurrencyUnit)8 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)7 ExchangeRate (javax.money.convert.ExchangeRate)6 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)6 CompoundRateProvider (org.javamoney.moneta.spi.CompoundRateProvider)4 LocalDate (java.time.LocalDate)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Comparator (java.util.Comparator)2 Map (java.util.Map)2 Objects (java.util.Objects)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Monetary (javax.money.Monetary)2