Skip to content

Commit

Permalink
#1306 simplify usage of MAP_OF_METHOD_AND_COERCED_ARGS in FakeValuesS…
Browse files Browse the repository at this point in the history
…ervice

Instead of three separate calls `containsKey`, `get` and `put`, now we use a single `computeIfAbsent`.
  • Loading branch information
asolntsev committed Jul 28, 2024
1 parent 8a52f03 commit 1e783ca
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,25 +989,17 @@ private Supplier<Object> resolveFakerObjectAndMethod(ProviderRegistration faker,

private MethodAndCoercedArgs retrieveMethodAccessor(Object object, String methodName, String[] args) {
Class<?> clazz = object.getClass();
Map<String[], MethodAndCoercedArgs> accessorMap =
MAP_OF_METHOD_AND_COERCED_ARGS
.getOrDefault(clazz, Collections.emptyMap())
.getOrDefault(methodName, Collections.emptyMap());
// value could be null
if (accessorMap.containsKey(args)) {
return accessorMap.get(args);
}
final MethodAndCoercedArgs accessor = accessor(clazz, methodName, args);
MAP_OF_METHOD_AND_COERCED_ARGS

return 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()
+ " called " + methodName + ".");
}
return accessor;
.computeIfAbsent(args, a -> {
final MethodAndCoercedArgs accessor = accessor(clazz, methodName, args);
if (accessor == null) {
LOG.fine(() -> "Can't find method on %s called %s.".formatted(object.getClass().getSimpleName(), methodName));
}
return accessor;
});
}

private Object invokeAndToString(MethodAndCoercedArgs accessor, Object objectWithMethodToInvoke) {
Expand Down

0 comments on commit 1e783ca

Please sign in to comment.