Skip to content

Commit 1a93941

Browse files
authored
Misc code cleanups (#12974)
1 parent 248f067 commit 1a93941

File tree

4 files changed

+45
-70
lines changed

4 files changed

+45
-70
lines changed

lucene/analysis/kuromoji/src/test/org/apache/lucene/analysis/ja/TestSearchMode.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
*/
1717
package org.apache.lucene.analysis.ja;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
1921
import java.io.FileNotFoundException;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.io.InputStreamReader;
2325
import java.io.LineNumberReader;
24-
import java.nio.charset.StandardCharsets;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728
import org.apache.lucene.analysis.Analyzer;
@@ -70,10 +71,9 @@ public void testSearchSegmentation() throws IOException {
7071
throw new FileNotFoundException(
7172
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
7273
}
73-
try {
74-
LineNumberReader reader =
75-
new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
76-
String line = null;
74+
try (is) {
75+
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
76+
String line;
7777
while ((line = reader.readLine()) != null) {
7878
// Remove comments
7979
line = line.replaceAll("#.*$", "");
@@ -98,10 +98,16 @@ public void testSearchSegmentation() throws IOException {
9898
expectedPosLengths[tokIDX] = 1;
9999
}
100100
}
101-
assertAnalyzesTo(analyzer, sourceText, expectedTokens, expectedPosIncrs);
101+
assertAnalyzesTo(
102+
analyzer,
103+
sourceText,
104+
expectedTokens,
105+
null,
106+
null,
107+
null,
108+
expectedPosIncrs,
109+
expectedPosLengths);
102110
}
103-
} finally {
104-
is.close();
105111
}
106112
}
107113

@@ -111,10 +117,9 @@ public void testSearchSegmentationNoOriginal() throws IOException {
111117
throw new FileNotFoundException(
112118
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
113119
}
114-
try {
115-
LineNumberReader reader =
116-
new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
117-
String line = null;
120+
try (is) {
121+
LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
122+
String line;
118123
while ((line = reader.readLine()) != null) {
119124
// Remove comments
120125
line = line.replaceAll("#.*$", "");
@@ -130,9 +135,9 @@ public void testSearchSegmentationNoOriginal() throws IOException {
130135
String[] tmpExpectedTokens = fields[1].split("\\s+");
131136

132137
List<String> expectedTokenList = new ArrayList<>();
133-
for (int tokIDX = 0; tokIDX < tmpExpectedTokens.length; tokIDX++) {
134-
if (!tmpExpectedTokens[tokIDX].endsWith("/0")) {
135-
expectedTokenList.add(tmpExpectedTokens[tokIDX]);
138+
for (String expectedToken : tmpExpectedTokens) {
139+
if (!expectedToken.endsWith("/0")) {
140+
expectedTokenList.add(expectedToken);
136141
}
137142
}
138143

@@ -145,11 +150,13 @@ public void testSearchSegmentationNoOriginal() throws IOException {
145150
assertAnalyzesTo(
146151
analyzerNoOriginal,
147152
sourceText,
148-
expectedTokenList.toArray(new String[expectedTokenList.size()]),
149-
expectedPosIncrs);
153+
expectedTokenList.toArray(new String[0]),
154+
null,
155+
null,
156+
null,
157+
expectedPosIncrs,
158+
expectedPosLengths);
150159
}
151-
} finally {
152-
is.close();
153160
}
154161
}
155162
}

lucene/core/src/java/org/apache/lucene/codecs/lucene99/PForUtil.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ final class PForUtil {
2828

2929
private static final int MAX_EXCEPTIONS = 7;
3030

31-
// IDENTITY_PLUS_ONE[i] == i + 1
32-
private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];
33-
34-
static {
35-
for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
36-
IDENTITY_PLUS_ONE[i] = i + 1;
37-
}
38-
}
39-
4031
static boolean allEqual(long[] l) {
4132
for (int i = 1; i < ForUtil.BLOCK_SIZE; ++i) {
4233
if (l[i] != l[0]) {

lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
public class TestDirectoryReader extends LuceneTestCase {
5656

5757
public void testDocument() throws IOException {
58-
SegmentReader[] readers = new SegmentReader[2];
5958
Directory dir = newDirectory();
6059
Document doc1 = new Document();
6160
Document doc2 = new Document();
@@ -80,8 +79,6 @@ public void testDocument() throws IOException {
8079
assertNotNull(vector);
8180

8281
reader.close();
83-
if (readers[0] != null) readers[0].close();
84-
if (readers[1] != null) readers[1].close();
8582
dir.close();
8683
}
8784

@@ -482,11 +479,8 @@ public void testFilesOpenClose() throws IOException {
482479

483480
public void testOpenReaderAfterDelete() throws IOException {
484481
Path dirFile = createTempDir("deletetest");
485-
Directory dir = newFSDirectory(dirFile);
486-
if (dir instanceof BaseDirectoryWrapper) {
487-
((BaseDirectoryWrapper) dir)
488-
.setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
489-
}
482+
BaseDirectoryWrapper dir = newFSDirectory(dirFile);
483+
dir.setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
490484
expectThrowsAnyOf(
491485
Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
492486
() -> DirectoryReader.open(dir));
@@ -702,7 +696,7 @@ public void testGetIndexCommit() throws IOException {
702696

703697
assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());
704698

705-
assertTrue(c.equals(r.getIndexCommit()));
699+
assertEquals(c, r.getIndexCommit());
706700

707701
// Change the index
708702
writer =
@@ -717,8 +711,8 @@ public void testGetIndexCommit() throws IOException {
717711

718712
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
719713
assertNotNull(r2);
720-
assertFalse(c.equals(r2.getIndexCommit()));
721-
assertFalse(r2.getIndexCommit().getSegmentCount() == 1);
714+
assertNotEquals(c, r2.getIndexCommit());
715+
assertNotEquals(1, r2.getIndexCommit().getSegmentCount());
722716
r2.close();
723717

724718
writer =
@@ -753,11 +747,7 @@ static Document createDocument(String id) {
753747
public void testNoDir() throws Throwable {
754748
Path tempDir = createTempDir("doesnotexist");
755749
Directory dir = newFSDirectory(tempDir);
756-
expectThrows(
757-
IndexNotFoundException.class,
758-
() -> {
759-
DirectoryReader.open(dir);
760-
});
750+
expectThrows(IndexNotFoundException.class, () -> DirectoryReader.open(dir));
761751
dir.close();
762752
}
763753

@@ -779,7 +769,7 @@ public void testNoDupCommitFileNames() throws Throwable {
779769
Collection<String> files = commit.getFileNames();
780770
HashSet<String> seen = new HashSet<>();
781771
for (final String fileName : files) {
782-
assertTrue("file " + fileName + " was duplicated", !seen.contains(fileName));
772+
assertFalse("file " + fileName + " was duplicated", seen.contains(fileName));
783773
seen.add(fileName);
784774
}
785775
}
@@ -970,13 +960,7 @@ public void testReaderFinishedListener() throws Exception {
970960
writer.commit();
971961
final DirectoryReader reader = DirectoryReader.open(writer);
972962
final int[] closeCount = new int[1];
973-
final IndexReader.ClosedListener listener =
974-
new IndexReader.ClosedListener() {
975-
@Override
976-
public void onClose(IndexReader.CacheKey key) {
977-
closeCount[0]++;
978-
}
979-
};
963+
final IndexReader.ClosedListener listener = key -> closeCount[0]++;
980964

981965
reader.getReaderCacheHelper().addClosedListener(listener);
982966

@@ -1002,11 +986,7 @@ public void testOOBDocID() throws Exception {
1002986
DirectoryReader r = DirectoryReader.open(writer);
1003987
writer.close();
1004988
r.storedFields().document(0);
1005-
expectThrows(
1006-
IllegalArgumentException.class,
1007-
() -> {
1008-
r.storedFields().document(1);
1009-
});
989+
expectThrows(IllegalArgumentException.class, () -> r.storedFields().document(1));
1010990
r.close();
1011991
dir.close();
1012992
}
@@ -1044,9 +1024,9 @@ public void testStressTryIncRef() throws IOException, InterruptedException {
10441024
r.decRef();
10451025
r.close();
10461026

1047-
for (int i = 0; i < threads.length; i++) {
1048-
threads[i].join();
1049-
assertNull(threads[i].failed);
1027+
for (IncThread thread : threads) {
1028+
thread.join();
1029+
assertNull(thread.failed);
10501030
}
10511031
assertFalse(r.tryIncRef());
10521032
writer.close();

lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseStoredFieldsFormatTestCase.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.Random;
3333
import java.util.concurrent.atomic.AtomicReference;
3434
import java.util.function.Function;
35-
import java.util.stream.Collectors;
3635
import org.apache.lucene.codecs.Codec;
3736
import org.apache.lucene.codecs.StoredFieldsFormat;
3837
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
@@ -136,12 +135,9 @@ public void testRandomStoredFields() throws IOException {
136135
}
137136

138137
for (int field : fieldIDs) {
139-
final String s;
140138
if (rand.nextInt(4) != 3) {
141-
s = TestUtil.randomUnicodeString(rand, 1000);
139+
final String s = TestUtil.randomUnicodeString(rand, 1000);
142140
doc.add(newField("f" + field, s, customType2));
143-
} else {
144-
s = null;
145141
}
146142
}
147143
w.addDocument(doc);
@@ -163,7 +159,7 @@ public void testRandomStoredFields() throws IOException {
163159
System.out.println("TEST: " + docs.size() + " docs in index; now load fields");
164160
}
165161
if (docs.size() > 0) {
166-
String[] idsList = docs.keySet().toArray(new String[docs.size()]);
162+
String[] idsList = docs.keySet().toArray(new String[0]);
167163

168164
for (int x = 0; x < 2; x++) {
169165
DirectoryReader r = maybeWrapWithMergingReader(w.getReader());
@@ -326,6 +322,7 @@ public void testNumericField() throws Exception {
326322
assertTrue("got f=" + f, f instanceof StoredField);
327323
assertEquals(docID, ids.nextDoc());
328324
assertEquals(answers[(int) ids.longValue()], f.numericValue());
325+
assertEquals(typeAnswers[(int) ids.longValue()], f.numericValue().getClass());
329326
}
330327
}
331328
r.close();
@@ -458,7 +455,7 @@ public void testConcurrentReads() throws Exception {
458455
readThreads.add(
459456
new Thread() {
460457

461-
int[] queries;
458+
final int[] queries;
462459

463460
{
464461
queries = new int[readsPerThread];
@@ -729,7 +726,7 @@ public void testBigDocuments() throws IOException {
729726
assumeWorkingMMapOnWindows();
730727

731728
// "big" as "much bigger than the chunk size"
732-
// for this test we force a FS dir
729+
// for this test we force an FS dir
733730
// we can't just use newFSDirectory, because this test doesn't really index anything.
734731
// so if we get NRTCachingDir+SimpleText, we make massive stored fields and OOM (LUCENE-4484)
735732
Directory dir =
@@ -952,7 +949,7 @@ public void testRandomStoredFieldsWithIndexSort() throws Exception {
952949
List<IndexableField> expectedFields =
953950
docs.get(testID).getFields().stream()
954951
.filter(f -> f.fieldType().stored())
955-
.collect(Collectors.toList());
952+
.toList();
956953
Document actualDoc = actualStoredFields.document(hits.scoreDocs[0].doc);
957954
assertEquals(expectedFields.size(), actualDoc.getFields().size());
958955
for (IndexableField expectedField : expectedFields) {
@@ -1010,7 +1007,7 @@ public void testRandomStoredFieldsWithIndexSort() throws Exception {
10101007

10111008
/** Test realistic data, which typically compresses better than random data. */
10121009
public void testLineFileDocs() throws IOException {
1013-
// Use a FS dir and a non-randomized IWC to not slow down indexing
1010+
// Use an FS dir and a non-randomized IWC to not slow down indexing
10141011
try (Directory dir = newFSDirectory(createTempDir())) {
10151012
try (LineFileDocs docs = new LineFileDocs(random());
10161013
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {

0 commit comments

Comments
 (0)