I have a Task class which looks like the following (using Java 8 Time API).
class Task {
LocalDateTime start;
LocalDateTime end;
Set<String> actionItems;
}
I have two sorted (first by start, then by end) lists containing such Task instances, lets say List<Task> tasksList1
and List<Task> tasksList2
. I want to combine overlapping tasks (by breaking the tasks if needed, and adding actionItems from other tasks which are overlapping into a single new task object).
For example, assume I have a task called T1 that starts on 01/01/2015 and ends on 01/31/2015, which contains action items A and B. Then a user creates a new Task T2 that starts on 01/15/2015 and ends on 02/15/2015 and adds action item C into it. When I combine, I should get three Task objects as follows.
- Task X - from 01/01/2015 to 01/15/2015, contains action items A, B
- Task Y - from 01/15/2015 to 01/31/2015, contains items A,B and C
- Task Z - from 01/31/2015 to 02/15/2015, contains item C
To visualize, if my task object from the two lists look like the following in a timeline:
> [-----] [-----] [----] [-----------------]
> [-----] [---------------] [------]
Then the resulting task list would contain tasks as follows.
> [--][-][--] [-----] [-----][----][--] [-][------][-----]`
Overlapping tasks should have the actionItems combined from both of the tasks that overlap for the period in which they overlap.
What is the most efficient way to handle this? At the moment I'm trying out different options with a PeekableIterator, but no luck yet. Any solutions using JodaTime instead of Java 8 APIs is also welcome.
See Question&Answers more detail:os