Apache Lucene open-source search software
Use Collections.addAll() instead of manual array copy and misc. code cleanups (#12977)
sabi07b8aece125aa
diff --git a/lucene/analysis/morfologik/src/test/org/apache/lucene/analysis/morfologik/TestMorfologikAnalyzer.java b/lucene/analysis/morfologik/src/test/org/apache/lucene/analysis/morfologik/TestMorfologikAnalyzer.javaindex 6e66771b760..616c6274c4a 100644--- a/lucene/analysis/morfologik/src/test/org/apache/lucene/analysis/morfologik/TestMorfologikAnalyzer.java+++ b/lucene/analysis/morfologik/src/test/org/apache/lucene/analysis/morfologik/TestMorfologikAnalyzer.java@@ -17,6 +17,7 @@package org.apache.lucene.analysis.morfologik;import java.io.IOException;+import java.util.Collections;import java.util.TreeSet;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.CharArraySet;@@ -127,9 +128,7 @@ public class TestMorfologikAnalyzer extends BaseTokenStreamTestCase {for (StringBuilder b : ts.getAttribute(MorphosyntacticTagsAttribute.class).getTags()) {actual.add(b.toString());}- for (String s : tags) {- expected.add(s);- }+ Collections.addAll(expected, tags);if (!expected.equals(actual)) {System.out.println("Expected:\n" + expected);diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.javaindex 85da41462f8..29510203f16 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java@@ -21,6 +21,7 @@ import java.io.StringReader;import java.lang.reflect.Constructor;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collections;import java.util.Locale;import org.apache.lucene.benchmark.byTask.PerfRunData;import org.apache.lucene.benchmark.byTask.tasks.PerfTask;@@ -77,8 +78,8 @@ public class Algorithm implements AutoCloseable {if (task instanceof RepSumByPrefTask) {stok.nextToken();String prefix = stok.sval;- if (prefix == null || prefix.length() == 0) {- throw new Exception("named report prefix problem - " + stok.toString());+ if (prefix == null || prefix.isEmpty()) {+ throw new Exception("named report prefix problem - " + stok);}((RepSumByPrefTask) task).setPrefix(prefix);}@@ -108,7 +109,7 @@ public class Algorithm implements AutoCloseable {}case StreamTokenizer.TT_EOF:{- throw new RuntimeException("Unexpexted EOF: - " + stok.toString());+ throw new RuntimeException("Unexpected EOF: - " + stok);}case '"':case '\'':@@ -160,7 +161,7 @@ public class Algorithm implements AutoCloseable {switch (c) {case ':':- if (!colonOk) throw new Exception("colon unexpexted: - " + stok.toString());+ if (!colonOk) throw new Exception("colon unexpected: - " + stok);colonOk = false;// get repetitions numberstok.nextToken();@@ -168,7 +169,7 @@ public class Algorithm implements AutoCloseable {((TaskSequence) prevTask).setRepetitions(TaskSequence.REPEAT_EXHAUST);} else {if (stok.ttype != StreamTokenizer.TT_NUMBER) {- throw new Exception("expected repetitions number or XXXs: - " + stok.toString());+ throw new Exception("expected repetitions number or XXXs: - " + stok);} else {double num = stok.nval;stok.nextToken();@@ -188,7 +189,7 @@ public class Algorithm implements AutoCloseable {// get rate numberstok.nextToken();if (stok.ttype != StreamTokenizer.TT_NUMBER)- throw new Exception("expected rate number: - " + stok.toString());+ throw new Exception("expected rate number: - " + stok);// check for unit - min or sec, sec is defaultstok.nextToken();if (stok.ttype != '/') {@@ -197,14 +198,14 @@ public class Algorithm implements AutoCloseable {} else {stok.nextToken();if (stok.ttype != StreamTokenizer.TT_WORD)- throw new Exception("expected rate unit: 'min' or 'sec' - " + stok.toString());+ throw new Exception("expected rate unit: 'min' or 'sec' - " + stok);String unit = stok.sval.toLowerCase(Locale.ROOT);if ("min".equals(unit)) {((TaskSequence) prevTask).setRate((int) stok.nval, true); // set rate per min} else if ("sec".equals(unit)) {((TaskSequence) prevTask).setRate((int) stok.nval, false); // set rate per sec} else {- throw new Exception("expected rate unit: 'min' or 'sec' - " + stok.toString());+ throw new Exception("expected rate unit: 'min' or 'sec' - " + stok);}}}@@ -221,8 +222,8 @@ public class Algorithm implements AutoCloseable {stok.pushBack();} else {name = stok.sval;- if (stok.ttype != '"' || name == null || name.length() == 0) {- throw new Exception("sequence name problem - " + stok.toString());+ if (stok.ttype != '"' || name == null || name.isEmpty()) {+ throw new Exception("sequence name problem - " + stok);}}// start the sequence@@ -299,9 +300,7 @@ public class Algorithm implements AutoCloseable {}ArrayList<String> pkgs = new ArrayList<>();pkgs.add(dfltPkg);- for (String alt : alts.split(",")) {- pkgs.add(alt);- }+ Collections.addAll(pkgs, alts.split(","));return pkgs.toArray(new String[0]);}@@ -322,11 +321,7 @@ public class Algorithm implements AutoCloseable {@Overridepublic String toString() {- String newline = System.getProperty("line.separator");- StringBuilder sb = new StringBuilder();- sb.append(sequence.toString());- sb.append(newline);- return sb.toString();+ return sequence.toString() + System.lineSeparator();}/** Execute this algorithm */diff --git a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.javaindex d99b78c3b4b..d32498c1663 100644--- a/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/NRTCachingDirectory.java@@ -20,10 +20,11 @@ import java.io.FileNotFoundException;import java.io.IOException;import java.io.UncheckedIOException;import java.nio.file.NoSuchFileException;-import java.util.Arrays;import java.util.Collection;+import java.util.Collections;import java.util.HashSet;import java.util.Set;+import java.util.TreeSet;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicLong;import org.apache.lucene.util.Accountable;@@ -31,7 +32,7 @@ import org.apache.lucene.util.IOUtils;// TODO// - let subclass dictate policy...?-// - rename to MergeCacheingDir? NRTCachingDir+// - rename to MergeCachingDir? NRTCachingDir/*** Wraps a RAM-resident directory around any provided delegate directory, to be used during NRT@@ -105,16 +106,10 @@ public class NRTCachingDirectory extends FilterDirectory implements Accountable@Overridepublic synchronized String[] listAll() throws IOException {- final Set<String> files = new HashSet<>();- for (String f : cacheDirectory.listAll()) {- files.add(f);- }- for (String f : in.listAll()) {- files.add(f);- }- String[] result = files.toArray(new String[files.size()]);- Arrays.sort(result);- return result;+ final Set<String> files = new TreeSet<>();+ Collections.addAll(files, cacheDirectory.listAll());+ Collections.addAll(files, in.listAll());+ return files.toArray(new String[0]);}@Overridediff --git a/lucene/core/src/test/org/apache/lucene/analysis/TestCharArraySet.java b/lucene/core/src/test/org/apache/lucene/analysis/TestCharArraySet.javaindex 7d432c22c25..dcc5bf2cce7 100644--- a/lucene/core/src/test/org/apache/lucene/analysis/TestCharArraySet.java+++ b/lucene/core/src/test/org/apache/lucene/analysis/TestCharArraySet.java@@ -36,12 +36,12 @@ public class TestCharArraySet extends LuceneTestCase {public void testRehash() throws Exception {CharArraySet cas = new CharArraySet(0, true);- for (int i = 0; i < TEST_STOP_WORDS.length; i++) {- cas.add(TEST_STOP_WORDS[i]);+ for (String stopWord : TEST_STOP_WORDS) {+ cas.add(stopWord);}assertEquals(TEST_STOP_WORDS.length, cas.size());- for (int i = 0; i < TEST_STOP_WORDS.length; i++) {- assertTrue(cas.contains(TEST_STOP_WORDS[i]));+ for (String testStopWord : TEST_STOP_WORDS) {+ assertTrue(cas.contains(testStopWord));}}@@ -80,13 +80,13 @@ public class TestCharArraySet extends LuceneTestCase {assertEquals("Not all words added", TEST_STOP_WORDS.length, set.size());set.clear();assertEquals("not empty", 0, set.size());- for (int i = 0; i < TEST_STOP_WORDS.length; i++) {- assertFalse(set.contains(TEST_STOP_WORDS[i]));+ for (String testStopWord : TEST_STOP_WORDS) {+ assertFalse(set.contains(testStopWord));}set.addAll(Arrays.asList(TEST_STOP_WORDS));assertEquals("Not all words added", TEST_STOP_WORDS.length, set.size());- for (int i = 0; i < TEST_STOP_WORDS.length; i++) {- assertTrue(set.contains(TEST_STOP_WORDS[i]));+ for (String testStopWord : TEST_STOP_WORDS) {+ assertTrue(set.contains(testStopWord));}}@@ -118,7 +118,7 @@ public class TestCharArraySet extends LuceneTestCase {"Test String has been added to unmodifiable set", unmodifiableSet.contains(NOT_IN_SET));assertEquals("Size of unmodifiable set has changed", size, unmodifiableSet.size());- expectThrows(UnsupportedOperationException.class, () -> unmodifiableSet.clear());+ expectThrows(UnsupportedOperationException.class, unmodifiableSet::clear);assertFalse("Changed unmodifiable set", unmodifiableSet.contains(NOT_IN_SET));assertEquals("Size of unmodifiable set has changed", size, unmodifiableSet.size());@@ -149,31 +149,27 @@ public class TestCharArraySet extends LuceneTestCase {assertFalse("Test String has been added to unmodifiable set", unmodifiableSet.contains(NOT_IN_SET));- for (int i = 0; i < TEST_STOP_WORDS.length; i++) {- assertTrue(set.contains(TEST_STOP_WORDS[i]));- assertTrue(unmodifiableSet.contains(TEST_STOP_WORDS[i]));+ for (String testStopWord : TEST_STOP_WORDS) {+ assertTrue(set.contains(testStopWord));+ assertTrue(unmodifiableSet.contains(testStopWord));}}public void testUnmodifiableSet() {CharArraySet set = new CharArraySet(10, true);set.addAll(Arrays.asList(TEST_STOP_WORDS));- set.add(Integer.valueOf(1));+ set.add(1);final int size = set.size();set = CharArraySet.unmodifiableSet(set);assertEquals("Set size changed due to unmodifiableSet call", size, set.size());for (String stopword : TEST_STOP_WORDS) {assertTrue(set.contains(stopword));}- assertTrue(set.contains(Integer.valueOf(1)));+ assertTrue(set.contains(1));assertTrue(set.contains("1"));assertTrue(set.contains(new char[] {'1'}));- expectThrows(- NullPointerException.class,- () -> {- CharArraySet.unmodifiableSet(null);- });+ expectThrows(NullPointerException.class, () -> CharArraySet.unmodifiableSet(null));}public void testSupplementaryChars() {@@ -186,17 +182,13 @@ public class TestCharArraySet extends LuceneTestCase {String[] lowerArr =new String[] {"abc\ud801\udc44", "\ud801\udc44\ud801\udc44cde", "a\ud801\udc44b"};CharArraySet set = new CharArraySet(Arrays.asList(TEST_STOP_WORDS), true);- for (String upper : upperArr) {- set.add(upper);- }+ Collections.addAll(set, upperArr);for (int i = 0; i < upperArr.length; i++) {assertTrue(String.format(Locale.ROOT, missing, upperArr[i]), set.contains(upperArr[i]));assertTrue(String.format(Locale.ROOT, missing, lowerArr[i]), set.contains(lowerArr[i]));}set = new CharArraySet(Arrays.asList(TEST_STOP_WORDS), false);- for (String upper : upperArr) {- set.add(upper);- }+ Collections.addAll(set, upperArr);for (int i = 0; i < upperArr.length; i++) {assertTrue(String.format(Locale.ROOT, missing, upperArr[i]), set.contains(upperArr[i]));assertFalse(String.format(Locale.ROOT, falsePos, lowerArr[i]), set.contains(lowerArr[i]));@@ -212,17 +204,13 @@ public class TestCharArraySet extends LuceneTestCase {String[] lowerArr =new String[] {"abc\uD800", "abc\uD800efg", "\uD800efg", "\uD800\ud801\udc44b"};CharArraySet set = new CharArraySet(Arrays.asList(TEST_STOP_WORDS), true);- for (String upper : upperArr) {- set.add(upper);- }+ Collections.addAll(set, upperArr);for (int i = 0; i < upperArr.length; i++) {assertTrue(String.format(Locale.ROOT, missing, upperArr[i]), set.contains(upperArr[i]));assertTrue(String.format(Locale.ROOT, missing, lowerArr[i]), set.contains(lowerArr[i]));}set = new CharArraySet(Arrays.asList(TEST_STOP_WORDS), false);- for (String upper : upperArr) {- set.add(upper);- }+ Collections.addAll(set, upperArr);for (int i = 0; i < upperArr.length; i++) {assertTrue(String.format(Locale.ROOT, missing, upperArr[i]), set.contains(upperArr[i]));assertFalse(String.format(Locale.ROOT, falsePos, upperArr[i]), set.contains(lowerArr[i]));@@ -239,9 +227,9 @@ public class TestCharArraySet extends LuceneTestCase {stopwordsUpper.add(string.toUpperCase(Locale.ROOT));}setIgnoreCase.addAll(Arrays.asList(TEST_STOP_WORDS));- setIgnoreCase.add(Integer.valueOf(1));+ setIgnoreCase.add(1);setCaseSensitive.addAll(Arrays.asList(TEST_STOP_WORDS));- setCaseSensitive.add(Integer.valueOf(1));+ setCaseSensitive.add(1);CharArraySet copy = CharArraySet.copy(setIgnoreCase);CharArraySet copyCaseSens = CharArraySet.copy(setCaseSensitive);@@ -274,7 +262,7 @@ public class TestCharArraySet extends LuceneTestCase {/** Test the static #copy() function with a CharArraySet as a source */public void testCopyCharArraySet() {- CharArraySet setIngoreCase = new CharArraySet(10, true);+ CharArraySet setIgnoreCase = new CharArraySet(10, true);CharArraySet setCaseSensitive = new CharArraySet(10, false);List<String> stopwords = Arrays.asList(TEST_STOP_WORDS);@@ -282,15 +270,15 @@ public class TestCharArraySet extends LuceneTestCase {for (String string : stopwords) {stopwordsUpper.add(string.toUpperCase(Locale.ROOT));}- setIngoreCase.addAll(Arrays.asList(TEST_STOP_WORDS));- setIngoreCase.add(Integer.valueOf(1));+ setIgnoreCase.addAll(Arrays.asList(TEST_STOP_WORDS));+ setIgnoreCase.add(1);setCaseSensitive.addAll(Arrays.asList(TEST_STOP_WORDS));- setCaseSensitive.add(Integer.valueOf(1));+ setCaseSensitive.add(1);- CharArraySet copy = CharArraySet.copy(setIngoreCase);+ CharArraySet copy = CharArraySet.copy(setIgnoreCase);CharArraySet copyCaseSens = CharArraySet.copy(setCaseSensitive);- assertEquals(setIngoreCase.size(), copy.size());+ assertEquals(setIgnoreCase.size(), copy.size());assertEquals(setCaseSensitive.size(), copy.size());assertTrue(copy.containsAll(stopwords));@@ -311,21 +299,19 @@ public class TestCharArraySet extends LuceneTestCase {assertTrue(copy.containsAll(newWords));// new added terms are not in the source setfor (String string : newWords) {- assertFalse(setIngoreCase.contains(string));+ assertFalse(setIgnoreCase.contains(string));assertFalse(setCaseSensitive.contains(string));}}/** Test the static #copy() function with a JDK {@link Set} as a source */public void testCopyJDKSet() {- Set<String> set = new HashSet<>();-List<String> stopwords = Arrays.asList(TEST_STOP_WORDS);List<String> stopwordsUpper = new ArrayList<>();for (String string : stopwords) {stopwordsUpper.add(string.toUpperCase(Locale.ROOT));}- set.addAll(Arrays.asList(TEST_STOP_WORDS));+ Set<String> set = new HashSet<>(Arrays.asList(TEST_STOP_WORDS));CharArraySet copy = CharArraySet.copy(set);@@ -377,23 +363,11 @@ public class TestCharArraySet extends LuceneTestCase {public void testContainsWithNull() {CharArraySet set = new CharArraySet(1, true);- expectThrows(- NullPointerException.class,- () -> {- set.contains((char[]) null, 0, 10);- });+ expectThrows(NullPointerException.class, () -> set.contains(null, 0, 10));- expectThrows(- NullPointerException.class,- () -> {- set.contains((CharSequence) null);- });+ expectThrows(NullPointerException.class, () -> set.contains((CharSequence) null));- expectThrows(- NullPointerException.class,- () -> {- set.contains((Object) null);- });+ expectThrows(NullPointerException.class, () -> set.contains((Object) null));}public void testToString() {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiLineShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiLineShapeQueries.javaindex 3a831f78eee..34a4e01f304 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiLineShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiLineShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -49,11 +50,9 @@ public class TestLatLonMultiLineShapeQueries extends BaseLatLonShapeTestCase {List<Field> allFields = new ArrayList<>();for (Line line : lines) {Field[] fields = LatLonShape.createIndexableFields(name, line);- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -61,7 +60,7 @@ public class TestLatLonMultiLineShapeQueries extends BaseLatLonShapeTestCase {return new MultiLineValidator(ENCODER);}- protected class MultiLineValidator extends Validator {+ protected static class MultiLineValidator extends Validator {TestLatLonLineShapeQueries.LineValidator LINEVALIDATOR;MultiLineValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPointShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPointShapeQueries.javaindex f623212135f..495736cd779 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPointShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPointShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -49,11 +50,9 @@ public class TestLatLonMultiPointShapeQueries extends BaseLatLonShapeTestCase {List<Field> allFields = new ArrayList<>();for (Point point : points) {Field[] fields = LatLonShape.createIndexableFields(name, point.getLat(), point.getLon());- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -61,7 +60,7 @@ public class TestLatLonMultiPointShapeQueries extends BaseLatLonShapeTestCase {return new MultiPointValidator(ENCODER);}- protected class MultiPointValidator extends Validator {+ protected static class MultiPointValidator extends Validator {TestLatLonPointShapeQueries.PointValidator POINTVALIDATOR;MultiPointValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.javaindex 3a2c9d4b439..c6e9d131c0c 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -79,11 +80,9 @@ public class TestLatLonMultiPolygonShapeQueries extends BaseLatLonShapeTestCaseList<Field> allFields = new ArrayList<>();for (Polygon polygon : polygons) {Field[] fields = LatLonShape.createIndexableFields(name, polygon);- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -91,7 +90,7 @@ public class TestLatLonMultiPolygonShapeQueries extends BaseLatLonShapeTestCasereturn new MultiPolygonValidator(ENCODER);}- protected class MultiPolygonValidator extends Validator {+ protected static class MultiPolygonValidator extends Validator {TestLatLonPolygonShapeQueries.PolygonValidator POLYGONVALIDATOR;MultiPolygonValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiLineShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiLineShapeQueries.javaindex e64fb385cf0..1c615fd2f2b 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiLineShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiLineShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -48,11 +49,9 @@ public class TestXYMultiLineShapeQueries extends BaseXYShapeTestCase {List<Field> allFields = new ArrayList<>();for (XYLine line : lines) {Field[] fields = XYShape.createIndexableFields(name, line);- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -60,7 +59,7 @@ public class TestXYMultiLineShapeQueries extends BaseXYShapeTestCase {return new MultiLineValidator(ENCODER);}- protected class MultiLineValidator extends Validator {+ protected static class MultiLineValidator extends Validator {TestXYLineShapeQueries.LineValidator LINEVALIDATOR;MultiLineValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPointShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPointShapeQueries.javaindex f72dca49e80..04de8675149 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPointShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPointShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -49,11 +50,9 @@ public class TestXYMultiPointShapeQueries extends BaseXYShapeTestCase {List<Field> allFields = new ArrayList<>();for (XYPoint point : points) {Field[] fields = XYShape.createIndexableFields(name, point.getX(), point.getY());- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -61,7 +60,7 @@ public class TestXYMultiPointShapeQueries extends BaseXYShapeTestCase {return new MultiPointValidator(ENCODER);}- protected class MultiPointValidator extends Validator {+ protected static class MultiPointValidator extends Validator {TestXYPointShapeQueries.PointValidator POINTVALIDATOR;MultiPointValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPolygonShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPolygonShapeQueries.javaindex 7688ac1108c..099e4cfb72b 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPolygonShapeQueries.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestXYMultiPolygonShapeQueries.java@@ -17,6 +17,7 @@package org.apache.lucene.document;import java.util.ArrayList;+import java.util.Collections;import java.util.List;import org.apache.lucene.document.ShapeField.QueryRelation;import org.apache.lucene.geo.Component2D;@@ -50,11 +51,9 @@ public class TestXYMultiPolygonShapeQueries extends BaseXYShapeTestCase {List<Field> allFields = new ArrayList<>();for (XYPolygon polygon : polygons) {Field[] fields = XYShape.createIndexableFields(name, polygon);- for (Field field : fields) {- allFields.add(field);- }+ Collections.addAll(allFields, fields);}- return allFields.toArray(new Field[allFields.size()]);+ return allFields.toArray(new Field[0]);}@Override@@ -62,7 +61,7 @@ public class TestXYMultiPolygonShapeQueries extends BaseXYShapeTestCase {return new MultiPolygonValidator(ENCODER);}- protected class MultiPolygonValidator extends Validator {+ protected static class MultiPolygonValidator extends Validator {TestXYPolygonShapeQueries.PolygonValidator POLYGONVALIDATOR;MultiPolygonValidator(Encoder encoder) {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.javaindex c92ad20fc78..e62ca8235a2 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexFileDeleter.java@@ -21,8 +21,8 @@ import java.nio.file.Path;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;+import java.util.Collections;import java.util.HashSet;-import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.concurrent.atomic.AtomicBoolean;@@ -30,7 +30,6 @@ import org.apache.lucene.codecs.simpletext.SimpleTextCodec;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriterConfig.OpenMode;-import org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;@@ -140,7 +139,7 @@ public class TestIndexFileDeleter extends LuceneTestCase {? new String[] {"_3.scf"}: new String[] {"_3.cfs", "_3.cfe"};for (String f : cfsFiles3) {- assertTrue(!slowFileExists(dir, f));+ assertFalse(slowFileExists(dir, f));}String[] cfsFiles1 =@@ -185,22 +184,14 @@ public class TestIndexFileDeleter extends LuceneTestCase {Set<String> set2 = new HashSet<>();Set<String> extra = new HashSet<>();- for (int x = 0; x < files1.length; x++) {- set1.add(files1[x]);- }- for (int x = 0; x < files2.length; x++) {- set2.add(files2[x]);- }- Iterator<String> i1 = set1.iterator();- while (i1.hasNext()) {- String o = i1.next();+ Collections.addAll(set1, files1);+ Collections.addAll(set2, files2);+ for (String o : set1) {if (!set2.contains(o)) {extra.add(o);}}- Iterator<String> i2 = set2.iterator();- while (i2.hasNext()) {- String o = i2.next();+ for (String o : set2) {if (!set1.contains(o)) {extra.add(o);}@@ -459,8 +450,7 @@ public class TestIndexFileDeleter extends LuceneTestCase {// suppress only FakeIOException:if (exc instanceof RuntimeException && exc.getMessage().equals("fake fail")) {// ok to ignore- } else if ((exc instanceof AlreadyClosedException- || exc instanceof IllegalStateException)+ } else if (exc instanceof IllegalStateException&& exc.getCause() != null&& "fake fail".equals(exc.getCause().getMessage())) {// also ok to ignorediff --git a/lucene/misc/src/java/org/apache/lucene/misc/index/IndexSplitter.java b/lucene/misc/src/java/org/apache/lucene/misc/index/IndexSplitter.javaindex 3a9c4def640..523e26a70d7 100644--- a/lucene/misc/src/java/org/apache/lucene/misc/index/IndexSplitter.java+++ b/lucene/misc/src/java/org/apache/lucene/misc/index/IndexSplitter.java@@ -22,15 +22,14 @@ import java.nio.file.Path;import java.nio.file.Paths;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;-import java.util.ArrayList;import java.util.Collection;import java.util.Collections;-import java.util.List;import java.util.Locale;import org.apache.lucene.index.SegmentCommitInfo;import org.apache.lucene.index.SegmentInfo;import org.apache.lucene.index.SegmentInfos;import org.apache.lucene.store.FSDirectory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.SuppressForbidden;/**@@ -51,7 +50,7 @@ public class IndexSplitter {Path dir;- @SuppressForbidden(reason = "System.out required: command line tool")+ @SuppressForbidden(reason = "System.err required: command line tool")public static void main(String[] args) throws Exception {if (args.length < 2) {System.err.println("Usage: IndexSplitter <srcDir> -l (list the segments and their sizes)");@@ -67,18 +66,10 @@ public class IndexSplitter {if (args[1].equals("-l")) {is.listSegments();} else if (args[1].equals("-d")) {- List<String> segs = new ArrayList<>();- for (int x = 2; x < args.length; x++) {- segs.add(args[x]);- }- is.remove(segs.toArray(new String[0]));+ is.remove(ArrayUtil.copyOfSubArray(args, 2, args.length));} else {Path targetDir = Paths.get(args[1]);- List<String> segs = new ArrayList<>();- for (int x = 2; x < args.length; x++) {- segs.add(args[x]);- }- is.split(targetDir, segs.toArray(new String[0]));+ is.split(targetDir, ArrayUtil.copyOfSubArray(args, 2, args.length));}}diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.javaindex 3cfc8e2582d..7f8532d3ce5 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java@@ -106,9 +106,7 @@ public class TopSuggestDocsCollector extends SimpleCollector {public void finish() throws IOException {if (seenSurfaceForms != null) {// NOTE: this also clears the priorityQueue:- for (SuggestScoreDoc hit : priorityQueue.getResults()) {- pendingResults.add(hit);- }+ Collections.addAll(pendingResults, priorityQueue.getResults());// Deduplicate all hits: we already dedup'd efficiently within each segment by// truncating the FST top paths search, but across segments there may still be dups:@@ -149,13 +147,12 @@ public class TopSuggestDocsCollector extends SimpleCollector {// numSegments), but typically numSegments is smallish and num is smallish so this won't// matter much in practice:- Collections.sort(- pendingResults,+ pendingResults.sort((a, b) -> {// sort by higher scoreint cmp = Float.compare(b.score, a.score);if (cmp == 0) {- // tie break by completion key+ // tie-break by completion keycmp = Lookup.CHARSEQUENCE_COMPARATOR.compare(a.key, b.key);if (cmp == 0) {// prefer smaller doc id, in case of a tiediff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.javaindex c57ddb25c57..547e745fec2 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/tst/TSTLookup.java@@ -18,6 +18,7 @@ package org.apache.lucene.search.suggest.tst;import java.io.IOException;import java.util.ArrayList;+import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Set;@@ -92,12 +93,10 @@ public class TSTLookup extends Lookup {// difference to match UTF16's sort order:// NOTE: instead of moving supplementary code points (0xee and 0xef) to the unused 0xfe- // and 0xff,- // we move them to the unused 0xfc and 0xfd [reserved for future 6-byte character- // sequences]+ // and 0xff, we move them to the unused 0xfc and 0xfd [reserved for future 6-byte+ // character sequences]// this reserves 0xff for preflex's term reordering (surrogate dance), and if unicode- // grows such- // that 6-byte sequences are needed we have much bigger problems anyway.+ // grows such that 6-byte sequences are needed we have much bigger problems anyway.if (aByte >= 0xee && bByte >= 0xee) {if ((aByte & 0xfe) == 0xee) {aByte += 0xe;@@ -135,7 +134,7 @@ public class TSTLookup extends Lookup {while ((spare = iterator.next()) != null) {charsSpare.copyUTF8Bytes(spare);tokens.add(charsSpare.toString());- vals.add(Long.valueOf(iterator.weight()));+ vals.add(iterator.weight());count++;}autocomplete.balancedTree(tokens.toArray(), vals.toArray(), 0, tokens.size() - 1, root);@@ -187,7 +186,7 @@ public class TSTLookup extends Lookup {}List<TernaryTreeNode> list = autocomplete.prefixCompletion(root, key, 0);List<LookupResult> res = new ArrayList<>();- if (list == null || list.size() == 0) {+ if (list == null || list.isEmpty()) {return res;}int maxCnt = Math.min(num, list.size());@@ -197,9 +196,7 @@ public class TSTLookup extends Lookup {for (TernaryTreeNode ttn : list) {queue.insertWithOverflow(new LookupResult(ttn.token, ((Number) ttn.val).longValue()));}- for (LookupResult lr : queue.getResults()) {- res.add(lr);- }+ Collections.addAll(res, queue.getResults());} else {for (int i = 0; i < maxCnt; i++) {TernaryTreeNode ttn = list.get(i);@@ -223,7 +220,7 @@ public class TSTLookup extends Lookup {node.token = in.readString();}if ((mask & HAS_VALUE) != 0) {- node.val = Long.valueOf(in.readLong());+ node.val = in.readLong();}if ((mask & LO_KID) != 0) {node.loKid = new TernaryTreeNode();diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestAnalyzingSuggester.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestAnalyzingSuggester.javaindex cb14d983716..75003c9293a 100644--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestAnalyzingSuggester.java+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestAnalyzingSuggester.java@@ -25,7 +25,6 @@ import java.nio.file.Path;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;-import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.List;@@ -704,8 +703,8 @@ public class TestAnalyzingSuggester extends LuceneTestCase {}private static class MockTokenEatingAnalyzer extends Analyzer {- private int numStopChars;- private boolean preserveHoles;+ private final int numStopChars;+ private final boolean preserveHoles;public MockTokenEatingAnalyzer(int numStopChars, boolean preserveHoles) {this.preserveHoles = preserveHoles;@@ -731,7 +730,7 @@ public class TestAnalyzingSuggester extends LuceneTestCase {}}- private static char SEP = '\u001F';+ private static final char SEP = '\u001F';public void testRandom() throws Exception {@@ -882,7 +881,7 @@ public class TestAnalyzingSuggester extends LuceneTestCase {List<LookupResult> r =suggester.lookup(TestUtil.stringToCharSequence(prefix, random()), false, topN);- // 2. go thru whole set to find suggestions:+ // 2. go through whole set to find suggestions:List<TermFreq2> matches = new ArrayList<>();// "Analyze" the key:@@ -919,7 +918,7 @@ public class TestAnalyzingSuggester extends LuceneTestCase {analyzedKey = s;}- if (analyzedKey.length() == 0) {+ if (analyzedKey.isEmpty()) {// Currently suggester can't suggest from the empty// string! You get no results, not all results...continue;@@ -943,17 +942,13 @@ public class TestAnalyzingSuggester extends LuceneTestCase {assertTrue(numStopChars > 0 || matches.size() > 0);if (matches.size() > 1) {- Collections.sort(- matches,- new Comparator<TermFreq2>() {- @Override- public int compare(TermFreq2 left, TermFreq2 right) {- int cmp = Long.compare(right.weight, left.weight);- if (cmp == 0) {- return left.analyzedForm.compareTo(right.analyzedForm);- } else {- return cmp;- }+ matches.sort(+ (left, right) -> {+ int cmp = Long.compare(right.weight, left.weight);+ if (cmp == 0) {+ return left.analyzedForm.compareTo(right.analyzedForm);+ } else {+ return cmp;}});}@@ -978,7 +973,7 @@ public class TestAnalyzingSuggester extends LuceneTestCase {for (int hit = 0; hit < r.size(); hit++) {// System.out.println(" check hit " + hit);- assertEquals(matches.get(hit).surfaceForm.toString(), r.get(hit).key.toString());+ assertEquals(matches.get(hit).surfaceForm, r.get(hit).key.toString());assertEquals(matches.get(hit).weight, r.get(hit).value);if (doPayloads) {assertEquals(matches.get(hit).payload, r.get(hit).payload);@@ -1316,11 +1311,8 @@ public class TestAnalyzingSuggester extends LuceneTestCase {IOUtils.close(a, tempDir);}- static final Iterable<Input> shuffle(Input... values) {- final List<Input> asList = new ArrayList<>(values.length);- for (Input value : values) {- asList.add(value);- }+ static Iterable<Input> shuffle(Input... values) {+ final List<Input> asList = Arrays.asList(values);Collections.shuffle(asList, random());return asList;}