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;
}
Aggregations