This is an automated email from the ASF dual-hosted git repository.

daim pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 91b7fcde16 OAK-11351 : removed usage of Guava's Maps.filterValues 
(#1959)
91b7fcde16 is described below

commit 91b7fcde1608e096affcf3e27349932f33ae9fce
Author: Rishabh Kumar <[email protected]>
AuthorDate: Thu Jan 9 10:53:35 2025 +0530

    OAK-11351 : removed usage of Guava's Maps.filterValues (#1959)
    
    Co-authored-by: Rishabh Kumar <[email protected]>
---
 .../oak/commons/collections/CollectionUtils.java   | 23 +++++++
 .../commons/collections/CollectionUtilsTest.java   | 74 ++++++++++++++++++++++
 .../oak/plugins/index/lucene/IndexTracker.java     |  4 +-
 .../search/spi/query/FulltextIndexTracker.java     |  4 +-
 .../document/memory/MemoryDocumentStore.java       |  9 ++-
 .../oak/plugins/memory/ModifiedNodeState.java      |  6 +-
 6 files changed, 108 insertions(+), 12 deletions(-)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
index d97ac059f5..a92e775b69 100644
--- 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
@@ -427,6 +427,7 @@ public class CollectionUtils {
      * @param predicate the predicate to apply to the keys, must not be null
      * @return a new map containing only the entries whose keys match the 
predicate
      * @throws NullPointerException if the map or predicate is null
+     * @see CollectionUtils#filterValues(Map, Predicate)
      */
     @NotNull
     public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, 
final @NotNull Predicate<? super K> predicate) {
@@ -438,6 +439,28 @@ public class CollectionUtils {
                 .collect(LinkedHashMap::new, (m, v)->m.put(v.getKey(), 
v.getValue()), LinkedHashMap::putAll);
     }
 
+    /**
+     * Create a new {@link Map} after filtering the entries of the given map
+     * based on the specified predicate applied to the values.
+     *
+     * @param <K> the type of keys in the map
+     * @param <V> the type of values in the map
+     * @param map the map to filter, must not be null
+     * @param predicate the predicate to apply to the values, must not be null
+     * @return a new map containing only the entries whose values match the 
predicate
+     * @throws NullPointerException if the map or predicate is null
+     * @see CollectionUtils#filterKeys(Map, Predicate)
+     */
+    @NotNull
+    public static <K,V> Map<K, V> filterValues(final @NotNull Map<K, V> map, 
final @NotNull Predicate<? super V> predicate) {
+        Objects.requireNonNull(map);
+        Objects.requireNonNull(predicate);
+        return map.entrySet()
+                .stream()
+                .filter(e -> predicate.test(e.getValue())) // using 
LinkedHashMap to maintain the order of previous map
+                .collect(LinkedHashMap::new, (m,v)->m.put(v.getKey(), 
v.getValue()), LinkedHashMap::putAll);
+    }
+
     /**
      * Convert an {@code Iterator} to an {@code Iterable}.
      * <p>
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
index 5f35e7db6b..15a5ca2ab0 100644
--- 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
@@ -634,6 +634,80 @@ public class CollectionUtilsTest {
         Assert.assertThrows(NullPointerException.class, () -> 
CollectionUtils.filterKeys(map, null));
     }
 
+    @Test
+    public void testFilterValues() {
+        final Map<Integer, String> map = new HashMap<>();
+        map.put(1, "one");
+        map.put(2, "two");
+        map.put(3, "three");
+
+        final Predicate<String> predicate = value -> value.startsWith("t");
+
+        final Map<Integer, String> result = CollectionUtils.filterValues(map, 
predicate);
+
+        Assert.assertEquals(2, result.size());
+        Assert.assertTrue(result.containsKey(2));
+        Assert.assertTrue(result.containsKey(3));
+        Assert.assertFalse(result.containsKey(1));
+    }
+
+    @Test
+    public void testFilterValuesEmptyMap() {
+        final Map<String, String> map = new HashMap<>();
+        final Predicate<String> predicate = key -> key.startsWith("t");
+
+        final Map<String, String> result = CollectionUtils.filterValues(map, 
predicate);
+
+        Assert.assertTrue(result.isEmpty());
+    }
+
+    @Test
+    public void testFilterNullValues() {
+        final Map<String, Integer> map = new HashMap<>();
+        map.put("one", 1);
+        map.put("two", 2);
+        map.put("three", 3);
+        map.put(null, null);
+
+        final Predicate<Integer> predicate = Objects::isNull;
+
+        final Map<String, Integer> result = CollectionUtils.filterValues(map, 
predicate);
+
+        Assert.assertEquals(1, result.size());
+    }
+
+    @Test
+    public void testFilterNonNullValues() {
+        final Map<String, Integer> map = new HashMap<>();
+        map.put("one", 1);
+        map.put("two", 2);
+        map.put("three", 3);
+        map.put(null, null);
+
+        final Predicate<Integer> predicate = Objects::nonNull;
+
+        final Map<String, Integer> result = CollectionUtils.filterValues(map, 
predicate);
+
+        Assert.assertEquals(3, result.size());
+    }
+
+    @Test
+    public void testFilterValuesNullMap() {
+        final Predicate<String> predicate = key -> key.startsWith("t");
+
+        Assert.assertThrows(NullPointerException.class, () -> 
CollectionUtils.filterKeys(null, predicate));
+    }
+
+    @Test
+    public void testFilterValuesNullPredicate() {
+        final Map<String, Integer> map = new HashMap<>();
+        map.put("one", 1);
+        map.put("two", 2);
+        map.put("three", 3);
+
+        Assert.assertThrows(NullPointerException.class, () -> 
CollectionUtils.filterKeys(map, null));
+    }
+
     @Test
     public void ensureCapacity() {
         int capacity = CollectionUtils.ensureCapacity(8);
diff --git 
a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
 
b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
index e4b9281e2a..2e4c416ebf 100644
--- 
a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
+++ 
b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
@@ -23,10 +23,10 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 
 import org.apache.jackrabbit.guava.common.collect.Iterables;
-import org.apache.jackrabbit.guava.common.collect.Maps;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.commons.PerfLogger;
 import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
@@ -186,7 +186,7 @@ public class IndexTracker {
         if (!updates.isEmpty()) {
             Map<String, LuceneIndexNodeManager> builder = new HashMap<>();
             builder.putAll(CollectionUtils.filterKeys(original, x -> 
!updates.containsKey(x)));
-            builder.putAll(Maps.filterValues(updates, x -> x != null));
+            builder.putAll(CollectionUtils.filterValues(updates, 
Objects::nonNull));
             indices = Collections.unmodifiableMap(builder);
 
             badIndexTracker.markGoodIndexes(updates.keySet());
diff --git 
a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexTracker.java
 
b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexTracker.java
index ac9b5c4798..df5f2291c4 100644
--- 
a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexTracker.java
+++ 
b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexTracker.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 
 import org.apache.jackrabbit.guava.common.collect.Iterables;
@@ -46,7 +47,6 @@ import org.slf4j.LoggerFactory;
 
 import static java.util.Objects.requireNonNull;
 
-import static org.apache.jackrabbit.guava.common.collect.Maps.filterValues;
 import static java.util.Collections.emptyMap;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.INDEX_DEFINITION_NODE;
 import static 
org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.STATUS_NODE;
@@ -149,7 +149,7 @@ public abstract class FulltextIndexTracker<I extends 
IndexNodeManager<N>, N exte
         if (!updates.isEmpty()) {
             Map<String, I> builder = new HashMap<>();
             builder.putAll(CollectionUtils.filterKeys(original, x -> 
!updates.containsKey(x)));
-            builder.putAll(filterValues(updates, x -> x != null));
+            builder.putAll(CollectionUtils.filterValues(updates, 
Objects::nonNull));
             indices = Collections.unmodifiableMap(builder);
 
             badIndexTracker.markGoodIndexes(updates.keySet());
diff --git 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
index 2438b8c2ee..6fe32fffe7 100644
--- 
a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
+++ 
b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/memory/MemoryDocumentStore.java
@@ -32,7 +32,6 @@ import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.jackrabbit.guava.common.base.Splitter;
-import org.apache.jackrabbit.guava.common.collect.Maps;
 import org.apache.jackrabbit.oak.cache.CacheStats;
 import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
 import org.apache.jackrabbit.oak.plugins.document.Collection;
@@ -226,10 +225,10 @@ public class MemoryDocumentStore implements DocumentStore 
{
         Lock lock = rwLock.writeLock();
         lock.lock();
         try {
-            Maps.filterValues(map, doc -> {
-                    Long modified = Utils.asLong((Number) 
doc.get(indexedProperty));
-                    return startValue < modified && modified < endValue;
-                }).clear();
+            map.entrySet().removeIf(entry -> {
+                Long modified = Utils.asLong((Number) 
entry.getValue().get(indexedProperty));
+                return startValue < modified && modified < endValue;
+            });
         } finally {
             lock.unlock();
         }
diff --git 
a/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
 
b/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
index b11c9292da..13d3bbfe48 100644
--- 
a/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
+++ 
b/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ModifiedNodeState.java
@@ -20,7 +20,6 @@ import static java.util.Objects.requireNonNull;
 
 import static org.apache.jackrabbit.guava.common.collect.Iterables.concat;
 import static org.apache.jackrabbit.guava.common.collect.Iterables.filter;
-import static org.apache.jackrabbit.guava.common.collect.Maps.filterValues;
 
 import static java.util.Collections.emptyList;
 import static java.util.Collections.emptyMap;
@@ -33,6 +32,7 @@ import java.util.Set;
 import java.util.function.Predicate;
 
 import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
 import org.apache.jackrabbit.oak.spi.state.AbstractNodeState;
 import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
@@ -215,7 +215,7 @@ public class ModifiedNodeState extends AbstractNodeState {
             final Set<String> keys = nodes.keySet(); 
             return concat(
                     filter(base.getChildNodeNames(), x -> !keys.contains(x)),
-                    filterValues(nodes, NodeState.EXISTS::test).keySet());
+                    CollectionUtils.filterValues(nodes, 
NodeState.EXISTS).keySet());
         }
     }
 
@@ -348,7 +348,7 @@ public class ModifiedNodeState extends AbstractNodeState {
                     x -> !keys.contains(x == null ? null : x.getName());
             return concat(
                     filter(base.getChildNodeEntries(), predicate::test),
-                    iterable(filterValues(nodes, 
NodeState.EXISTS::test).entrySet()));
+                    iterable(CollectionUtils.filterValues(nodes, 
NodeState.EXISTS).entrySet()));
         }
     }
 

Reply via email to