From b306ddb34408a1bc486d1935b3931786fa75b716 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Thu, 9 Jan 2025 22:45:09 +0530 Subject: [PATCH] OAK-11375 : removed usage of Guava's Maps.transformValues --- .../impl/PrincipalPolicyImporterTest.java | 3 +- .../commons/collections/CollectionUtils.java | 22 +++++++++ .../collections/CollectionUtilsTest.java | 45 +++++++++++++++++++ .../oak/plugins/document/CommitBuilder.java | 5 ++- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImporterTest.java b/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImporterTest.java index a6aabe51378..3d9070608de 100644 --- a/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImporterTest.java +++ b/oak-authorization-principalbased/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImporterTest.java @@ -21,6 +21,7 @@ import org.apache.jackrabbit.api.security.user.User; import org.apache.jackrabbit.oak.api.Tree; import org.apache.jackrabbit.oak.commons.PathUtils; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.plugins.tree.TreeUtil; import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters; import org.apache.jackrabbit.oak.spi.security.authorization.AuthorizationConfiguration; @@ -138,7 +139,7 @@ private List mockPropInfos(@Nullable String effectivePath, @NotNull S } private List mockPropInfos(@NotNull Map restrictions, int propertyType) throws RepositoryException { - return mockPropInfos(Maps.transformValues(restrictions, string -> { + return mockPropInfos(CollectionUtils.transformValues(restrictions, string -> { try { return new Value[] {getValueFactory(root).createValue(string, propertyType)}; } catch (ValueFormatException e) { 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 7be1a8c8bd8..6418ff671ac 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 @@ -36,6 +36,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -486,6 +487,27 @@ public static Map filterEntries(final @NotNull Map map, final .collect(LinkedHashMap::new, (m,e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll); } + /** + * Create a new {@link Map} after transforming the values of the given map + * based on the specified function. + * + * @param the type of keys in the map + * @param the type of original values in the map + * @param the type of transformed values in the map + * @param map the map whose values are to be transformed, must not be null + * @param function the function to apply to the values, must not be null + * @return a new map containing the same keys as the given map, but with values transformed by the specified function + * @throws NullPointerException if the map or function is null + */ + @NotNull + public static Map transformValues(final @NotNull Map map, final @NotNull Function function) { + Objects.requireNonNull(map); + Objects.requireNonNull(function); + return map.entrySet() + .stream() // using LinkedHashMap to maintain the order of previous map + .collect(LinkedHashMap::new, (m, e)->m.put(e.getKey(), function.apply(e.getValue())), LinkedHashMap::putAll); + } + /** * Convert an {@code Iterator} to an {@code Iterable}. *

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 2f3168d3c09..116ad0669cf 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 @@ -34,6 +34,7 @@ import java.util.Properties; import java.util.Set; import java.util.TreeSet; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -766,6 +767,50 @@ public void testFilterEntriesNullPredicate() { Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(map, null)); } + @Test + public void testTransformValues() { + final Map map = new HashMap<>(); + map.put("one", 1); + map.put("two", 2); + map.put("three", 3); + + final Function function = value -> "Number " + value; + + final Map result = CollectionUtils.transformValues(map, function); + + Assert.assertEquals(3, result.size()); + Assert.assertEquals("Number 1", result.get("one")); + Assert.assertEquals("Number 2", result.get("two")); + Assert.assertEquals("Number 3", result.get("three")); + } + + @Test + public void testTransformValuesEmptyMap() { + final Map map = new HashMap<>(); + final Function function = value -> "Number " + value; + + final Map result = CollectionUtils.transformValues(map, function); + + Assert.assertTrue(result.isEmpty()); + } + + @Test + public void testTransformValuesNullMap() { + final Function function = value -> "Number " + value; + + Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.transformValues(null, function)); + } + + @Test + public void testTransformValuesNullFunction() { + final Map map = new HashMap<>(); + map.put("one", 1); + map.put("two", 2); + map.put("three", 3); + + Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.transformValues(map, null)); + } + @Test public void ensureCapacity() { int capacity = CollectionUtils.ensureCapacity(8); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/CommitBuilder.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/CommitBuilder.java index 9a38c5333d5..78049ad60ab 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/CommitBuilder.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/CommitBuilder.java @@ -21,10 +21,12 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.oak.api.PropertyState; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.spi.state.NodeState; import org.jetbrains.annotations.NotNull; @@ -268,8 +270,7 @@ Commit build(@NotNull Revision revision) { requireNonNull(revision); Revision from = this.revision; - Map operations = Maps.transformValues( - this.operations, op -> rewrite(op, from, revision)); + Map operations = CollectionUtils.transformValues(this.operations, op -> rewrite(op, from, revision)); return new Commit(nodeStore, revision, baseRevision, startRevisions, operations, addedNodes, removedNodes, nodesWithBinaries, bundledNodes);