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,8 +59,10 @@ public class Capabilities {
} }
public Capabilities(Collection<Capability> capabilities) { public Capabilities(Collection<Capability> capabilities) {
synchronized (this.capabilities) { synchronized (capabilities) {
this.capabilities.addAll(capabilities); synchronized (this.capabilities) {
this.capabilities.addAll(capabilities);
}
} }
} }
@ -73,9 +75,11 @@ public class Capabilities {
} }
public void set(Collection<Capability> capabilities) { public void set(Collection<Capability> capabilities) {
synchronized (this.capabilities) { synchronized (capabilities) {
this.capabilities.clear(); synchronized (this.capabilities) {
this.capabilities.addAll(capabilities); this.capabilities.clear();
this.capabilities.addAll(capabilities);
}
} }
} }
@ -87,15 +91,19 @@ public class Capabilities {
public void addAll(Capabilities capabilities) { public void addAll(Capabilities capabilities) {
if (capabilities != null) { if (capabilities != null) {
synchronized (this.capabilities) { synchronized (capabilities.capabilities) {
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) {
synchronized (this.capabilities) { synchronized(requiredItems) {
return capabilities.containsAll(requiredItems); synchronized (this.capabilities) {
return capabilities.containsAll(requiredItems);
}
} }
} }
@ -129,7 +137,9 @@ public class Capabilities {
* @return int list of Capability ordinals * @return int list of Capability ordinals
*/ */
public static List<Integer> toIntList(Capabilities capabilities) { public static List<Integer> toIntList(Capabilities capabilities) {
return capabilities.capabilities.stream().map(Enum::ordinal).sorted().collect(Collectors.toList()); synchronized (capabilities.capabilities) {
return capabilities.capabilities.stream().map(Enum::ordinal).sorted().collect(Collectors.toList());
}
} }
/** /**
@ -139,11 +149,13 @@ public class Capabilities {
* @return a {@link Capabilities} object * @return a {@link Capabilities} object
*/ */
public static Capabilities fromIntList(List<Integer> capabilities) { public static Capabilities fromIntList(List<Integer> capabilities) {
return new Capabilities(capabilities.stream() synchronized (capabilities) {
.filter(integer -> integer < Capability.values().length) return new Capabilities(capabilities.stream()
.filter(integer -> integer >= 0) .filter(integer -> integer < Capability.values().length)
.map(integer -> Capability.values()[integer]) .filter(integer -> integer >= 0)
.collect(Collectors.toSet())); .map(integer -> Capability.values()[integer])
.collect(Collectors.toSet()));
}
} }
/** /**
@ -181,7 +193,9 @@ public class Capabilities {
} }
public static boolean hasMandatoryCapability(Capabilities capabilities, Capability mandatoryCapability) { public static boolean hasMandatoryCapability(Capabilities capabilities, Capability mandatoryCapability) {
return capabilities.capabilities.stream().anyMatch(c -> c == mandatoryCapability); synchronized (capabilities.capabilities) {
return capabilities.capabilities.stream().anyMatch(c -> c == mandatoryCapability);
}
} }
@Override @Override
@ -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 // 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. // backward compatibility issues, so we should treat capabilities as an append-only data structure.
public int findHighestCapability(Capabilities capabilities) { public int findHighestCapability(Capabilities capabilities) {
return (int) capabilities.capabilities.stream() synchronized (capabilities.capabilities) {
.mapToLong(e -> (long) e.ordinal()) return (int) capabilities.capabilities.stream()
.sum(); .mapToLong(e -> (long) e.ordinal())
.sum();
}
} }
} }