Skip to content

Commit 24d491e

Browse files
author
Rishabh Kumar
committed
OAK-11362 : removed usage of Guava's Maps.filterEntries
1 parent 91b7fcd commit 24d491e

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

oak-authorization-principalbased/src/main/java/org/apache/jackrabbit/oak/spi/security/authorization/principalbased/impl/PrincipalPolicyImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.apache.jackrabbit.oak.spi.security.authorization.principalbased.impl;
1818

1919
import org.apache.jackrabbit.guava.common.base.Strings;
20-
import org.apache.jackrabbit.guava.common.collect.Maps;
2120
import org.apache.jackrabbit.api.security.authorization.PrincipalAccessControlList;
2221
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
2322
import org.apache.jackrabbit.oak.api.Tree;
2423
import org.apache.jackrabbit.oak.api.Type;
2524
import org.apache.jackrabbit.oak.commons.PathUtils;
25+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
2626
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
2727
import org.apache.jackrabbit.oak.plugins.tree.TreeUtil;
2828
import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AbstractAccessControlList;
@@ -153,7 +153,7 @@ public boolean addEntry(@NotNull Principal principal, @NotNull Privilege[] privi
153153

154154
String jcrNodePathName = getNamePathMapper().getJcrName(AccessControlConstants.REP_NODE_PATH);
155155
String path = extractPathFromRestrictions(restrictions, jcrNodePathName);
156-
Map<String, Value> filteredRestrictions = Maps.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey()));
156+
Map<String, Value> filteredRestrictions = CollectionUtils.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey()));
157157

158158
return addEntry(path, privileges, filteredRestrictions, (mvRestrictions == null) ? Collections.emptyMap() : mvRestrictions);
159159
}

oak-blob-cloud-azure/src/test/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import javax.net.ssl.HttpsURLConnection;
3535

3636
import org.apache.jackrabbit.guava.common.base.Strings;
37-
import org.apache.jackrabbit.guava.common.collect.Maps;
3837
import com.microsoft.azure.storage.blob.CloudBlobContainer;
3938
import org.apache.commons.io.IOUtils;
4039
import org.apache.jackrabbit.core.data.DataStore;
@@ -110,8 +109,8 @@ public static Properties getAzureConfig() {
110109
IOUtils.closeQuietly(is);
111110
}
112111
props.putAll(getConfig());
113-
Map<String, String> filtered = Maps.filterEntries(CollectionUtils.fromProperties(props),
114-
input -> !Strings.isNullOrEmpty((String) input.getValue()));
112+
Map<String, String> filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props),
113+
input -> !Strings.isNullOrEmpty(input.getValue()));
115114
props = new Properties();
116115
props.putAll(filtered);
117116
}

oak-blob-cloud/src/test/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3DataStoreUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.amazonaws.services.s3.transfer.TransferManager;
4242

4343
import org.apache.jackrabbit.guava.common.base.Strings;
44-
import org.apache.jackrabbit.guava.common.collect.Maps;
4544
import org.apache.commons.io.IOUtils;
4645
import org.apache.jackrabbit.core.data.DataStore;
4746
import org.apache.jackrabbit.oak.commons.PropertiesUtil;
@@ -123,8 +122,8 @@ public static Properties getS3Config() {
123122
IOUtils.closeQuietly(is);
124123
}
125124
props.putAll(getConfig());
126-
Map filtered = Maps.filterEntries(CollectionUtils.fromProperties(props),
127-
input ->!Strings.isNullOrEmpty((String) input.getValue()));
125+
Map<String, String> filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props),
126+
input ->!Strings.isNullOrEmpty(input.getValue()));
128127
props = new Properties();
129128
props.putAll(filtered);
130129
}

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ public static Map<String, String> fromProperties(final @NotNull Properties prope
428428
* @return a new map containing only the entries whose keys match the predicate
429429
* @throws NullPointerException if the map or predicate is null
430430
* @see CollectionUtils#filterValues(Map, Predicate)
431+
* @see CollectionUtils#filterEntries(Map, Predicate)
431432
*/
432433
@NotNull
433434
public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, final @NotNull Predicate<? super K> predicate) {
@@ -436,7 +437,7 @@ public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, final @No
436437
return map.entrySet()
437438
.stream()
438439
.filter(e -> predicate.test(e.getKey())) // using LinkedHashMap to maintain the order of previous map
439-
.collect(LinkedHashMap::new, (m, v)->m.put(v.getKey(), v.getValue()), LinkedHashMap::putAll);
440+
.collect(LinkedHashMap::new, (m, e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll);
440441
}
441442

442443
/**
@@ -450,6 +451,7 @@ public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, final @No
450451
* @return a new map containing only the entries whose values match the predicate
451452
* @throws NullPointerException if the map or predicate is null
452453
* @see CollectionUtils#filterKeys(Map, Predicate)
454+
* @see CollectionUtils#filterEntries(Map, Predicate)
453455
*/
454456
@NotNull
455457
public static <K,V> Map<K, V> filterValues(final @NotNull Map<K, V> map, final @NotNull Predicate<? super V> predicate) {
@@ -458,7 +460,30 @@ public static <K,V> Map<K, V> filterValues(final @NotNull Map<K, V> map, final @
458460
return map.entrySet()
459461
.stream()
460462
.filter(e -> predicate.test(e.getValue())) // using LinkedHashMap to maintain the order of previous map
461-
.collect(LinkedHashMap::new, (m,v)->m.put(v.getKey(), v.getValue()), LinkedHashMap::putAll);
463+
.collect(LinkedHashMap::new, (m,e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll);
464+
}
465+
466+
/**
467+
* Create a new {@link Map} after filtering the entries of the given map
468+
* based on the specified predicate applied to the {@link java.util.Map.Entry}.
469+
*
470+
* @param <K> the type of keys in the map
471+
* @param <V> the type of values in the map
472+
* @param map the map to filter, must not be null
473+
* @param predicate the predicate to apply to the {@link java.util.Map.Entry}, must not be null
474+
* @return a new map containing only the entries whose values match the predicate
475+
* @throws NullPointerException if the map or predicate is null
476+
* @see CollectionUtils#filterKeys(Map, Predicate)
477+
* @see CollectionUtils#filterValues(Map, Predicate)
478+
*/
479+
@NotNull
480+
public static <K,V> Map<K, V> filterEntries(final @NotNull Map<K, V> map, final @NotNull Predicate<? super Map.Entry<K, V>> predicate) {
481+
Objects.requireNonNull(map);
482+
Objects.requireNonNull(predicate);
483+
return map.entrySet()
484+
.stream()
485+
.filter(predicate) // using LinkedHashMap to maintain the order of previous map
486+
.collect(LinkedHashMap::new, (m,e)->m.put(e.getKey(), e.getValue()), LinkedHashMap::putAll);
462487
}
463488

464489
/**

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,64 @@ public void testFilterValuesNullPredicate() {
708708
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterKeys(map, null));
709709
}
710710

711+
@Test
712+
public void testFilterEntries() {
713+
final Map<String, Integer> map = new HashMap<>();
714+
map.put("one", 1);
715+
map.put("two", 2);
716+
map.put("three", 3);
717+
718+
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;
719+
720+
final Map<String, Integer> result = CollectionUtils.filterEntries(map, predicate);
721+
722+
Assert.assertEquals(2, result.size());
723+
Assert.assertTrue(result.containsKey("two"));
724+
Assert.assertTrue(result.containsKey("three"));
725+
Assert.assertFalse(result.containsKey("one"));
726+
}
727+
728+
@Test
729+
public void testFilterEntriesEmptyMap() {
730+
final Map<String, Integer> map = new HashMap<>();
731+
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;
732+
733+
final Map<String, Integer> result = CollectionUtils.filterEntries(map, predicate);
734+
735+
Assert.assertTrue(result.isEmpty());
736+
}
737+
738+
@Test
739+
public void testFilterEntriesNoMatch() {
740+
final Map<String, Integer> map = new HashMap<>();
741+
map.put("one", 1);
742+
map.put("two", 2);
743+
map.put("three", 3);
744+
745+
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 3;
746+
747+
final Map<String, Integer> result = CollectionUtils.filterEntries(map, predicate);
748+
749+
Assert.assertTrue(result.isEmpty());
750+
}
751+
752+
@Test
753+
public void testFilterEntriesNullMap() {
754+
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;
755+
756+
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(null, predicate));
757+
}
758+
759+
@Test
760+
public void testFilterEntriesNullPredicate() {
761+
final Map<String, Integer> map = new HashMap<>();
762+
map.put("one", 1);
763+
map.put("two", 2);
764+
map.put("three", 3);
765+
766+
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(map, null));
767+
}
768+
711769
@Test
712770
public void ensureCapacity() {
713771
int capacity = CollectionUtils.ensureCapacity(8);

0 commit comments

Comments
 (0)