Use Map::computeIfAbsent to simplify scope implementations

Closes gh-25038
This commit is contained in:
Yanming Zhou 2020-05-10 21:41:00 +08:00 committed by GitHub
parent f5d011cb82
commit 50a4fdac6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 12 deletions

View File

@ -67,12 +67,7 @@ public class SimpleThreadScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> scope = this.threadScope.get();
Object scopedObject = scope.get(name);
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
scope.put(name, scopedObject);
}
return scopedObject;
return scope.computeIfAbsent(name, k -> objectFactory.getObject());
}
@Override

View File

@ -50,12 +50,7 @@ public class SimpleTransactionScope implements Scope {
TransactionSynchronizationManager.registerSynchronization(new CleanupSynchronization(scopedObjects));
TransactionSynchronizationManager.bindResource(this, scopedObjects);
}
Object scopedObject = scopedObjects.scopedInstances.get(name);
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
scopedObjects.scopedInstances.put(name, scopedObject);
}
return scopedObject;
return scopedObjects.scopedInstances.computeIfAbsent(name, k -> objectFactory.getObject());
}
@Override