Skip to content

Commit

Permalink
Merge pull request #1931 from apache/OAK-11334
Browse files Browse the repository at this point in the history
OAK-11334 : removed usage of Guava Lists.reverse
rishabhdaim authored Jan 2, 2025
2 parents 29a7d8b + 30e4017 commit 9eae47c
Showing 10 changed files with 69 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -114,6 +114,20 @@ public static <T> List<List<T>> partitionList(final List<T> list, final int n) {
.collect(Collectors.toList());
}

/**
* Returns a new list containing the elements of the specified list in reverse order.
*
* @param <T> the type of elements in the list
* @param l the list to be reversed, must not be null
* @return a new list containing the elements of the specified list in reverse order
* @throws NullPointerException if the list is null
*/
@NotNull
public static <T> List<T> reverse(final List<T> l) {
Objects.requireNonNull(l);
return IntStream.range(0, l.size()).map(i -> l.size() - 1- i).mapToObj(l::get).collect(Collectors.toList());
}

/**
* Convert an iterable to a set. The returning set is mutable and supports all optional operations.
* @param iterable the iterable to convert
Original file line number Diff line number Diff line change
@@ -83,6 +83,37 @@ public void partitionListWhenListIsSmallerThanPartitionSize() {
Assert.assertEquals(List.of("a"), partitions.get(0));
}

@Test
public void testReverseWithNonEmptyList() {
List<String> list = List.of("a", "b", "c");
List<String> result = CollectionUtils.reverse(list);
List<String> expected = List.of("c", "b", "a");
Assert.assertEquals(expected, result);
}

@Test
public void testReverseWithEmptyList() {
List<String> list = List.of();
List<String> result = CollectionUtils.reverse(list);
List<String> expected = List.of();
Assert.assertEquals(expected, result);
}

@Test(expected = NullPointerException.class)
public void testReverseWithNullList() {
List<String> list = null;
CollectionUtils.reverse(list);
fail("Shouldn't reach here");
}

@Test
public void testReverseWithSingleElementList() {
List<String> list = List.of("a");
List<String> result = CollectionUtils.reverse(list);
List<String> expected = List.of("a");
Assert.assertEquals(expected, result);
}

@Test
public void iterableToSet() {
// create an iterable
Original file line number Diff line number Diff line change
@@ -17,12 +17,12 @@
package org.apache.jackrabbit.oak.plugins.index;

import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.oak.api.Result;
import org.apache.jackrabbit.oak.api.ResultRow;
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.plugins.index.search.FulltextIndexConstants;
import org.apache.jackrabbit.oak.plugins.index.search.util.IndexDefinitionBuilder;
import org.apache.jackrabbit.oak.query.AbstractQueryTest;
@@ -347,7 +347,7 @@ public void sortOnNodeName() throws Exception {

query = "/jcr:root/test/* order by fn:name() descending option(index tag fnName)";
assertXpathPlan(query, "/oak:index/fnName");
assertEquals(Lists.reverse(expected), executeQuery(query, XPATH));
assertEquals(CollectionUtils.reverse(expected), executeQuery(query, XPATH));

// order by fn:name() although function index is on "name()"
query = "/jcr:root/test/* order by fn:name() option(index tag name)";
@@ -360,7 +360,7 @@ public void sortOnNodeName() throws Exception {

query = "/jcr:root/test/* order by fn:name() descending option(index tag name)";
assertXpathPlan(query, "/oak:index/name");
assertEquals(Lists.reverse(expected), executeQuery(query, XPATH));
assertEquals(CollectionUtils.reverse(expected), executeQuery(query, XPATH));
});
}

@@ -421,7 +421,7 @@ public void sortOnLocalName() throws Exception {

query = "/jcr:root/test/* order by fn:local-name() descending option(index tag fnLocalName)";
assertXpathPlan(query, "/oak:index/fnLocalName");
assertEquals(Lists.reverse(expected), executeQuery(query, XPATH));
assertEquals(CollectionUtils.reverse(expected), executeQuery(query, XPATH));

// order by fn:name() although function index is on "name()"
query = "/jcr:root/test/* order by fn:local-name() option(index tag localName)";
@@ -434,7 +434,7 @@ public void sortOnLocalName() throws Exception {

query = "/jcr:root/test/* order by fn:local-name() descending option(index tag localName)";
assertXpathPlan(query, "/oak:index/localName");
assertEquals(Lists.reverse(expected), executeQuery(query, XPATH));
assertEquals(CollectionUtils.reverse(expected), executeQuery(query, XPATH));
});
}

Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import java.util.stream.IntStream;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzuriteDockerRule;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.segment.remote.WriteAccessController;
import org.apache.jackrabbit.oak.segment.spi.persistence.JournalFileReader;
import org.apache.jackrabbit.oak.segment.spi.persistence.JournalFileWriter;
@@ -37,7 +38,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.apache.jackrabbit.guava.common.collect.Lists.reverse;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -129,7 +129,7 @@ public void testBatchWriteLines() throws IOException {
}

List<String> entries = readEntriesFromJournal();
assertEquals(lines, reverse(entries));
assertEquals(lines, CollectionUtils.reverse(entries));
}

@Test
@@ -191,7 +191,7 @@ public void testBatchWriteLines_splitJournalFile() throws Exception {

private void assertJournalEntriesCount(int index) throws IOException {
List<String> entries = readEntriesFromJournal();
assertEquals(buildLines(0, index), reverse(entries));
assertEquals(buildLines(0, index), CollectionUtils.reverse(entries));
}

@NotNull
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
package org.apache.jackrabbit.oak.segment.tool;

import static java.util.Objects.requireNonNull;
import static org.apache.jackrabbit.guava.common.collect.Lists.reverse;
import static org.apache.jackrabbit.oak.commons.PathUtils.elements;
import static org.apache.jackrabbit.oak.segment.RecordId.fromString;

@@ -27,6 +26,7 @@
import java.util.Iterator;
import java.util.List;

import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.segment.RecordId;
import org.apache.jackrabbit.oak.segment.SegmentIdProvider;
import org.apache.jackrabbit.oak.segment.SegmentNotFoundException;
@@ -281,7 +281,7 @@ private void diff() throws Exception {
List<String> revDiffs = revs.subList(Math.min(s, e), Math.max(s, e) + 1);
if (s > e) {
// reverse list
revDiffs = reverse(revDiffs);
revDiffs = CollectionUtils.reverse(revDiffs);
}
if (revDiffs.size() < 2) {
System.out.println("Nothing to diff: " + revDiffs);
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
import static java.util.Objects.requireNonNull;
import static org.apache.jackrabbit.guava.common.collect.Iterables.partition;
import static org.apache.jackrabbit.guava.common.collect.Iterables.transform;
import static org.apache.jackrabbit.guava.common.collect.Lists.reverse;
import static java.util.Collections.singletonList;
import static java.util.Objects.nonNull;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
@@ -1960,7 +1959,7 @@ RevisionVector reset(@NotNull RevisionVector branchHead,
// reset each branch commit in reverse order
Map<Path, UpdateOp> operations = new HashMap<>();
AtomicReference<Revision> currentRev = new AtomicReference<>();
for (Revision r : reverse(revs)) {
for (Revision r : CollectionUtils.reverse(revs)) {
operations.clear();
Revision previous = currentRev.getAndSet(r);
if (previous == null) {
Original file line number Diff line number Diff line change
@@ -96,8 +96,6 @@

import org.apache.jackrabbit.guava.common.base.Throwables;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Lists;

import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
@@ -3326,7 +3324,7 @@ public void run() {
assertNotNull(doc);
long previousValue = -1;
List<String> values = new ArrayList<>(doc.getLocalMap("p").values());
for (String v : Lists.reverse(values)) {
for (String v : CollectionUtils.reverse(values)) {
long currentValue = Long.parseLong(v);
assertEquals(previousValue + 1, currentValue);
previousValue = currentValue;
Original file line number Diff line number Diff line change
@@ -35,8 +35,6 @@

import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.guava.common.collect.Lists;

import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
@@ -799,8 +797,7 @@ public void getChangesMixedClusterIds() throws Exception {
DocumentNodeStore ns2 = createTestStore(store, 2, 0);
List<DocumentNodeStore> nodeStores = List.of(ns1, ns2);

List<RevisionVector> headRevisions = Lists.reverse(
createTestData(nodeStores, random, numChanges));
List<RevisionVector> headRevisions = CollectionUtils.reverse(createTestData(nodeStores, random, numChanges));
NodeDocument doc = getRootDocument(store);
for (int i = 0; i < 10; i++) {
int idx = random.nextInt(numChanges);
@@ -979,8 +976,7 @@ public <T extends Document> T find(Collection<T> collection,
DocumentNodeStore ns1 = createTestStore(store, 1, 0);
DocumentNodeStore ns2 = createTestStore(store, 2, 0);
List<DocumentNodeStore> nodeStores = List.of(ns1, ns2);
List<RevisionVector> headRevisions = Lists.reverse(
createTestData(nodeStores, random, numChanges));
List<RevisionVector> headRevisions = CollectionUtils.reverse(createTestData(nodeStores, random, numChanges));

NodeDocument doc = getRootDocument(store);

@@ -1057,8 +1053,7 @@ public <T extends Document> T find(Collection<T> collection,
DocumentNodeStore ns1 = createTestStore(store, 1, 0);
DocumentNodeStore ns2 = createTestStore(store, 2, 0);
List<DocumentNodeStore> nodeStores = List.of(ns1, ns2);
List<RevisionVector> headRevisions = Lists.reverse(
createTestData(nodeStores, random, numChanges));
List<RevisionVector> headRevisions = CollectionUtils.reverse(createTestData(nodeStores, random, numChanges));

NodeBuilder builder = ns1.getRoot().builder();
builder.setProperty("q", 1);
@@ -1072,8 +1067,7 @@ public <T extends Document> T find(Collection<T> collection,
}
// do not yet merge, but create more test data
int numMoreChanges = 50;
List<RevisionVector> moreRevs = Lists.reverse(
createTestData(nodeStores, random, numMoreChanges, numChanges));
List<RevisionVector> moreRevs = CollectionUtils.reverse(createTestData(nodeStores, random, numMoreChanges, numChanges));
headRevisions = CollectionUtils.toList(Iterables.concat(moreRevs, headRevisions));
numChanges += numMoreChanges;

@@ -1086,8 +1080,7 @@ public <T extends Document> T find(Collection<T> collection,

// and create yet more test data
numMoreChanges = 50;
moreRevs = Lists.reverse(
createTestData(nodeStores, random, numMoreChanges, numChanges));
moreRevs = CollectionUtils.reverse(createTestData(nodeStores, random, numMoreChanges, numChanges));
headRevisions = CollectionUtils.toList(Iterables.concat(moreRevs, headRevisions));
numChanges += numMoreChanges;

@@ -1192,8 +1185,7 @@ public void getVisibleChangesMixedClusterIds() throws Exception {
DocumentNodeStore ns2 = createTestStore(store, 2, 0);
List<DocumentNodeStore> nodeStores = List.of(ns1, ns2);

List<RevisionVector> headRevisions = Lists.reverse(
createTestData(nodeStores, random, numChanges));
List<RevisionVector> headRevisions = CollectionUtils.reverse(createTestData(nodeStores, random, numChanges));
NodeDocument doc = getRootDocument(store);
for (int i = 0; i < 10; i++) {
int idx = random.nextInt(numChanges);
Original file line number Diff line number Diff line change
@@ -21,8 +21,8 @@
import java.util.List;
import java.util.Set;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.util.Text;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -47,7 +47,7 @@ public MoveTracker() {
public void addMove(@NotNull String sourcePath, @NotNull String destPath) {
// calculate original source path
String originalSource = sourcePath;
for (MoveEntry me : Lists.reverse(entries)) {
for (MoveEntry me : CollectionUtils.reverse(entries)) {
if (Text.isDescendantOrEqual(me.destPath, sourcePath)) {
String relPath = PathUtils.relativize(me.destPath, sourcePath);
if (!relPath.isEmpty()) {
@@ -68,7 +68,7 @@ public boolean isEmpty() {

@Nullable
public String getSourcePath(String destPath) {
for (MoveEntry me : Lists.reverse(entries)) {
for (MoveEntry me : CollectionUtils.reverse(entries)) {
if (me.destPath.equals(destPath)) {
return me.sourcePath;
}
@@ -78,7 +78,7 @@ public String getSourcePath(String destPath) {

@Nullable
public String getDestPath(String sourcePath) {
for (MoveEntry me : Lists.reverse(entries)) {
for (MoveEntry me : CollectionUtils.reverse(entries)) {
if (me.sourcePath.equals(sourcePath)) {
return me.destPath;
}
Original file line number Diff line number Diff line change
@@ -24,9 +24,9 @@
import java.util.List;
import java.util.function.Supplier;

import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.commons.Buffer;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.conditions.Validate;
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder;
import org.apache.jackrabbit.oak.segment.RecordId;
@@ -124,7 +124,7 @@ private String getPath() {
}
currentDiff = currentDiff.parent;
}
segments = Lists.reverse(segments);
segments = CollectionUtils.reverse(segments);

StringBuilder path = new StringBuilder();
for (String segment : segments) {

0 comments on commit 9eae47c

Please sign in to comment.