mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Ensure MessageTreeImpl#contains() is thread-safe.
This commit is contained in:
@@ -12,6 +12,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
@@ -19,10 +20,16 @@ import javax.annotation.concurrent.ThreadSafe;
|
|||||||
public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
||||||
implements MessageTree<T> {
|
implements MessageTree<T> {
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private final Map<MessageId, List<T>> nodeMap = new HashMap<>();
|
private final Map<MessageId, List<T>> nodeMap = new HashMap<>();
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private final List<T> roots = new ArrayList<>();
|
private final List<T> roots = new ArrayList<>();
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private final List<List<T>> unsortedLists = new ArrayList<>();
|
private final List<List<T>> unsortedLists = new ArrayList<>();
|
||||||
|
|
||||||
|
@SuppressWarnings("UseCompareMethod")
|
||||||
private Comparator<T> comparator = (o1, o2) ->
|
private Comparator<T> comparator = (o1, o2) ->
|
||||||
Long.valueOf(o1.getTimestamp()).compareTo(o2.getTimestamp());
|
Long.valueOf(o1.getTimestamp()).compareTo(o2.getTimestamp());
|
||||||
|
|
||||||
@@ -50,11 +57,13 @@ public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
|||||||
add(Collections.singletonList(node));
|
add(Collections.singletonList(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private void markAsUnsorted(List<T> list) {
|
private void markAsUnsorted(List<T> list) {
|
||||||
if (!unsortedLists.contains(list))
|
if (!unsortedLists.contains(list))
|
||||||
unsortedLists.add(list);
|
unsortedLists.add(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private void parseNode(T node) {
|
private void parseNode(T node) {
|
||||||
if (node.getParentId() == null) {
|
if (node.getParentId() == null) {
|
||||||
roots.add(node);
|
roots.add(node);
|
||||||
@@ -67,6 +76,7 @@ public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private void sortUnsorted() {
|
private void sortUnsorted() {
|
||||||
for (List<T> list : unsortedLists) {
|
for (List<T> list : unsortedLists) {
|
||||||
Collections.sort(list, comparator);
|
Collections.sort(list, comparator);
|
||||||
@@ -74,6 +84,7 @@ public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
|||||||
unsortedLists.clear();
|
unsortedLists.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
private void traverse(List<T> list, T node, int level) {
|
private void traverse(List<T> list, T node, int level) {
|
||||||
list.add(node);
|
list.add(node);
|
||||||
List<T> children = nodeMap.get(node.getId());
|
List<T> children = nodeMap.get(node.getId());
|
||||||
@@ -103,7 +114,7 @@ public class MessageTreeImpl<T extends MessageTree.MessageNode>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(MessageId m) {
|
public synchronized boolean contains(MessageId m) {
|
||||||
return nodeMap.containsKey(m);
|
return nodeMap.containsKey(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user