for (ActivitySlice slice : slices) {
// Get activity identifier
Key<Activity> activityKey = slice.getActivity();
Long activityId = activityKey.getId();
// Get full activity
Activity fullActivity = fullActivities.get(activityKey);
if (fullActivity == null)
continue;
List<Activity> activities;
Activity activity;
if (map.containsKey(activityId)) {
// Get the current list of activities for this key
activities = map.get(activityId);
// Try to use the last activity
activity = activities.get(activities.size() - 1);
} else {
// Create a new list of activities for this key
activities = new ArrayList<Activity>();
// Clone this activity
activity = (Activity) fullActivity.clone();
// Make it start at the same date as the slice
// but with a zero duration
activity.setStart(slice.getDate());
activity.setEnd(slice.getDate());
// Store in list and map
activities.add(activity);
map.put(activityId, activities);
}
// Try to append the slice
boolean isAppended = activity.addSlice(slice);
if (!isAppended) {
// The slice could not be added to the activity
// Clone the activity and make it match
// the time span of the slice
activity = (Activity) activity.clone();
activity.setStart(slice.getDate());
activity.setDuration(slice.getDuration());
activities.add(activity);
}
}
// Unite all activity lists into one flat list