Skip to content

Commit

Permalink
#1306 fix usage of CopyOnWriteMap in FakeValuesService
Browse files Browse the repository at this point in the history
Instead of two separate calls `putIfAbsent` and `get`, now we use `computeIfAbsent`.
Before this change, `get` sometimes returned null (apparently, because of GC or parallel threads or something similar).
  • Loading branch information
asolntsev committed Jul 28, 2024
1 parent 43e1fff commit 8a52f03
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,10 @@ private MethodAndCoercedArgs retrieveMethodAccessor(Object object, String method
return accessorMap.get(args);
}
final MethodAndCoercedArgs accessor = accessor(clazz, methodName, args);
final Map<String, Map<String[], MethodAndCoercedArgs>> stringMapMap =
MAP_OF_METHOD_AND_COERCED_ARGS.computeIfAbsent(clazz, t -> new CopyOnWriteMap<>(WeakHashMap::new));
stringMapMap.putIfAbsent(methodName, new CopyOnWriteMap<>(WeakHashMap::new));
stringMapMap.get(methodName).putIfAbsent(args, accessor);
MAP_OF_METHOD_AND_COERCED_ARGS
.computeIfAbsent(clazz, cl -> new CopyOnWriteMap<>(WeakHashMap::new))
.computeIfAbsent(methodName, mn -> new CopyOnWriteMap<>(WeakHashMap::new))
.putIfAbsent(args, accessor);
if (accessor == null) {
LOG.fine("Can't find method on "
+ object.getClass().getSimpleName()
Expand Down

0 comments on commit 8a52f03

Please sign in to comment.