Source:
<code>List<ContiguousIntervalUsageInArrear> computeInArrearUsageInterval() { final List<ContiguousIntervalUsageInArrear> usageIntervals = Lists.newLinkedList(); final Map<String, ContiguousIntervalUsageInArrear> inFlightInArrearUsageIntervals = new HashMap<String, ContiguousIntervalUsageInArrear>(); final Set<String> allSeenUsage = new HashSet<String>(); for (final BillingEvent event : subscriptionBillingEvents) { final List<Usage> usages = findUsageInArrearUsages(event); allSeenUsage.addAll(Collections2.transform(usages, new Function<Usage, String>() { @Override public String apply(final Usage input) { return input.getName(); } })); final Set<String> toBeClosed = new HashSet<String>(allSeenUsage); for (final Usage usage : usages) { ContiguousIntervalUsageInArrear existingInterval = inFlightInArrearUsageIntervals.get(usage.getName()); if (existingInterval == null) { <START> existingInterval = new ContiguousIntervalUsageInArrear(usage, accountId, invoiceId, rawSubscriptionUsage, targetDate, rawUsageStartDate, internalTenantContext, invoiceConfig); <END> inFlightInArrearUsageIntervals.put(usage.getName(), existingInterval); } existingInterval.addBillingEvent(event); toBeClosed.remove(usage.getName()); } for (final String usageName : toBeClosed) { final ContiguousIntervalUsageInArrear interval = inFlightInArrearUsageIntervals.remove(usageName); if (interval != null) { interval.addBillingEvent(event); usageIntervals.add(interval.build(true)); } } } for (final String usageName : inFlightInArrearUsageIntervals.keySet()) { usageIntervals.add(inFlightInArrearUsageIntervals.get(usageName).build(false)); } inFlightInArrearUsageIntervals.clear(); return usageIntervals; }</code><technical_language>I pass UsageDetailMode instead of InvoiceConfig here, change (and is for SubscriptionUsageInArrear ) invoice generation (since invoiceConfig.getItemResultBehaviorMode(internalTenantContext) fetches latest configured is updatable runtime, in a situation invoice is generated is updated)</technical_language>

Target:
List<ContiguousIntervalUsageInArrear> computeInArrearUsageInterval() { final List<ContiguousIntervalUsageInArrear> usageIntervals = Lists.newLinkedList(); final Map<String, ContiguousIntervalUsageInArrear> inFlightInArrearUsageIntervals = new HashMap<String, ContiguousIntervalUsageInArrear>(); final Set<String> allSeenUsage = new HashSet<String>(); for (final BillingEvent event : subscriptionBillingEvents) { final List<Usage> usages = findUsageInArrearUsages(event); allSeenUsage.addAll(Collections2.transform(usages, new Function<Usage, String>() { @Override public String apply(final Usage input) { return input.getName(); } })); final Set<String> toBeClosed = new HashSet<String>(allSeenUsage); for (final Usage usage : usages) { ContiguousIntervalUsageInArrear existingInterval = inFlightInArrearUsageIntervals.get(usage.getName()); if (existingInterval == null) { existingInterval = new ContiguousIntervalUsageInArrear(usage, accountId, invoiceId, rawSubscriptionUsage, targetDate, rawUsageStartDate, usageDetailMode, internalTenantContext); inFlightInArrearUsageIntervals.put(usage.getName(), existingInterval); } existingInterval.addBillingEvent(event); toBeClosed.remove(usage.getName()); } for (final String usageName : toBeClosed) { final ContiguousIntervalUsageInArrear interval = inFlightInArrearUsageIntervals.remove(usageName); if (interval != null) { interval.addBillingEvent(event); usageIntervals.add(interval.build(true)); } } } for (final String usageName : inFlightInArrearUsageIntervals.keySet()) { usageIntervals.add(inFlightInArrearUsageIntervals.get(usageName).build(false)); } inFlightInArrearUsageIntervals.clear(); return usageIntervals; }