Source:
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, internalTenantContext, invoiceConfig); 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; }

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