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:
us...@infra.apache.org


Reply via email to