You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2021/04/10 16:10:59 UTC
[sling-org-apache-sling-api] 01/01: SLING-8759 use computeIfAbsent
to reduce code size
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch feature/SLING-8759
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git
commit 92516dfaaf11fac4ee9ac66fca71cceaf63d6903
Author: Joerg Hoh <jh...@adobe.com>
AuthorDate: Sat Apr 10 17:58:56 2021 +0200
SLING-8759 use computeIfAbsent to reduce code size
---
.../org/apache/sling/api/adapter/SlingAdaptable.java | 18 ++++--------------
1 file changed, 4 insertions(+), 14 deletions(-)
diff --git a/src/main/java/org/apache/sling/api/adapter/SlingAdaptable.java b/src/main/java/org/apache/sling/api/adapter/SlingAdaptable.java
index 5adf0ce..fa3c5b4 100644
--- a/src/main/java/org/apache/sling/api/adapter/SlingAdaptable.java
+++ b/src/main/java/org/apache/sling/api/adapter/SlingAdaptable.java
@@ -94,22 +94,12 @@ public abstract class SlingAdaptable implements Adaptable {
*/
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
- AdapterType result = null;
synchronized ( this ) {
- if ( adaptersCache != null ) {
- result = (AdapterType) adaptersCache.get(type);
- }
- if ( result == null ) {
- final AdapterManager mgr = ADAPTER_MANAGER;
- result = (mgr == null ? null : mgr.getAdapter(this, type));
- if ( result != null ) {
- if ( adaptersCache == null ) {
- adaptersCache = new HashMap<Class<?>, Object>();
- }
- adaptersCache.put(type, result);
- }
+ if (adaptersCache == null) {
+ adaptersCache = new HashMap<Class<?>, Object>();
}
+ return (AdapterType) adaptersCache.computeIfAbsent(type,
+ (klazz) -> ADAPTER_MANAGER.getAdapter(this, type));
}
- return result;
}
}