Search in sources :

Example 1 with MonetaryAmountFactoryProviderSpi

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

the class DefaultMonetaryAmountsSingletonQuerySpi method getAmountFactories.

/**
 * (non-Javadoc)
 *
 * @see javax.money.spi.MonetaryAmountsSingletonQuerySpi#getAmountFactories(javax.money.MonetaryAmountFactoryQuery)
 */
@Override
public Collection<MonetaryAmountFactory<?>> getAmountFactories(MonetaryAmountFactoryQuery factoryQuery) {
    Objects.requireNonNull(factoryQuery);
    List<MonetaryAmountFactory<?>> factories = new ArrayList<>();
    // first check for explicit type
    for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
        if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
            continue;
        }
        if (factoryQuery.getTargetType() == factory.getAmountType()) {
            if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
                factories.add(factory.createMonetaryAmountFactory());
            } else {
                throw new MonetaryException("Incompatible context required=" + factoryQuery + ", maximal=" + factory.getMaximalMonetaryContext());
            }
        }
    }
    List<MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount>> selection = new ArrayList<>();
    for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
        if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.DIRECT_REFERENCE_ONLY || factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
            continue;
        }
        if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
            selection.add(factory);
        }
    }
    if (selection.isEmpty()) {
        // fall back, add all selections, ignore flavor
        for (@SuppressWarnings("unchecked") MonetaryAmountFactoryProviderSpi<? extends MonetaryAmount> factory : Bootstrap.getServices(MonetaryAmountFactoryProviderSpi.class)) {
            if (factory.getQueryInclusionPolicy() == QueryInclusionPolicy.DIRECT_REFERENCE_ONLY || factory.getQueryInclusionPolicy() == QueryInclusionPolicy.NEVER) {
                continue;
            }
            if (isPrecisionOK(factoryQuery, factory.getMaximalMonetaryContext())) {
                selection.add(factory);
            }
        }
    }
    if (selection.size() == 1) {
        factories.add(selection.get(0).createMonetaryAmountFactory());
    }
    MonetaryContext context = createContext(factoryQuery);
    factories.forEach(f -> f.setContext(context));
    selection.sort(CONTEXT_COMPARATOR);
    factories.add(selection.get(0).createMonetaryAmountFactory());
    return factories;
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryAmountFactoryProviderSpi(javax.money.spi.MonetaryAmountFactoryProviderSpi) ArrayList(java.util.ArrayList) MonetaryAmountFactory(javax.money.MonetaryAmountFactory) MonetaryContext(javax.money.MonetaryContext) MonetaryException(javax.money.MonetaryException)

Aggregations

ArrayList (java.util.ArrayList)1 MonetaryAmount (javax.money.MonetaryAmount)1 MonetaryAmountFactory (javax.money.MonetaryAmountFactory)1 MonetaryContext (javax.money.MonetaryContext)1 MonetaryException (javax.money.MonetaryException)1 MonetaryAmountFactoryProviderSpi (javax.money.spi.MonetaryAmountFactoryProviderSpi)1