From 8b58b5b23588c73eaaf73a81a2823e6579072441 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 18 Nov 2023 14:59:57 -0500 Subject: [PATCH] make Capabilities thread safe to fix concurrent modification exception --- .../java/haveno/common/app/Capabilities.java | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/haveno/common/app/Capabilities.java b/common/src/main/java/haveno/common/app/Capabilities.java index 48238cebe6..80289f44e3 100644 --- a/common/src/main/java/haveno/common/app/Capabilities.java +++ b/common/src/main/java/haveno/common/app/Capabilities.java @@ -59,7 +59,9 @@ public class Capabilities { } public Capabilities(Collection capabilities) { - this.capabilities.addAll(capabilities); + synchronized (this.capabilities) { + this.capabilities.addAll(capabilities); + } } public void set(Capability... capabilities) { @@ -71,21 +73,30 @@ public class Capabilities { } public void set(Collection capabilities) { - this.capabilities.clear(); - this.capabilities.addAll(capabilities); + synchronized (this.capabilities) { + this.capabilities.clear(); + this.capabilities.addAll(capabilities); + } } public void addAll(Capability... capabilities) { - this.capabilities.addAll(Arrays.asList(capabilities)); + synchronized (this.capabilities) { + this.capabilities.addAll(Arrays.asList(capabilities)); + } } public void addAll(Capabilities capabilities) { - if (capabilities != null) - this.capabilities.addAll(capabilities.capabilities); + if (capabilities != null) { + synchronized (this.capabilities) { + this.capabilities.addAll(capabilities.capabilities); + } + } } public boolean containsAll(final Set requiredItems) { - return capabilities.containsAll(requiredItems); + synchronized (this.capabilities) { + return capabilities.containsAll(requiredItems); + } } public boolean containsAll(final Capabilities capabilities) { @@ -93,15 +104,21 @@ public class Capabilities { } public boolean containsAll(Capability... capabilities) { - return this.capabilities.containsAll(Arrays.asList(capabilities)); + synchronized (this.capabilities) { + return this.capabilities.containsAll(Arrays.asList(capabilities)); + } } public boolean contains(Capability capability) { - return this.capabilities.contains(capability); + synchronized (this.capabilities) { + return this.capabilities.contains(capability); + } } public boolean isEmpty() { - return capabilities.isEmpty(); + synchronized (this.capabilities) { + return capabilities.isEmpty(); + } } @@ -173,10 +190,12 @@ public class Capabilities { } public String prettyPrint() { - return capabilities.stream() - .sorted(Comparator.comparingInt(Enum::ordinal)) - .map(e -> e.name() + " [" + e.ordinal() + "]") - .collect(Collectors.joining(", ")); + synchronized (capabilities) { + return capabilities.stream() + .sorted(Comparator.comparingInt(Enum::ordinal)) + .map(e -> e.name() + " [" + e.ordinal() + "]") + .collect(Collectors.joining(", ")); + } } public int size() {