Skip to content

Apache Lucene open-source search software

Misc code cleanups (#12974)

sabi01a939410dd23
diff --git a/lucene/analysis/kuromoji/src/test/org/apache/lucene/analysis/ja/TestSearchMode.java b/lucene/analysis/kuromoji/src/test/org/apache/lucene/analysis/ja/TestSearchMode.java
index afe7dca1c76..9ded025c9e9 100644
--- a/lucene/analysis/kuromoji/src/test/org/apache/lucene/analysis/ja/TestSearchMode.java
+++ b/lucene/analysis/kuromoji/src/test/org/apache/lucene/analysis/ja/TestSearchMode.java
@@ -16,12 +16,13 @@
*/
package org.apache.lucene.analysis.ja;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
-import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
@@ -70,10 +71,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
throw new FileNotFoundException(
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
}
- try {
- LineNumberReader reader =
- new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
- String line = null;
+ try (is) {
+ LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
+ String line;
while ((line = reader.readLine()) != null) {
// Remove comments
line = line.replaceAll("#.*$", "");
@@ -98,10 +98,16 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
expectedPosLengths[tokIDX] = 1;
}
}
- assertAnalyzesTo(analyzer, sourceText, expectedTokens, expectedPosIncrs);
+ assertAnalyzesTo(
+ analyzer,
+ sourceText,
+ expectedTokens,
+ null,
+ null,
+ null,
+ expectedPosIncrs,
+ expectedPosLengths);
}
- } finally {
- is.close();
}
}
@@ -111,10 +117,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
throw new FileNotFoundException(
"Cannot find " + SEGMENTATION_FILENAME + " in test classpath");
}
- try {
- LineNumberReader reader =
- new LineNumberReader(new InputStreamReader(is, StandardCharsets.UTF_8));
- String line = null;
+ try (is) {
+ LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, UTF_8));
+ String line;
while ((line = reader.readLine()) != null) {
// Remove comments
line = line.replaceAll("#.*$", "");
@@ -130,9 +135,9 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
String[] tmpExpectedTokens = fields[1].split("\\s+");
List<String> expectedTokenList = new ArrayList<>();
- for (int tokIDX = 0; tokIDX < tmpExpectedTokens.length; tokIDX++) {
- if (!tmpExpectedTokens[tokIDX].endsWith("/0")) {
- expectedTokenList.add(tmpExpectedTokens[tokIDX]);
+ for (String expectedToken : tmpExpectedTokens) {
+ if (!expectedToken.endsWith("/0")) {
+ expectedTokenList.add(expectedToken);
}
}
@@ -145,11 +150,13 @@ public class TestSearchMode extends BaseTokenStreamTestCase {
assertAnalyzesTo(
analyzerNoOriginal,
sourceText,
- expectedTokenList.toArray(new String[expectedTokenList.size()]),
- expectedPosIncrs);
+ expectedTokenList.toArray(new String[0]),
+ null,
+ null,
+ null,
+ expectedPosIncrs,
+ expectedPosLengths);
}
- } finally {
- is.close();
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/PForUtil.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/PForUtil.java
index 93ebb3d0f9d..de62fb6b40b 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/PForUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/PForUtil.java
@@ -28,15 +28,6 @@ final class PForUtil {
private static final int MAX_EXCEPTIONS = 7;
- // IDENTITY_PLUS_ONE[i] == i + 1
- private static final long[] IDENTITY_PLUS_ONE = new long[ForUtil.BLOCK_SIZE];
-
- static {
- for (int i = 0; i < ForUtil.BLOCK_SIZE; ++i) {
- IDENTITY_PLUS_ONE[i] = i + 1;
- }
- }
-
static boolean allEqual(long[] l) {
for (int i = 1; i < ForUtil.BLOCK_SIZE; ++i) {
if (l[i] != l[0]) {
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
index d0c16b2667b..38f356d012c 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java
@@ -55,7 +55,6 @@ import org.junit.Assume;
public class TestDirectoryReader extends LuceneTestCase {
public void testDocument() throws IOException {
- SegmentReader[] readers = new SegmentReader[2];
Directory dir = newDirectory();
Document doc1 = new Document();
Document doc2 = new Document();
@@ -80,8 +79,6 @@ public class TestDirectoryReader extends LuceneTestCase {
assertNotNull(vector);
reader.close();
- if (readers[0] != null) readers[0].close();
- if (readers[1] != null) readers[1].close();
dir.close();
}
@@ -482,11 +479,8 @@ public class TestDirectoryReader extends LuceneTestCase {
public void testOpenReaderAfterDelete() throws IOException {
Path dirFile = createTempDir("deletetest");
- Directory dir = newFSDirectory(dirFile);
- if (dir instanceof BaseDirectoryWrapper) {
- ((BaseDirectoryWrapper) dir)
- .setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
- }
+ BaseDirectoryWrapper dir = newFSDirectory(dirFile);
+ dir.setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
expectThrowsAnyOf(
Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
() -> DirectoryReader.open(dir));
@@ -702,7 +696,7 @@ public class TestDirectoryReader extends LuceneTestCase {
assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());
- assertTrue(c.equals(r.getIndexCommit()));
+ assertEquals(c, r.getIndexCommit());
// Change the index
writer =
@@ -717,8 +711,8 @@ public class TestDirectoryReader extends LuceneTestCase {
DirectoryReader r2 = DirectoryReader.openIfChanged(r);
assertNotNull(r2);
- assertFalse(c.equals(r2.getIndexCommit()));
- assertFalse(r2.getIndexCommit().getSegmentCount() == 1);
+ assertNotEquals(c, r2.getIndexCommit());
+ assertNotEquals(1, r2.getIndexCommit().getSegmentCount());
r2.close();
writer =
@@ -753,11 +747,7 @@ public class TestDirectoryReader extends LuceneTestCase {
public void testNoDir() throws Throwable {
Path tempDir = createTempDir("doesnotexist");
Directory dir = newFSDirectory(tempDir);
- expectThrows(
- IndexNotFoundException.class,
- () -> {
- DirectoryReader.open(dir);
- });
+ expectThrows(IndexNotFoundException.class, () -> DirectoryReader.open(dir));
dir.close();
}
@@ -779,7 +769,7 @@ public class TestDirectoryReader extends LuceneTestCase {
Collection<String> files = commit.getFileNames();
HashSet<String> seen = new HashSet<>();
for (final String fileName : files) {
- assertTrue("file " + fileName + " was duplicated", !seen.contains(fileName));
+ assertFalse("file " + fileName + " was duplicated", seen.contains(fileName));
seen.add(fileName);
}
}
@@ -970,13 +960,7 @@ public class TestDirectoryReader extends LuceneTestCase {
writer.commit();
final DirectoryReader reader = DirectoryReader.open(writer);
final int[] closeCount = new int[1];
- final IndexReader.ClosedListener listener =
- new IndexReader.ClosedListener() {
- @Override
- public void onClose(IndexReader.CacheKey key) {
- closeCount[0]++;
- }
- };
+ final IndexReader.ClosedListener listener = key -> closeCount[0]++;
reader.getReaderCacheHelper().addClosedListener(listener);
@@ -1002,11 +986,7 @@ public class TestDirectoryReader extends LuceneTestCase {
DirectoryReader r = DirectoryReader.open(writer);
writer.close();
r.storedFields().document(0);
- expectThrows(
- IllegalArgumentException.class,
- () -> {
- r.storedFields().document(1);
- });
+ expectThrows(IllegalArgumentException.class, () -> r.storedFields().document(1));
r.close();
dir.close();
}
@@ -1044,9 +1024,9 @@ public class TestDirectoryReader extends LuceneTestCase {
r.decRef();
r.close();
- for (int i = 0; i < threads.length; i++) {
- threads[i].join();
- assertNull(threads[i].failed);
+ for (IncThread thread : threads) {
+ thread.join();
+ assertNull(thread.failed);
}
assertFalse(r.tryIncRef());
writer.close();
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseStoredFieldsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseStoredFieldsFormatTestCase.java
index 761cf9b7387..efd351c21fe 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseStoredFieldsFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseStoredFieldsFormatTestCase.java
@@ -32,7 +32,6 @@ import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
-import java.util.stream.Collectors;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.StoredFieldsFormat;
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
@@ -136,12 +135,9 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
}
for (int field : fieldIDs) {
- final String s;
if (rand.nextInt(4) != 3) {
- s = TestUtil.randomUnicodeString(rand, 1000);
+ final String s = TestUtil.randomUnicodeString(rand, 1000);
doc.add(newField("f" + field, s, customType2));
- } else {
- s = null;
}
}
w.addDocument(doc);
@@ -163,7 +159,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
System.out.println("TEST: " + docs.size() + " docs in index; now load fields");
}
if (docs.size() > 0) {
- String[] idsList = docs.keySet().toArray(new String[docs.size()]);
+ String[] idsList = docs.keySet().toArray(new String[0]);
for (int x = 0; x < 2; x++) {
DirectoryReader r = maybeWrapWithMergingReader(w.getReader());
@@ -326,6 +322,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
assertTrue("got f=" + f, f instanceof StoredField);
assertEquals(docID, ids.nextDoc());
assertEquals(answers[(int) ids.longValue()], f.numericValue());
+ assertEquals(typeAnswers[(int) ids.longValue()], f.numericValue().getClass());
}
}
r.close();
@@ -458,7 +455,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
readThreads.add(
new Thread() {
- int[] queries;
+ final int[] queries;
{
queries = new int[readsPerThread];
@@ -729,7 +726,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
assumeWorkingMMapOnWindows();
// "big" as "much bigger than the chunk size"
- // for this test we force a FS dir
+ // for this test we force an FS dir
// we can't just use newFSDirectory, because this test doesn't really index anything.
// so if we get NRTCachingDir+SimpleText, we make massive stored fields and OOM (LUCENE-4484)
Directory dir =
@@ -952,7 +949,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
List<IndexableField> expectedFields =
docs.get(testID).getFields().stream()
.filter(f -> f.fieldType().stored())
- .collect(Collectors.toList());
+ .toList();
Document actualDoc = actualStoredFields.document(hits.scoreDocs[0].doc);
assertEquals(expectedFields.size(), actualDoc.getFields().size());
for (IndexableField expectedField : expectedFields) {
@@ -1010,7 +1007,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormat
/** Test realistic data, which typically compresses better than random data. */
public void testLineFileDocs() throws IOException {
- // Use a FS dir and a non-randomized IWC to not slow down indexing
+ // Use an FS dir and a non-randomized IWC to not slow down indexing
try (Directory dir = newFSDirectory(createTempDir())) {
try (LineFileDocs docs = new LineFileDocs(random());
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {