First if you care about dates only (don't care about times), it's better to use LocalDate
instead. Second, I assume that you have a task constructor. So I used the following Task
object:
static class Task {
LocalDate start;
LocalDate end;
Set<String> actionItems;
public Task(LocalDate start, LocalDate end,
Collection<String> actionItems) {
this.start = start;
this.end = end;
this.actionItems = new HashSet<>(actionItems);
}
@Override
public String toString() {
return start + ".." + end + ": "+actionItems;
}
}
Here's the solution of more general task which just merges all the tasks in given collection according to your rules (the input collection is not necessarily sorted):
public static List<Task> convert(Collection<Task> input) {
NavigableMap<LocalDate, Set<String>> map = new TreeMap<>();
map.put(LocalDate.MIN, new HashSet<>());
for (Task task : input) {
if (!map.containsKey(task.start)) {
map.put(task.start, new HashSet<>(map.lowerEntry(task.start).getValue()));
}
if (!map.containsKey(task.end)) {
map.put(task.end, new HashSet<>(map.lowerEntry(task.end).getValue()));
}
for (Set<String> set : map.subMap(task.start, task.end).values()) {
set.addAll(task.actionItems);
}
}
List<Task> result = new ArrayList<>();
LocalDate prev = null;
Set<String> prevValues = Collections.emptySet();
for (Entry<LocalDate, Set<String>> entry : map.entrySet()) {
if (!prevValues.isEmpty()) {
result.add(new Task(prev, entry.getKey(), prevValues));
}
prev = entry.getKey();
prevValues = entry.getValue();
}
return result;
}
The core thing is the NavigableMap
where each key is the start of the next time period and the value is the collection of actions for the period from given start until the next key (empty values correspond to the periods without actions). Upon adding the new task existing entries are updated accordingly. Usage example:
List<Task> res = convert(Arrays.asList(
new Task(LocalDate.parse("2015-01-01"), LocalDate.parse("2015-01-31"),
Arrays.asList("A", "B")),
new Task(LocalDate.parse("2014-01-01"), LocalDate.parse("2014-01-31"),
Arrays.asList("A", "B")),
new Task(LocalDate.parse("2015-01-15"), LocalDate.parse("2015-02-15"),
Arrays.asList("C"))));
res.stream().forEach(System.out::println);
Output:
2014-01-01..2014-01-31: [A, B]
2015-01-01..2015-01-15: [A, B]
2015-01-15..2015-01-31: [A, B, C]
2015-01-31..2015-02-15: [C]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…