Skip to content

Commit

Permalink
OAK-11375 : removed usage of Guava's Maps.transformValues
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Kumar committed Jan 9, 2025
1 parent db447dc commit b306ddb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,7 +139,7 @@ private List<PropInfo> mockPropInfos(@Nullable String effectivePath, @NotNull S
}

private List<PropInfo> mockPropInfos(@NotNull Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -486,6 +487,27 @@ public static <K,V> Map<K, V> filterEntries(final @NotNull Map<K, V> 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 <K> the type of keys in the map
* @param <V1> the type of original values in the map
* @param <V2> 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 <K, V1, V2> Map<K, V2> transformValues(final @NotNull Map<K, V1> map, final @NotNull Function<? super V1, ? extends V2> 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}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -766,6 +767,50 @@ public void testFilterEntriesNullPredicate() {
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterEntries(map, null));
}

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

final Function<Integer, String> function = value -> "Number " + value;

final Map<String, String> 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<String, Integer> map = new HashMap<>();
final Function<Integer, String> function = value -> "Number " + value;

final Map<String, String> result = CollectionUtils.transformValues(map, function);

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

@Test
public void testTransformValuesNullMap() {
final Function<Integer, String> function = value -> "Number " + value;

Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.transformValues(null, function));
}

@Test
public void testTransformValuesNullFunction() {
final Map<String, Integer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -268,8 +270,7 @@ Commit build(@NotNull Revision revision) {
requireNonNull(revision);

Revision from = this.revision;
Map<Path, UpdateOp> operations = Maps.transformValues(
this.operations, op -> rewrite(op, from, revision));
Map<Path, UpdateOp> operations = CollectionUtils.transformValues(this.operations, op -> rewrite(op, from, revision));
return new Commit(nodeStore, revision, baseRevision, startRevisions,
operations, addedNodes, removedNodes, nodesWithBinaries,
bundledNodes);
Expand Down

0 comments on commit b306ddb

Please sign in to comment.