Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Value> filteredRestrictions = Maps.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey()));
Map<String, Value> filteredRestrictions = CollectionUtils.filterEntries(restrictions, entry -> !jcrNodePathName.equals(entry.getKey()));

return addEntry(path, privileges, filteredRestrictions, (mvRestrictions == null) ? Collections.emptyMap() : mvRestrictions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,8 +109,8 @@ public static Properties getAzureConfig() {
IOUtils.closeQuietly(is);
}
props.putAll(getConfig());
Map<String, String> filtered = Maps.filterEntries(CollectionUtils.fromProperties(props),
input -> !Strings.isNullOrEmpty((String) input.getValue()));
Map<String, String> filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props),
input -> !Strings.isNullOrEmpty(input.getValue()));
props = new Properties();
props.putAll(filtered);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> filtered = CollectionUtils.filterEntries(CollectionUtils.fromProperties(props),
input ->!Strings.isNullOrEmpty(input.getValue()));
props = new Properties();
props.putAll(filtered);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public static Map<String, String> 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 <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, final @NotNull Predicate<? super K> predicate) {
Expand All @@ -436,7 +437,7 @@ public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> 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);
}

/**
Expand All @@ -450,6 +451,7 @@ public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> 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 <K,V> Map<K, V> filterValues(final @NotNull Map<K, V> map, final @NotNull Predicate<? super V> predicate) {
Expand All @@ -458,7 +460,30 @@ public static <K,V> Map<K, V> filterValues(final @NotNull Map<K, V> 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 <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 {@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 <K,V> Map<K, V> filterEntries(final @NotNull Map<K, V> map, final @NotNull Predicate<? super Map.Entry<K, V>> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,64 @@ public void testFilterValuesNullPredicate() {
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterKeys(map, null));
}

@Test
public void testFilterEntries() {
final Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;

final Map<String, Integer> 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<String, Integer> map = new HashMap<>();
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;

final Map<String, Integer> result = CollectionUtils.filterEntries(map, predicate);

Assert.assertTrue(result.isEmpty());
}

@Test
public void testFilterEntriesNoMatch() {
final Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);

final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 3;

final Map<String, Integer> result = CollectionUtils.filterEntries(map, predicate);

Assert.assertTrue(result.isEmpty());
}

@Test
public void testFilterEntriesNullMap() {
final Predicate<Map.Entry<String, Integer>> predicate = entry -> entry.getValue() > 1;

Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(null, predicate));
}

@Test
public void testFilterEntriesNullPredicate() {
final Map<String, Integer> 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);
Expand Down
Loading