Construct a new message tree from an iterator of MessageSummary
objects.
while (messages.hasNext()) {
// store each message in a map for fast retrieval by ID
MessageSummary curMsg = (MessageSummary) messages.next();
this.idToMsgMap.put(new Long(curMsg.getID()), curMsg);
// build the inverted map that maps reply-to IDs to
// lists of messages
Long curReplyID = new Long(curMsg.getInReplyTo());
List replyToList =
(List) this.replyIDToMsgListMap.get(curReplyID);
if (replyToList == null) {
replyToList = new ArrayList();
this.replyIDToMsgListMap.put(curReplyID, replyToList);
}
replyToList.add(curMsg);
}
// build the list of top-level messages. A top-level message
// fits one of the following two criteria:
// - its reply-to ID is -1
// - its reply-to ID was not found in the list of messages. This
// occurs when a message is a reply to a previous month's message
Iterator iter = this.replyIDToMsgListMap.keySet().iterator();
while (iter.hasNext()) {
Long curReplyToID = (Long) iter.next();
if (curReplyToID.longValue() == -1
|| !this.idToMsgMap.containsKey(curReplyToID)) {
List msgsToAdd =
(List) this.replyIDToMsgListMap.get(curReplyToID);
this.topLevelMsgs.addAll(msgsToAdd);
}
}
Collections.sort(this.topLevelMsgs);