make Capabilities thread safe to fix concurrent modification exception

This commit is contained in:
woodser 2023-11-18 14:59:57 -05:00
parent 5fc67ec65a
commit 8b58b5b235

View file

@ -59,7 +59,9 @@ public class Capabilities {
} }
public Capabilities(Collection<Capability> capabilities) { public Capabilities(Collection<Capability> capabilities) {
this.capabilities.addAll(capabilities); synchronized (this.capabilities) {
this.capabilities.addAll(capabilities);
}
} }
public void set(Capability... capabilities) { public void set(Capability... capabilities) {
@ -71,21 +73,30 @@ public class Capabilities {
} }
public void set(Collection<Capability> capabilities) { public void set(Collection<Capability> capabilities) {
this.capabilities.clear(); synchronized (this.capabilities) {
this.capabilities.addAll(capabilities); this.capabilities.clear();
this.capabilities.addAll(capabilities);
}
} }
public void addAll(Capability... 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) { public void addAll(Capabilities capabilities) {
if (capabilities != null) if (capabilities != null) {
this.capabilities.addAll(capabilities.capabilities); synchronized (this.capabilities) {
this.capabilities.addAll(capabilities.capabilities);
}
}
} }
public boolean containsAll(final Set<Capability> requiredItems) { public boolean containsAll(final Set<Capability> requiredItems) {
return capabilities.containsAll(requiredItems); synchronized (this.capabilities) {
return capabilities.containsAll(requiredItems);
}
} }
public boolean containsAll(final Capabilities capabilities) { public boolean containsAll(final Capabilities capabilities) {
@ -93,15 +104,21 @@ public class Capabilities {
} }
public boolean containsAll(Capability... 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) { public boolean contains(Capability capability) {
return this.capabilities.contains(capability); synchronized (this.capabilities) {
return this.capabilities.contains(capability);
}
} }
public boolean isEmpty() { public boolean isEmpty() {
return capabilities.isEmpty(); synchronized (this.capabilities) {
return capabilities.isEmpty();
}
} }
@ -173,10 +190,12 @@ public class Capabilities {
} }
public String prettyPrint() { public String prettyPrint() {
return capabilities.stream() synchronized (capabilities) {
.sorted(Comparator.comparingInt(Enum::ordinal)) return capabilities.stream()
.map(e -> e.name() + " [" + e.ordinal() + "]") .sorted(Comparator.comparingInt(Enum::ordinal))
.collect(Collectors.joining(", ")); .map(e -> e.name() + " [" + e.ordinal() + "]")
.collect(Collectors.joining(", "));
}
} }
public int size() { public int size() {