use of javax.money.MonetaryAmountFactory 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;
}
use of javax.money.MonetaryAmountFactory in project jsr354-ri-bp by JavaMoney.
the class BaseMonetaryAmountsSingletonQuerySpi method getAmountTypes.
/**
* Executes the query and returns the {@link javax.money.MonetaryAmount} implementation types found.
*
* @param query the factory query, not null.
* @return the type found, or null.
*/
public Collection<Class<? extends MonetaryAmount>> getAmountTypes(MonetaryAmountFactoryQuery query) {
Collection<MonetaryAmountFactory<? extends MonetaryAmount>> factories = getAmountFactories(query);
Set<Class<? extends MonetaryAmount>> result = new HashSet<>();
for (MonetaryAmountFactory f : factories) {
// noinspection unchecked
result.add(f.getAmountType());
}
return result;
}
use of javax.money.MonetaryAmountFactory in project drbookings by DrBookings.
the class AirbnbEarningsCalculator method calculateEarnings.
public float calculateEarnings(Collection<? extends BookingBean> bookings) {
if (isPaymentDone()) {
bookings = bookings.stream().filter(b -> b.isPaymentDone()).collect(Collectors.toCollection(LinkedHashSet::new));
}
if (getDateRange() != null) {
bookings = bookings.stream().filter(b -> getDateRange().contains(b.getCheckOut())).collect(Collectors.toCollection(LinkedHashSet::new));
}
MonetaryAmountFactory<?> moneyFactory = Monetary.getDefaultAmountFactory().setCurrency(DrBookingsApplication.DEFAULT_CURRENCY.getCurrencyCode());
MonetaryAmount result = moneyFactory.setNumber(0).create();
if (logger.isDebugEnabled()) {
logger.debug("Calculating earnings for\n" + bookings.stream().map(b -> b.toString()).collect(Collectors.joining("\n")));
}
for (BookingBean b : bookings) {
if (b.getNumberOfNights() > LocalDate.now().getMonth().minLength()) {
if (logger.isDebugEnabled()) {
logger.debug("Days in month " + LocalDate.now().getMonth() + ": " + LocalDate.now().getMonth().minLength());
}
if (logger.isInfoEnabled()) {
logger.info("Airbnb long term booking, looking at manual registered payments");
}
Optional<Payment> op = Payments.getLastPayment(b.getPayments());
MonetaryAmount payment;
if (op.isPresent()) {
payment = op.get().getAmount();
} else {
payment = moneyFactory.setNumber(0).create();
}
if (logger.isDebugEnabled()) {
logger.debug("Payment is " + payment);
}
result = result.add(payment);
} else
result = result.add(moneyFactory.setNumber(b.getEarnings(isNetEarnings())).create());
}
return result.getNumber().floatValue();
}
Aggregations