Search in sources :

Example 1 with MonetaryAmountFactory

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

Example 2 with MonetaryAmountFactory

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;
}
Also used : MonetaryAmount(javax.money.MonetaryAmount) MonetaryAmountFactory(javax.money.MonetaryAmountFactory) HashSet(java.util.HashSet)

Example 3 with MonetaryAmountFactory

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BookingBean(com.github.drbookings.model.data.BookingBean) Monetary(javax.money.Monetary) MonetaryAmountFactory(javax.money.MonetaryAmountFactory) Logger(org.slf4j.Logger) Collection(java.util.Collection) Range(com.google.common.collect.Range) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) DrBookingsApplication(com.github.drbookings.DrBookingsApplication) LocalDate(java.time.LocalDate) Optional(java.util.Optional) MonetaryAmount(javax.money.MonetaryAmount) LinkedHashSet(java.util.LinkedHashSet) MonetaryAmount(javax.money.MonetaryAmount) BookingBean(com.github.drbookings.model.data.BookingBean)

Aggregations

MonetaryAmount (javax.money.MonetaryAmount)3 MonetaryAmountFactory (javax.money.MonetaryAmountFactory)3 DrBookingsApplication (com.github.drbookings.DrBookingsApplication)1 BookingBean (com.github.drbookings.model.data.BookingBean)1 Range (com.google.common.collect.Range)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Monetary (javax.money.Monetary)1 MonetaryContext (javax.money.MonetaryContext)1 MonetaryException (javax.money.MonetaryException)1 MonetaryAmountFactoryProviderSpi (javax.money.spi.MonetaryAmountFactoryProviderSpi)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1