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