You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2021/06/21 16:45:51 UTC

[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz opened a new pull request #25: SLING-10502 - lazy loading helpers

bdelacretaz opened a new pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] raducotescu commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
raducotescu commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r657778056



##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Contrary to the usual Map contract, this always
+     *  returns null, to avoid calling a supplier "for nothing".
+     *  If the value is needed, call {@link #get} first.
+     */
+    @Override
+    public T remove(Object key) {
+        super.remove(key);
+        suppliers.remove(key);
+        return null;
+    }
+
+    @Override
+    public void clear() {
+        suppliers.clear();
+        super.clear();
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        return super.containsKey(key) || suppliers.containsKey(key);
+    }
+
+    @Override
+    public int size() {
+        return keySet().size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return super.isEmpty() && suppliers.isEmpty();
+    }
+
+    @Override
+    public Set<K> keySet() {
+        final Set<K> result = new HashSet<>();
+        result.addAll(super.keySet());
+        result.addAll(suppliers.keySet());
+        return result;
+    }
+
+    /** Required for some methods that need all our values
+     *  Calling those methods should be avoided if possible
+     */
+    private void computeAll() {
+        if(!suppliers.isEmpty()) {
+            log.info("computeAll called, all remaining lazy values will be evaluated now");

Review comment:
       I think this should be logged on `debug` - I assume that with GraphQL in Sling catching on, we'll have a lot of `LazyLoadingMaps`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865798018


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866701204


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866753756


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865191175


   SonarCloud Quality Gate failed.
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/D.png' alt='D' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.8%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.8% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] jsedding commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
jsedding commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r657995275



##########
File path: src/main/java/org/apache/sling/graphql/helpers/lazyloading/LazyLoadingMap.java
##########
@@ -0,0 +1,197 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.lazyloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private boolean computeValueOnRemove;
+    private boolean forbidComputeAll;
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Unless {@link #computeValueOnRemove(boolean)} is set to
+     *  true, this returns null to avoid calling a supplier
+     * "for nothing".
+     */
+    @Override
+    @SuppressWarnings("squid:S2201")
+    public T remove(Object key) {
+        if(computeValueOnRemove) {
+            get(key);
+        }
+        super.remove(key);
+        suppliers.remove(key);
+        return null;

Review comment:
       @bdelacretaz I think you meant to return `super.remove(key)` here if `computeValueOnRemove` is `true`. How about this?
   
   ```
   Supplier<T> supplier = suppliers.remove(key);
   if (supplier != null) {
       super.remove(key); // this should be unnecessary, otherwise I believe it would be a bug?!
       return computeValueOnRemove ? supplier.get() : null;
   } else {
       return super.remove(key);    
   }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
bdelacretaz commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866680040


   Thank you for the detailed review, all good suggestions! Of course, HashMap not being synchronized it doesn't make sense to make the `LazyLoadingMap` thread safe, I have removed all synchronization in that class.
   
   I think I have implemented all other suggestions, please cross-check.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] raducotescu commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
raducotescu commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r657775764



##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Contrary to the usual Map contract, this always
+     *  returns null, to avoid calling a supplier "for nothing".
+     *  If the value is needed, call {@link #get} first.
+     */
+    @Override
+    public T remove(Object key) {
+        super.remove(key);
+        suppliers.remove(key);

Review comment:
       Would it be too expensive to obey the `Map` contract in this case?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865856572


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-867548856


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] jsedding commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
jsedding commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r658068368



##########
File path: src/main/java/org/apache/sling/graphql/helpers/lazyloading/LazyLoadingMap.java
##########
@@ -0,0 +1,197 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.lazyloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private boolean computeValueOnRemove;
+    private boolean forbidComputeAll;
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Unless {@link #computeValueOnRemove(boolean)} is set to
+     *  true, this returns null to avoid calling a supplier
+     * "for nothing".
+     */
+    @Override
+    @SuppressWarnings("squid:S2201")
+    public T remove(Object key) {
+        if(computeValueOnRemove) {
+            get(key);
+        }
+        super.remove(key);
+        suppliers.remove(key);
+        return null;

Review comment:
       👍 looks good to me




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865204722


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.8%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.8% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
bdelacretaz commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r657814394



##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Contrary to the usual Map contract, this always
+     *  returns null, to avoid calling a supplier "for nothing".
+     *  If the value is needed, call {@link #get} first.
+     */
+    @Override
+    public T remove(Object key) {
+        super.remove(key);
+        suppliers.remove(key);

Review comment:
       Committed that, using class methods to set options instead.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865798018


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
bdelacretaz commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r657790590



##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Contrary to the usual Map contract, this always
+     *  returns null, to avoid calling a supplier "for nothing".
+     *  If the value is needed, call {@link #get} first.
+     */
+    @Override
+    public T remove(Object key) {
+        super.remove(key);
+        suppliers.remove(key);

Review comment:
       It's hard to say - I will make that optional, adding an `Options` object to the class constructor.

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Contrary to the usual Map contract, this always
+     *  returns null, to avoid calling a supplier "for nothing".
+     *  If the value is needed, call {@link #get} first.
+     */
+    @Override
+    public T remove(Object key) {
+        super.remove(key);
+        suppliers.remove(key);
+        return null;
+    }
+
+    @Override
+    public void clear() {
+        suppliers.clear();
+        super.clear();
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        return super.containsKey(key) || suppliers.containsKey(key);
+    }
+
+    @Override
+    public int size() {
+        return keySet().size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return super.isEmpty() && suppliers.isEmpty();
+    }
+
+    @Override
+    public Set<K> keySet() {
+        final Set<K> result = new HashSet<>();
+        result.addAll(super.keySet());
+        result.addAll(suppliers.keySet());
+        return result;
+    }
+
+    /** Required for some methods that need all our values
+     *  Calling those methods should be avoided if possible
+     */
+    private void computeAll() {
+        if(!suppliers.isEmpty()) {
+            log.info("computeAll called, all remaining lazy values will be evaluated now");

Review comment:
       +1 I'll do that




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866701204


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865204722


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.8%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.8% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866683490


   SonarCloud Quality Gate failed.
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/B.png' alt='B' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865191175


   SonarCloud Quality Gate failed.
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/D.png' alt='D' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.8%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.8% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-867518033


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-867518033


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz merged pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
bdelacretaz merged pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866683490


   SonarCloud Quality Gate failed.
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/B.png' alt='B' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] bdelacretaz commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
bdelacretaz commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r658040800



##########
File path: src/main/java/org/apache/sling/graphql/helpers/lazyloading/LazyLoadingMap.java
##########
@@ -0,0 +1,197 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.lazyloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@link java.util.HashMap} that optionally uses Suppliers to provide its
+ *  values. Each Supplier is called at most once, if the corresponding
+ *  value is requested. Some "global" operations requires all values
+ *  to be computed, and should be considered costly.
+ * 
+ *  Like HashMap, this class is NOT thread safe. If needed, 
+ *  {@link java.util.Collections#synchronizedMap} can be used
+ *  to sychronize it.
+  */
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private boolean computeValueOnRemove;
+    private boolean forbidComputeAll;
+
+    public class Stats {
+        int suppliersCallCount;
+
+        public int getSuppliersCallCount() {
+            return suppliersCallCount;
+        }
+
+        public int getUnusedSuppliersCount() {
+            return suppliers.size();
+        }
+    }
+
+    private final Stats stats = new Stats();
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        super.remove(key);
+        return suppliers.put(key, supplier);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T get(Object key) {
+        computeIfAbsent((K)key, k -> {
+            final Supplier<T> s = suppliers.remove(k);
+            if(s != null) {
+                stats.suppliersCallCount++;
+                return s.get();
+            }
+            return null;
+        });
+        return super.get(key);
+    }
+
+    /** Unless {@link #computeValueOnRemove(boolean)} is set to
+     *  true, this returns null to avoid calling a supplier
+     * "for nothing".
+     */
+    @Override
+    @SuppressWarnings("squid:S2201")
+    public T remove(Object key) {
+        if(computeValueOnRemove) {
+            get(key);
+        }
+        super.remove(key);
+        suppliers.remove(key);
+        return null;

Review comment:
       Indeed, _both the tests and the code were wrong_!
   
   Good idea to use the Supplier in this way, I have committed a slightly different fix that sets the Stats and that always returns null unless `computeValueOnRemove` is set - I think that's less confusing. https://github.com/apache/sling-org-apache-sling-graphql-core/commit/4cce0790dea00fb5109606531817f17304f62450
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-866753756


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [2 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='98.7%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [98.7% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865191175


   SonarCloud Quality Gate failed.
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/D.png' alt='D' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.8%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.8% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] removed a comment on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] removed a comment on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865856572


   Kudos, SonarCloud Quality Gate passed!
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=BUG)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=VULNERABILITY)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=SECURITY_HOTSPOT)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL) [3 Code Smells](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&resolved=false&types=CODE_SMELL)
   
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/90.png' alt='97.9%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list) [97.9% Coverage](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_coverage&view=list)  
   [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-graphql-core&pullRequest=25&metric=new_duplicated_lines_density&view=list)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] sonarcloud[bot] commented on pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
sonarcloud[bot] commented on pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#issuecomment-865191175






-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [sling-org-apache-sling-graphql-core] jsedding commented on a change in pull request #25: SLING-10502 - lazy loading helpers

Posted by GitBox <gi...@apache.org>.
jsedding commented on a change in pull request #25:
URL: https://github.com/apache/sling-org-apache-sling-graphql-core/pull/25#discussion_r656099353



##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingField.java
##########
@@ -0,0 +1,44 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.function.Supplier;
+
+/** Helper for a single lazy-loading value */
+public class LazyLoadingField<T> {
+    private Supplier<T> supplier;
+    private T value;
+
+    public LazyLoadingField(Supplier<T> supplier) {
+        this.supplier = supplier;
+    }
+
+    public T get() {
+        if(value == null) {

Review comment:
       AFAIK double-checked locking requires the checked field to be volatile. Why not make supplier volatile, use that one exclusively for checking and get support for "null" values as well? Granted, your current code looks like it can support null values, but in case of a null value, it will always enter the synchronized block even when the computation was already performed. 

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {
+                super.remove(key);
+                return suppliers.put(key, supplier);
+            }
+        } else {
+            return suppliers.put(key, supplier);
+        }
+    }
+
+    @Override
+    public T get(Object key) {
+        return lazyCompute(key);
+    }
+
+    @SuppressWarnings("unchecked")
+    private T lazyCompute(Object key) {
+        if(key == null) {
+            return null;
+        }
+        T value = super.get(key);

Review comment:
       Consider whether `Map#comuteIfAbsent()` would make the code more concise here.

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {
+                super.remove(key);
+                return suppliers.put(key, supplier);
+            }
+        } else {
+            return suppliers.put(key, supplier);
+        }
+    }
+
+    @Override
+    public T get(Object key) {
+        return lazyCompute(key);
+    }
+
+    @SuppressWarnings("unchecked")
+    private T lazyCompute(Object key) {
+        if(key == null) {
+            return null;
+        }
+        T value = super.get(key);
+        if(value == null) {
+            synchronized(this) {
+                if(value == null) {
+                    final Supplier<T> s = suppliers.remove(key);
+                    if(s != null) {
+                        suppliersCallCount++;
+                        value = s.get();
+                        super.put((K)key, value);
+                    }
+                }
+            }
+        }
+        return value;
+    }
+
+
+    /** This indicates how many Supplier calls have been made.
+     *  Can be useful in case of doubt, as several methods need
+     *  to call computeAll().
+     */
+    public int getSuppliersCallCount() {

Review comment:
       Getting the number of `Supplier` still to be called could also be interesting. Maybe provide a `Stats` object with this sort of meta-information?

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {
+                super.remove(key);
+                return suppliers.put(key, supplier);
+            }
+        } else {
+            return suppliers.put(key, supplier);
+        }
+    }
+
+    @Override
+    public T get(Object key) {
+        return lazyCompute(key);
+    }
+
+    @SuppressWarnings("unchecked")
+    private T lazyCompute(Object key) {
+        if(key == null) {
+            return null;
+        }
+        T value = super.get(key);
+        if(value == null) {
+            synchronized(this) {
+                if(value == null) {
+                    final Supplier<T> s = suppliers.remove(key);
+                    if(s != null) {
+                        suppliersCallCount++;
+                        value = s.get();
+                        super.put((K)key, value);
+                    }
+                }
+            }
+        }
+        return value;
+    }
+
+
+    /** This indicates how many Supplier calls have been made.
+     *  Can be useful in case of doubt, as several methods need
+     *  to call computeAll().
+     */
+    public int getSuppliersCallCount() {
+        return suppliersCallCount;
+    }
+
+    @Override
+    public T remove(Object key) {
+        synchronized(this) {
+            lazyCompute(key);

Review comment:
       We could opt to skip computation here. I understand that this would violate the contract regarding the return value. But we could document that `null`is returned if the value was never computed. For the few cases where the return value is indeed used, it would then be possible to call `get()` before removing.

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {

Review comment:
       I don't think we need to synchronize here. `HashMap` is not thread-safe, so `LazyLoadingMap` which extends `HashMap` and uses it internally, will also not be thread-safe. Or am I missing something?

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {
+                super.remove(key);
+                return suppliers.put(key, supplier);
+            }
+        } else {
+            return suppliers.put(key, supplier);
+        }
+    }
+
+    @Override
+    public T get(Object key) {
+        return lazyCompute(key);
+    }
+
+    @SuppressWarnings("unchecked")
+    private T lazyCompute(Object key) {
+        if(key == null) {

Review comment:
       `HashMap` supports the `null` key, so it could easily supported here as well. Not sure why anyone would use it, but why write extra code in order to impose artificial restrictions?

##########
File path: src/main/java/org/apache/sling/graphql/helpers/layzloading/LazyLoadingMap.java
##########
@@ -0,0 +1,177 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+package org.apache.sling.graphql.helpers.layzloading;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LazyLoadingMap<K, T> extends HashMap<K, T> {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<K, Supplier<T>> suppliers = new HashMap<>();
+    private int suppliersCallCount;
+
+    /** Calls computeAll - should be avoided if possible */
+    @Override
+    public boolean equals(Object o) {
+        if(!(o instanceof LazyLoadingMap)) {
+            return false;
+        }
+        final LazyLoadingMap<?,?> other = (LazyLoadingMap<?,?>)o;
+
+        // Equality seems complicated to compute without this
+        computeAll();
+        return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode() + suppliers.hashCode();
+    }
+
+    /** Adds a Supplier that provides a lazy loaded value.
+     *  Removes existing value with the same key if it exists.
+     */
+    public Supplier<T> put(K key, Supplier<T> supplier) {
+        if(super.containsKey(key)) {
+            synchronized(this) {
+                super.remove(key);
+                return suppliers.put(key, supplier);
+            }
+        } else {
+            return suppliers.put(key, supplier);
+        }
+    }
+
+    @Override
+    public T get(Object key) {
+        return lazyCompute(key);
+    }
+
+    @SuppressWarnings("unchecked")
+    private T lazyCompute(Object key) {
+        if(key == null) {
+            return null;
+        }
+        T value = super.get(key);
+        if(value == null) {
+            synchronized(this) {
+                if(value == null) {
+                    final Supplier<T> s = suppliers.remove(key);
+                    if(s != null) {
+                        suppliersCallCount++;
+                        value = s.get();
+                        super.put((K)key, value);
+                    }
+                }
+            }
+        }
+        return value;
+    }
+
+
+    /** This indicates how many Supplier calls have been made.
+     *  Can be useful in case of doubt, as several methods need
+     *  to call computeAll().
+     */
+    public int getSuppliersCallCount() {
+        return suppliersCallCount;
+    }
+
+    @Override
+    public T remove(Object key) {
+        synchronized(this) {
+            lazyCompute(key);
+            return super.remove(key);
+        }        
+    }
+
+    @Override
+    public void clear() {
+        synchronized(this) {
+            suppliers.clear();
+            super.clear();
+        }
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        return super.containsKey(key) || suppliers.containsKey(key);
+    }
+
+    @Override
+    public int size() {
+        return keySet().size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return super.isEmpty() && suppliers.isEmpty();
+    }
+
+    @Override
+    public Set<K> keySet() {
+        final Set<K> result = new HashSet<>();
+        result.addAll(super.keySet());
+        result.addAll(suppliers.keySet());
+        return result;
+    }
+
+    /** Required for some methods that need all our values
+     *  Calling those methods should be avoided if possible
+     */
+    private void computeAll() {
+        log.info("computeAll called, all remaining lazy values will be evaluated now");
+        suppliers.entrySet().forEach(e -> {

Review comment:
       Does `suppliers.keySet().forEach(key -> this.get(key))` also work? Or does it cause `ConcurrentmodificationException` because `suppliers` has its elements removed during iteration? If that's the case, copying `suppliers.keySet()` before iteration should work.
   
   On a related note: I think the current implementation needs to call `suppliers.clear()` to be correct.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org