mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
generic sorted message tree
This commit is contained in:
89
briar-core/src/org/briarproject/clients/MessageTreeImpl.java
Normal file
89
briar-core/src/org/briarproject/clients/MessageTreeImpl.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package org.briarproject.clients;
|
||||
|
||||
import org.briarproject.api.clients.MessageTree;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
||||
implements MessageTree<T> {
|
||||
|
||||
Map<MessageId, List<T>> nodeMap = new HashMap<MessageId, List<T>>();
|
||||
List<T> roots = new ArrayList<T>();
|
||||
|
||||
private Comparator<T> comparator = new Comparator<T>() {
|
||||
@Override
|
||||
public int compare(T o1, T o2) {
|
||||
return Long.valueOf(o1.getTimestamp()).compareTo(o2.getTimestamp());
|
||||
}
|
||||
};
|
||||
|
||||
@Inject
|
||||
public MessageTreeImpl() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
roots.clear();
|
||||
nodeMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Collection<T> nodes) {
|
||||
// add all nodes to the node map
|
||||
for (T node : nodes) {
|
||||
nodeMap.put(node.getId(), new ArrayList<T>());
|
||||
}
|
||||
// parse the nodes for dependencies
|
||||
for (T node : nodes) {
|
||||
if (node.getParentId() == null) {
|
||||
roots.add(node);
|
||||
}
|
||||
else {
|
||||
// retrieve the parent's children
|
||||
List<T> pChildren = nodeMap.get(node.getParentId());
|
||||
pChildren.add(node);
|
||||
}
|
||||
}
|
||||
sortAll();
|
||||
}
|
||||
|
||||
private void sortAll() {
|
||||
Collections.sort(roots, comparator);
|
||||
// Sort all the sub-lists
|
||||
for (Map.Entry<MessageId, List<T>> entry: nodeMap.entrySet()) {
|
||||
Collections.sort(entry.getValue(), comparator);
|
||||
}
|
||||
}
|
||||
|
||||
private void traverse(List<T> list, T node) {
|
||||
list.add(node);
|
||||
for (T child : nodeMap.get(node.getId())) {
|
||||
traverse(list, child);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<T> depthFirstOrder() {
|
||||
List<T> orderedList = new ArrayList<T>();
|
||||
for (T root : roots) {
|
||||
traverse(orderedList, root);
|
||||
}
|
||||
return Collections.unmodifiableList(orderedList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComparator(Comparator<T> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.forum;
|
||||
|
||||
import org.briarproject.api.clients.ClientHelper;
|
||||
import org.briarproject.api.clients.MessageQueueManager;
|
||||
import org.briarproject.api.clients.MessageTree;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.data.MetadataEncoder;
|
||||
@@ -9,12 +10,14 @@ import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.forum.ForumFactory;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.api.forum.ForumPostFactory;
|
||||
import org.briarproject.api.forum.ForumPostHeader;
|
||||
import org.briarproject.api.forum.ForumSharingManager;
|
||||
import org.briarproject.api.identity.AuthorFactory;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.sync.GroupFactory;
|
||||
import org.briarproject.api.sync.ValidationManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.clients.MessageTreeImpl;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
@@ -100,4 +103,11 @@ public class ForumModule {
|
||||
|
||||
return forumSharingManager;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
MessageTree<ForumPostHeader> provideForumMessageTree(
|
||||
MessageTreeImpl<ForumPostHeader> messageTree) {
|
||||
return messageTree;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user