Search in sources :

Example 1 with LinkedListMultimap

use of com.google.common.collect.LinkedListMultimap in project fastjson by alibaba.

the class LinkedListMultimapTest method test_for_multimap.

public void test_for_multimap() throws Exception {
    LinkedListMultimap map = LinkedListMultimap.create();
    map.put("name", "a");
    map.put("name", "b");
    String json = JSON.toJSONString(map);
    assertEquals("{\"name\":[\"a\",\"b\"]}", json);
}
Also used : LinkedListMultimap(com.google.common.collect.LinkedListMultimap)

Example 2 with LinkedListMultimap

use of com.google.common.collect.LinkedListMultimap in project killbill by killbill.

the class FixedAndRecurringInvoiceItemGenerator method safetyBounds.

@VisibleForTesting
void safetyBounds(final Iterable<InvoiceItem> resultingItems, final Multimap<UUID, LocalDate> createdItemsPerDayPerSubscription, final InternalTenantContext internalCallContext) throws InvoiceApiException {
    // See https://github.com/killbill/killbill/issues/664
    if (config.isSanitySafetyBoundEnabled(internalCallContext)) {
        final Map<UUID, Multimap<LocalDate, InvoiceItem>> fixedItemsPerDateAndSubscription = new HashMap<UUID, Multimap<LocalDate, InvoiceItem>>();
        final Map<UUID, Multimap<Range<LocalDate>, InvoiceItem>> recurringItemsPerServicePeriodAndSubscription = new HashMap<UUID, Multimap<Range<LocalDate>, InvoiceItem>>();
        for (final InvoiceItem resultingItem : resultingItems) {
            if (resultingItem.getInvoiceItemType() == InvoiceItemType.FIXED) {
                if (fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()) == null) {
                    fixedItemsPerDateAndSubscription.put(resultingItem.getSubscriptionId(), LinkedListMultimap.<LocalDate, InvoiceItem>create());
                }
                fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()).put(resultingItem.getStartDate(), resultingItem);
                final Collection<InvoiceItem> resultingInvoiceItems = fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()).get(resultingItem.getStartDate());
                if (resultingInvoiceItems.size() > 1) {
                    throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED Multiple FIXED items for subscriptionId='%s', startDate='%s', resultingItems=%s", resultingItem.getSubscriptionId(), resultingItem.getStartDate(), resultingInvoiceItems));
                }
            } else if (resultingItem.getInvoiceItemType() == InvoiceItemType.RECURRING) {
                if (recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()) == null) {
                    recurringItemsPerServicePeriodAndSubscription.put(resultingItem.getSubscriptionId(), LinkedListMultimap.<Range<LocalDate>, InvoiceItem>create());
                }
                final Range<LocalDate> interval = Range.<LocalDate>closedOpen(resultingItem.getStartDate(), resultingItem.getEndDate());
                recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()).put(interval, resultingItem);
                final Collection<InvoiceItem> resultingInvoiceItems = recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()).get(interval);
                if (resultingInvoiceItems.size() > 1) {
                    throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED Multiple RECURRING items for subscriptionId='%s', startDate='%s', endDate='%s', resultingItems=%s", resultingItem.getSubscriptionId(), resultingItem.getStartDate(), resultingItem.getEndDate(), resultingInvoiceItems));
                }
            }
        }
    }
    // Trigger an exception if we create too many invoice items for a subscription on a given day
    if (config.getMaxDailyNumberOfItemsSafetyBound(internalCallContext) == -1) {
        // Safety bound disabled
        return;
    }
    for (final InvoiceItem invoiceItem : resultingItems) {
        if (invoiceItem.getSubscriptionId() != null) {
            final LocalDate resultingItemCreationDay = trackInvoiceItemCreatedDay(invoiceItem, createdItemsPerDayPerSubscription, internalCallContext);
            final Collection<LocalDate> creationDaysForSubscription = createdItemsPerDayPerSubscription.get(invoiceItem.getSubscriptionId());
            int i = 0;
            for (final LocalDate creationDayForSubscription : creationDaysForSubscription) {
                if (creationDayForSubscription.compareTo(resultingItemCreationDay) == 0) {
                    i++;
                    if (i > config.getMaxDailyNumberOfItemsSafetyBound(internalCallContext)) {
                        // Proposed items have already been logged
                        throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED subscriptionId='%s', resultingItem=%s", invoiceItem.getSubscriptionId(), invoiceItem));
                    }
                }
            }
        }
    }
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) HashMap(java.util.HashMap) Range(com.google.common.collect.Range) LocalDate(org.joda.time.LocalDate) Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Collection(java.util.Collection) UUID(java.util.UUID) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

LinkedListMultimap (com.google.common.collect.LinkedListMultimap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Multimap (com.google.common.collect.Multimap)1 Range (com.google.common.collect.Range)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 LocalDate (org.joda.time.LocalDate)1 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)1 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)1 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)1 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)1