diff --git a/oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java b/oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java index 07ca9467b5c..1cdb1041a7f 100644 --- a/oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java +++ b/oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java @@ -17,12 +17,12 @@ package org.apache.jackrabbit.oak.spi.security.authorization.principalbased.impl; import org.apache.jackrabbit.guava.common.base.Strings; -import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.api.security.authorization.PrincipalAccessControlList; import org.apache.jackrabbit.api.security.authorization.PrivilegeManager; import org.apache.jackrabbit.oak.api.Tree; import org.apache.jackrabbit.oak.api.Type; import org.apache.jackrabbit.oak.commons.PathUtils; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.namepath.NamePathMapper; import org.apache.jackrabbit.oak.plugins.tree.TreeUtil; import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AbstractAccessControlList; @@ -153,7 +153,7 @@ public boolean addEntry(@NotNull Principal principal, @NotNull Privilege[] privi String jcrNodePathName = getNamePathMapper().getJcrName(AccessControlConstants.REP_NODE_PATH); String path = extractPathFromRestrictions(restrictions, jcrNodePathName); - Map filteredRestrictions = Maps.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey())); + Map filteredRestrictions = CollectionUtils.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey())); return addEntry(path, privileges, filteredRestrictions, (mvRestrictions == null) ? Collections.emptyMap() : mvRestrictions); } diff --git a/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreUtils.java b/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreUtils.java index 2edced8d229..f5ef3fe4e9a 100644 --- a/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreUtils.java +++ b/oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreUtils.java @@ -34,7 +34,6 @@ import javax.net.ssl.HttpsURLConnection; import org.apache.jackrabbit.guava.common.base.Strings; -import org.apache.jackrabbit.guava.common.collect.Maps; import com.microsoft.azure.storage.blob.CloudBlobContainer; import org.apache.commons.io.IOUtils; import org.apache.jackrabbit.core.data.DataStore; @@ -110,8 +109,8 @@ public static Properties getAzureConfig() { IOUtils.closeQuietly(is); } props.putAll(getConfig()); - Map filtered = Maps.filterEntries(CollectionUtils.fromProperties(props), - input -> !Strings.isNullOrEmpty((String) input.getValue())); + Map filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props), + input -> !Strings.isNullOrEmpty(input.getValue())); props = new Properties(); props.putAll(filtered); } diff --git a/oak-blob-cloud/src/test/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3DataStoreUtils.java b/oak-blob-cloud/src/test/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3DataStoreUtils.java index b628f46df2c..d913e19c0bd 100644 --- a/oak-blob-cloud/src/test/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3DataStoreUtils.java +++ b/oak-blob-cloud/src/test/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3DataStoreUtils.java @@ -41,7 +41,6 @@ import com.amazonaws.services.s3.transfer.TransferManager; import org.apache.jackrabbit.guava.common.base.Strings; -import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.commons.io.IOUtils; import org.apache.jackrabbit.core.data.DataStore; import org.apache.jackrabbit.oak.commons.PropertiesUtil; @@ -123,8 +122,8 @@ public static Properties getS3Config() { IOUtils.closeQuietly(is); } props.putAll(getConfig()); - Map filtered = Maps.filterEntries(CollectionUtils.fromProperties(props), - input ->!Strings.isNullOrEmpty((String) input.getValue())); + Map filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props), + input ->!Strings.isNullOrEmpty(input.getValue())); props = new Properties(); props.putAll(filtered); } 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 a92e775b69a..7be1a8c8bd8 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 @@ -428,6 +428,7 @@ public static Map fromProperties(final @NotNull Properties prope * @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) + * @see CollectionUtils#filterEntries(Map, Predicate) */ @NotNull public static Map filterKeys(final @NotNull Map map, final @NotNull Predicate predicate) { @@ -436,7 +437,7 @@ public static Map filterKeys(final @NotNull Map map, final @No return map.entrySet() .stream() .filter(e -> predicate.test(e.getKey())) // using LinkedHashMap to maintain the order of previous map - .collect(LinkedHashMap::new, (m, v)->m.put(v.getKey(), v.getValue()), LinkedHashMap::putAll); + .collect(LinkedHashMap::new, (m, e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll); } /** @@ -450,6 +451,7 @@ public static Map filterKeys(final @NotNull Map map, final @No * @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) + * @see CollectionUtils#filterEntries(Map, Predicate) */ @NotNull public static Map filterValues(final @NotNull Map map, final @NotNull Predicate predicate) { @@ -458,7 +460,30 @@ public static Map filterValues(final @NotNull Map map, final @ 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); + .collect(LinkedHashMap::new, (m,e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll); + } + + /** + * Create a new {@link Map} after filtering the entries of the given map + * based on the specified predicate applied to the {@link java.util.Map.Entry}. + * + * @param the type of keys in the map + * @param 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 {@link java.util.Map.Entry}, 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) + * @see CollectionUtils#filterValues(Map, Predicate) + */ + @NotNull + public static Map filterEntries(final @NotNull Map map, final @NotNull Predicate> predicate) { + Objects.requireNonNull(map); + Objects.requireNonNull(predicate); + return map.entrySet() + .stream() + .filter(predicate) // using LinkedHashMap to maintain the order of previous map + .collect(LinkedHashMap::new, (m,e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll); } /** 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 15a5ca2ab0a..2f3168d3c09 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 @@ -708,6 +708,64 @@ public void testFilterValuesNullPredicate() { Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterKeys(map, null)); } + @Test + public void testFilterEntries() { + final Map map = new HashMap<>(); + map.put("one", 1); + map.put("two", 2); + map.put("three", 3); + + final Predicate> predicate = entry -> entry.getValue() > 1; + + final Map result = CollectionUtils.filterEntries(map, predicate); + + Assert.assertEquals(2, result.size()); + Assert.assertTrue(result.containsKey("two")); + Assert.assertTrue(result.containsKey("three")); + Assert.assertFalse(result.containsKey("one")); + } + + @Test + public void testFilterEntriesEmptyMap() { + final Map map = new HashMap<>(); + final Predicate> predicate = entry -> entry.getValue() > 1; + + final Map result = CollectionUtils.filterEntries(map, predicate); + + Assert.assertTrue(result.isEmpty()); + } + + @Test + public void testFilterEntriesNoMatch() { + final Map map = new HashMap<>(); + map.put("one", 1); + map.put("two", 2); + map.put("three", 3); + + final Predicate> predicate = entry -> entry.getValue() > 3; + + final Map result = CollectionUtils.filterEntries(map, predicate); + + Assert.assertTrue(result.isEmpty()); + } + + @Test + public void testFilterEntriesNullMap() { + final Predicate> predicate = entry -> entry.getValue() > 1; + + Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(null, predicate)); + } + + @Test + public void testFilterEntriesNullPredicate() { + final Map map = new HashMap<>(); + map.put("one", 1); + map.put("two", 2); + map.put("three", 3); + + Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(map, null)); + } + @Test public void ensureCapacity() { int capacity = CollectionUtils.ensureCapacity(8);