Skip to content

Commit 4485148

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11747 : removed usage of Guava's ComparisonChain with JDK comparator (#2319)
Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent e2a1539 commit 4485148

File tree

8 files changed

+217
-38
lines changed

8 files changed

+217
-38
lines changed

oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndexTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Calendar;
3232
import java.util.Collection;
3333
import java.util.Collections;
34+
import java.util.Comparator;
3435
import java.util.HashMap;
3536
import java.util.Iterator;
3637
import java.util.LinkedList;
@@ -44,7 +45,6 @@
4445
import javax.jcr.PropertyType;
4546

4647
import org.apache.commons.io.input.CountingInputStream;
47-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
4848
import org.apache.commons.io.FileUtils;
4949
import org.apache.commons.io.IOUtils;
5050
import org.apache.jackrabbit.JcrConstants;
@@ -3264,11 +3264,10 @@ public Tuple2(Comparable value, Comparable value2, String path) {
32643264
}
32653265

32663266
@Override
3267-
public int compareTo(Tuple2 o) {
3268-
return ComparisonChain.start()
3269-
.compare(value, o.value)
3270-
.compare(value2, o.value2, Collections.reverseOrder())
3271-
.result();
3267+
public int compareTo(@NotNull Tuple2 o) {
3268+
return Comparator.comparing((Tuple2 t) -> t.value)
3269+
.thenComparing( t -> ((Tuple2) t).value2, Comparator.reverseOrder())
3270+
.compare(this, o);
32723271
}
32733272

32743273
@Override

oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/BinaryStats.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
import java.io.StringWriter;
2525
import java.util.ArrayList;
2626
import java.util.Collections;
27+
import java.util.Comparator;
2728
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031

31-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
3232
import org.codehaus.groovy.runtime.StringGroovyMethods;
33+
import org.jetbrains.annotations.NotNull;
3334

3435
import static org.apache.jackrabbit.oak.commons.IOUtils.humanReadableByteCount;
3536

@@ -131,7 +132,7 @@ private String getSummary(List<MimeTypeStats> stats) {
131132
return sw.toString();
132133
}
133134

134-
private MimeTypeStats createStat(String mimeType) {
135+
MimeTypeStats createStat(String mimeType) {
135136
MimeTypeStats stats = new MimeTypeStats(mimeType);
136137
stats.setIndexed(tika.isIndexed(mimeType));
137138
stats.setSupported(tika.isSupportedMediaType(mimeType));
@@ -142,7 +143,7 @@ private static String center(String s, int width) {
142143
return StringGroovyMethods.center(s, width);
143144
}
144145

145-
private static class MimeTypeStats implements Comparable<MimeTypeStats> {
146+
static class MimeTypeStats implements Comparable<MimeTypeStats> {
146147
private final String mimeType;
147148
private int count;
148149
private long totalSize;
@@ -187,11 +188,11 @@ public boolean isSupported() {
187188
}
188189

189190
@Override
190-
public int compareTo(MimeTypeStats o) {
191-
return ComparisonChain.start()
192-
.compareFalseFirst(indexed, o.indexed)
193-
.compare(totalSize, o.totalSize)
194-
.result();
191+
public int compareTo(@NotNull MimeTypeStats o) {
192+
return Comparator
193+
.comparing(MimeTypeStats::isIndexed) // false comes before true by default
194+
.thenComparingLong(MimeTypeStats::getTotalSize) // then compare by totalSize
195+
.compare(this, o);
195196
}
196197
}
197198
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.jackrabbit.oak.plugins.tika;
20+
21+
import org.apache.jackrabbit.guava.common.collect.FluentIterable;
22+
import org.apache.jackrabbit.oak.plugins.tika.BinaryStats.MimeTypeStats;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
30+
31+
public class BinaryStatsTest {
32+
33+
@Test
34+
public void testMimeTypeStatsComparison() throws IOException {
35+
// Create BinaryStats instance with a minimal implementation of BinaryResourceProvider
36+
BinaryStats stats = new BinaryStats(null, path -> FluentIterable.of());
37+
38+
// Create test MimeTypeStats instances
39+
MimeTypeStats indexedLargeSize = stats.createStat("application/pdf");
40+
indexedLargeSize.setIndexed(true);
41+
indexedLargeSize.addSize(1000);
42+
43+
MimeTypeStats indexedSmallSize = stats.createStat("text/plain");
44+
indexedSmallSize.setIndexed(true);
45+
indexedSmallSize.addSize(100);
46+
47+
MimeTypeStats notIndexedLargeSize = stats.createStat("image/png");
48+
notIndexedLargeSize.setIndexed(false);
49+
notIndexedLargeSize.addSize(2000);
50+
51+
MimeTypeStats notIndexedSmallSize = stats.createStat("audio/mp3");
52+
notIndexedSmallSize.setIndexed(false);
53+
notIndexedSmallSize.addSize(500);
54+
55+
// Test case 1: Compare by indexed status (false first, then true)
56+
Assert.assertTrue(notIndexedLargeSize.compareTo(indexedLargeSize) < 0);
57+
Assert.assertTrue(indexedLargeSize.compareTo(notIndexedLargeSize) > 0);
58+
59+
// Test case 2: Same indexed status, compare by size (larger comes first)
60+
Assert.assertTrue(indexedLargeSize.compareTo(indexedSmallSize) > 0);
61+
Assert.assertTrue(notIndexedLargeSize.compareTo(notIndexedSmallSize) > 0);
62+
63+
// Test case 3: Sort a list and verify ordering
64+
List<MimeTypeStats> statsList = new ArrayList<>();
65+
statsList.add(indexedLargeSize);
66+
statsList.add(notIndexedSmallSize);
67+
statsList.add(indexedSmallSize);
68+
statsList.add(notIndexedLargeSize);
69+
70+
Collections.sort(statsList);
71+
72+
// Verify sort order: not indexed first (larger to smaller), then indexed (larger to smaller)
73+
Assert.assertSame(notIndexedSmallSize, statsList.get(0));
74+
Assert.assertSame(notIndexedLargeSize, statsList.get(1));
75+
Assert.assertSame(indexedSmallSize, statsList.get(2));
76+
Assert.assertSame(indexedLargeSize, statsList.get(3));
77+
}
78+
}

oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/MapEntry.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Comparator;
2525
import java.util.Map;
2626

27-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
2827
import org.apache.jackrabbit.oak.commons.conditions.Validate;
2928
import org.apache.jackrabbit.oak.spi.state.AbstractChildNodeEntry;
3029
import org.jetbrains.annotations.NotNull;
@@ -149,11 +148,10 @@ public RecordId setValue(RecordId value) {
149148

150149
@Override
151150
public int compareTo(@NotNull MapEntry that) {
152-
return ComparisonChain.start()
153-
.compare(getHash() & HASH_MASK, that.getHash() & HASH_MASK)
154-
.compare(name, that.name)
155-
.compare(value, that.value, Comparator.nullsLast(Comparator.naturalOrder()))
156-
.result();
151+
return Comparator.comparingLong((MapEntry me) -> me.getHash() & HASH_MASK)
152+
.thenComparing(MapEntry::getName)
153+
.thenComparing(MapEntry::getValue, Comparator.nullsLast(Comparator.naturalOrder()))
154+
.compare(this, that);
157155
}
158156

159157
}

oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/MapRecord.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import java.util.ArrayList;
2828
import java.util.Arrays;
2929
import java.util.Collections;
30+
import java.util.Comparator;
3031
import java.util.Iterator;
3132
import java.util.List;
3233
import java.util.Objects;
3334

34-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
3535
import org.apache.jackrabbit.oak.commons.collections.IterableUtils;
3636
import org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff;
3737
import org.apache.jackrabbit.oak.spi.state.NodeState;
@@ -631,10 +631,9 @@ private static int compare(MapEntry before, MapEntry after) {
631631
} else if (after == null) {
632632
return -1; // see above
633633
} else {
634-
return ComparisonChain.start()
635-
.compare(before.getHash() & HASH_MASK, after.getHash() & HASH_MASK)
636-
.compare(before.getName(), after.getName())
637-
.result();
634+
return Comparator.comparingLong((MapEntry me) -> me.getHash() & HASH_MASK)
635+
.thenComparing(MapEntry::getName)
636+
.compare(before, after);
638637
}
639638
}
640639

oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/PropertyTemplate.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.jackrabbit.oak.commons.StringUtils;
2727
import org.jetbrains.annotations.NotNull;
2828

29-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
29+
import java.util.Comparator;
3030

3131
/**
3232
* A property definition within a template (the property name, the type, and the
@@ -74,11 +74,10 @@ public Type<?> getType() {
7474
@Override
7575
public int compareTo(@NotNull PropertyTemplate template) {
7676
requireNonNull(template);
77-
return ComparisonChain.start()
78-
.compare(hashCode(), template.hashCode()) // important
79-
.compare(name, template.name)
80-
.compare(type, template.type)
81-
.result();
77+
return Comparator.comparingInt(PropertyTemplate::hashCode)
78+
.thenComparing(PropertyTemplate::getName)
79+
.thenComparing(PropertyTemplate::getType)
80+
.compare(this, template);
8281
}
8382

8483
//------------------------------------------------------------< Object >--
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.jackrabbit.oak.segment;
20+
21+
import org.apache.jackrabbit.oak.api.Type;
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
import java.util.ArrayList;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class PropertyTemplateTest {
30+
31+
@Test
32+
public void testCompareToWithDifferentNames() {
33+
PropertyTemplate p1 = new PropertyTemplate(0, "aName", Type.STRING);
34+
PropertyTemplate p2 = new PropertyTemplate(1, "bName", Type.STRING);
35+
36+
// Since hashCode is used for primary comparison and hashCode uses only name,
37+
// templates with different names should be ordered by name's hashCode
38+
Assert.assertTrue(p1.hashCode() < p2.hashCode());
39+
Assert.assertTrue(p1.compareTo(p2) < 0);
40+
Assert.assertTrue(p2.compareTo(p1) > 0);
41+
}
42+
43+
@Test
44+
public void testCompareToWithSameNamesDifferentTypes() {
45+
PropertyTemplate p1 = new PropertyTemplate(0, "sameName", Type.STRING);
46+
PropertyTemplate p2 = new PropertyTemplate(0, "sameName", Type.BOOLEAN);
47+
48+
// Same name (so same hashCode), different types
49+
Assert.assertEquals(p1.hashCode(), p2.hashCode());
50+
// Type comparison is determined by the Type enum's natural order
51+
Assert.assertNotEquals(0, p1.compareTo(p2));
52+
}
53+
54+
@Test
55+
public void testCompareToWithSameNamesSameTypesDifferentIndices() {
56+
PropertyTemplate p1 = new PropertyTemplate(0, "sameName", Type.STRING);
57+
PropertyTemplate p2 = new PropertyTemplate(1, "sameName", Type.STRING);
58+
59+
// Same name and type, different indices
60+
Assert.assertEquals(p1.hashCode(), p2.hashCode());
61+
Assert.assertEquals(0, p1.compareTo(p2)); // Index is not used in comparison
62+
Assert.assertEquals(p1, p2); // Index is not used in equals either
63+
}
64+
65+
@Test
66+
public void testConsistencyBetweenEqualsAndCompareTo() {
67+
PropertyTemplate p1 = new PropertyTemplate(0, "name", Type.STRING);
68+
PropertyTemplate p2 = new PropertyTemplate(0, "name", Type.STRING);
69+
PropertyTemplate p3 = new PropertyTemplate(0, "name", Type.BOOLEAN);
70+
71+
// Basic equality check
72+
Assert.assertEquals(p1, p1); // Reflexivity
73+
Assert.assertEquals(p1, p2); // Symmetry
74+
Assert.assertEquals(0, p1.compareTo(p2));
75+
76+
// Different type
77+
Assert.assertNotEquals(p1, p3);
78+
Assert.assertNotEquals(0, p1.compareTo(p3));
79+
80+
// Not a PropertyTemplate
81+
Assert.assertNotEquals("not a template", p1);
82+
}
83+
84+
@Test
85+
public void testSortingBehavior() {
86+
List<PropertyTemplate> templates = new ArrayList<>();
87+
88+
// Create templates with varying names and types
89+
templates.add(new PropertyTemplate(0, "c", Type.STRING));
90+
templates.add(new PropertyTemplate(1, "a", Type.STRING));
91+
templates.add(new PropertyTemplate(2, "b", Type.STRING));
92+
templates.add(new PropertyTemplate(3, "a", Type.BOOLEAN));
93+
94+
Collections.sort(templates);
95+
96+
// Check the expected sort order based on hashCode, then name, then type
97+
Assert.assertEquals("a", templates.get(0).getName()); // 'a' has lowest hashCode
98+
// BOOLEAN comes after STRING for same name 'a'
99+
// since they are compared by Type enum tag value (javax.jcr.PropertyType) which is higher for BOOLEAN
100+
Assert.assertEquals(Type.STRING, templates.get(0).getType());
101+
Assert.assertEquals("a", templates.get(1).getName());
102+
Assert.assertEquals(Type.BOOLEAN, templates.get(1).getType());
103+
Assert.assertEquals("b", templates.get(2).getName());
104+
Assert.assertEquals("c", templates.get(3).getName());
105+
}
106+
107+
}

oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/FormatVersion.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package org.apache.jackrabbit.oak.plugins.document;
1818

1919
import java.util.ArrayList;
20+
import java.util.Comparator;
2021
import java.util.List;
2122

22-
import org.apache.jackrabbit.guava.common.collect.ComparisonChain;
23-
2423
import static java.util.Objects.requireNonNull;
2524
import static org.apache.jackrabbit.oak.plugins.document.Collection.SETTINGS;
2625

@@ -259,11 +258,10 @@ public boolean equals(Object obj) {
259258
@Override
260259
public int compareTo(@NotNull FormatVersion other) {
261260
requireNonNull(other);
262-
return ComparisonChain.start()
263-
.compare(major, other.major)
264-
.compare(minor, other.minor)
265-
.compare(micro, other.micro)
266-
.result();
261+
return Comparator.comparingInt((FormatVersion fv) -> fv.major)
262+
.thenComparingInt(fv -> fv.minor)
263+
.thenComparingInt(fv -> fv.micro)
264+
.compare(this, other);
267265
}
268266

269267
private static DocumentStoreException concurrentUpdate() {

0 commit comments

Comments
 (0)