private List<UsageStatistic> retrieveApplicationUsage(ApplicationUsage usage, Date startDate, Date endDate) {
log.debug("Determining application usage for application {}: dates {} -> {}", new Object[] {usage.getApplicationId(), startDate, endDate});
log.trace("Considering application usage events of {}: {}", usage.getApplicationId(), usage.getEvents());
for (int i = 0; i < usage.getEvents().size(); i++) {
ApplicationEvent current = usage.getEvents().get(i);
Date eventStartDate = current.getDate();
Date eventEndDate;
if (i < usage.getEvents().size() - 1) {
ApplicationEvent next = usage.getEvents().get(i + 1);
eventEndDate = next.getDate();
} else if (current.getState() == Lifecycle.DESTROYED) {
UsageStatistic statistic = new UsageStatistic(ApplicationTransformer.statusFromLifecycle(current.getState()), usage.getApplicationId(), usage.getApplicationId(), format(eventStartDate), format(eventEndDate), duration, usage.getMetadata());
log.trace("Adding application usage statistic to response for app {}: {}", usage.getApplicationId(), statistic);
result.add(statistic);