Synchronize access
@@ -25,12 +25,15 @@ public class RankingConstants {
private final Map<String, RankingConstant> constants = new LinkedHashMap<>();
+ private final Object mutex = new Object();
+
public RankingConstants(FileRegistry fileRegistry, Optional<Schema> owner) {
this.fileRegistry = fileRegistry;
this.owner = owner;
}
public void add(RankingConstant constant) {
+ synchronized (mutex) {
constant.validate();
constant.register(fileRegistry);
String name = constant.getName();
@@ -38,15 +41,19 @@ public class RankingConstants {
if (prev != null)
throw new IllegalArgumentException("Constant '" + name + "' defined twice");
}
+ }
public void putIfAbsent(RankingConstant constant) {
+ synchronized (mutex) {
constant.validate();
constant.register(fileRegistry);
String name = constant.getName();
constants.putIfAbsent(name, constant);
}
+ }
public void computeIfAbsent(String name, Function<? super String, ? extends RankingConstant> createConstant) {
+ synchronized (mutex) {
constants.computeIfAbsent(name, key -> {
RankingConstant constant = createConstant.apply(key);
constant.validate();
@@ -54,18 +61,22 @@ public class RankingConstants {
return constant;
});
}
+ }
/** Returns the ranking constant with the given name, or null if not present */
public RankingConstant get(String name) {
+ synchronized (mutex) {
var constant = constants.get(name);
if (constant != null) return constant;
if (owner.isPresent() && owner.get().inherited().isPresent())
return owner.get().inherited().get().rankingConstants().get(name);
return null;
}
+ }
/** Returns a read-only map of the ranking constants in this indexed by name */
public Map<String, RankingConstant> asMap() {
+ synchronized (mutex) {
// Shortcuts
if (owner.isEmpty() || owner.get().inherited().isEmpty()) return Collections.unmodifiableMap(constants);
if (constants.isEmpty()) return owner.get().inherited().get().rankingConstants().asMap();
@@ -74,5 +85,6 @@ public class RankingConstants {
allConstants.putAll(constants);
return Collections.unmodifiableMap(allConstants);
}
+ }
}
