fix concurrency exception updating capabilities #1473

This commit is contained in:
woodser 2024-12-16 05:59:43 -05:00
parent 4b7db9a1ae
commit ece3b0fec0

View file

@ -59,10 +59,12 @@ public class Capabilities {
}
public Capabilities(Collection<Capability> capabilities) {
synchronized (capabilities) {
synchronized (this.capabilities) {
this.capabilities.addAll(capabilities);
}
}
}
public void set(Capability... capabilities) {
set(Arrays.asList(capabilities));
@ -73,11 +75,13 @@ public class Capabilities {
}
public void set(Collection<Capability> capabilities) {
synchronized (capabilities) {
synchronized (this.capabilities) {
this.capabilities.clear();
this.capabilities.addAll(capabilities);
}
}
}
public void addAll(Capability... capabilities) {
synchronized (this.capabilities) {
@ -87,17 +91,21 @@ public class Capabilities {
public void addAll(Capabilities capabilities) {
if (capabilities != null) {
synchronized (capabilities.capabilities) {
synchronized (this.capabilities) {
this.capabilities.addAll(capabilities.capabilities);
}
}
}
}
public boolean containsAll(final Set<Capability> requiredItems) {
synchronized(requiredItems) {
synchronized (this.capabilities) {
return capabilities.containsAll(requiredItems);
}
}
}
public boolean containsAll(final Capabilities capabilities) {
return containsAll(capabilities.capabilities);
@ -129,8 +137,10 @@ public class Capabilities {
* @return int list of Capability ordinals
*/
public static List<Integer> toIntList(Capabilities capabilities) {
synchronized (capabilities.capabilities) {
return capabilities.capabilities.stream().map(Enum::ordinal).sorted().collect(Collectors.toList());
}
}
/**
* helper for protobuffer stuff
@ -139,12 +149,14 @@ public class Capabilities {
* @return a {@link Capabilities} object
*/
public static Capabilities fromIntList(List<Integer> capabilities) {
synchronized (capabilities) {
return new Capabilities(capabilities.stream()
.filter(integer -> integer < Capability.values().length)
.filter(integer -> integer >= 0)
.map(integer -> Capability.values()[integer])
.collect(Collectors.toSet()));
}
}
/**
*
@ -181,8 +193,10 @@ public class Capabilities {
}
public static boolean hasMandatoryCapability(Capabilities capabilities, Capability mandatoryCapability) {
synchronized (capabilities.capabilities) {
return capabilities.capabilities.stream().anyMatch(c -> c == mandatoryCapability);
}
}
@Override
public String toString() {
@ -211,8 +225,10 @@ public class Capabilities {
// Neither would support removal of past capabilities, a use case we never had so far and which might have
// backward compatibility issues, so we should treat capabilities as an append-only data structure.
public int findHighestCapability(Capabilities capabilities) {
synchronized (capabilities.capabilities) {
return (int) capabilities.capabilities.stream()
.mapToLong(e -> (long) e.ordinal())
.sum();
}
}
}