Skip to content

Commit 3a6f2f7

Browse files
authored
Use Map.copyOf in lucene core (#649)
This cuts over several places that use the pattern of creating a copy of the supplied map with Map.copyOf.
1 parent fb28958 commit 3a6f2f7

File tree

8 files changed

+16
-35
lines changed

8 files changed

+16
-35
lines changed

lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,8 @@ static String getDictionaryEncoding(InputStream affix) throws IOException, Parse
660660
}
661661
}
662662

663-
static final Map<String,String> CHARSET_ALIASES;
664-
static {
665-
Map<String,String> m = new HashMap<>();
666-
m.put("microsoft-cp1251", "windows-1251");
667-
m.put("TIS620-2533", "TIS-620");
668-
CHARSET_ALIASES = Collections.unmodifiableMap(m);
669-
}
670-
663+
static final Map<String,String> CHARSET_ALIASES = Map.of("microsoft-cp1251", "windows-1251", "TIS620-2533", "TIS-620");
664+
671665
/**
672666
* Retrieves the CharsetDecoder for the given encoding. Note, This isn't perfect as I think ISCII-DEVANAGARI and
673667
* MICROSOFT-CP1251 etc are allowed...

lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AbstractAnalysisFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.ArrayList;
2929
import java.util.Collection;
3030
import java.util.Collections;
31-
import java.util.HashMap;
3231
import java.util.HashSet;
3332
import java.util.List;
3433
import java.util.Map;
@@ -69,7 +68,7 @@ public abstract class AbstractAnalysisFactory {
6968
* Initialize this factory via a set of key-value pairs.
7069
*/
7170
protected AbstractAnalysisFactory(Map<String,String> args) {
72-
originalArgs = Collections.unmodifiableMap(new HashMap<>(args));
71+
originalArgs = Map.copyOf(args);
7372
String version = get(args, LUCENE_MATCH_VERSION_PARAM);
7473
if (version == null) {
7574
luceneMatchVersion = Version.LATEST;

lucene/core/src/java/org/apache/lucene/index/FrozenBufferedUpdates.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.Collections;
24-
import java.util.HashMap;
2524
import java.util.HashSet;
2625
import java.util.List;
2726
import java.util.Locale;
@@ -111,7 +110,7 @@ public FrozenBufferedUpdates(InfoStream infoStream, BufferedUpdates updates, Seg
111110
// so that it maps to all fields it affects, sorted by their docUpto, and traverse
112111
// that Term only once, applying the update to all fields that still need to be
113112
// updated.
114-
this.fieldUpdates = Collections.unmodifiableMap(new HashMap<>(updates.fieldUpdates));
113+
this.fieldUpdates = Map.copyOf(updates.fieldUpdates);
115114
this.fieldUpdatesCount = updates.numFieldUpdates.get();
116115

117116
bytesUsed = (int) ((deleteTerms.ramBytesUsed() + deleteQueries.length * BYTES_PER_DEL_QUERY)

lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
import java.io.PrintStream;
2121
import java.util.Arrays;
22-
import java.util.Collections;
2322
import java.util.EnumSet;
24-
import java.util.HashMap;
2523
import java.util.Map;
2624
import java.util.Objects;
2725
import java.util.stream.Collectors;
@@ -554,8 +552,7 @@ public IndexWriterConfig setSoftDeletesField(String softDeletesField) {
554552
* Note: This method make a shallow copy of the provided map.
555553
*/
556554
public IndexWriterConfig setReaderAttributes(Map<String, String> readerAttributes) {
557-
this.readerAttributes = Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(readerAttributes)));
555+
this.readerAttributes = Map.copyOf(Objects.requireNonNull(readerAttributes));
558556
return this;
559557
}
560-
561558
}

lucene/core/src/java/org/apache/lucene/index/SegmentInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final class SegmentInfo {
8787
Version minVersion;
8888

8989
void setDiagnostics(Map<String, String> diagnostics) {
90-
this.diagnostics = Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(diagnostics)));
90+
this.diagnostics = Map.copyOf(Objects.requireNonNull(diagnostics));
9191
}
9292

9393
/** Returns diagnostics saved into the segment when it was
@@ -112,12 +112,12 @@ public SegmentInfo(Directory dir, Version version, Version minVersion, String na
112112
this.maxDoc = maxDoc;
113113
this.isCompoundFile = isCompoundFile;
114114
this.codec = codec;
115-
this.diagnostics = Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(diagnostics)));
115+
this.diagnostics = Map.copyOf(Objects.requireNonNull(diagnostics));
116116
this.id = id;
117117
if (id.length != StringHelper.ID_LENGTH) {
118118
throw new IllegalArgumentException("invalid id: " + Arrays.toString(id));
119119
}
120-
this.attributes = Collections.unmodifiableMap(new HashMap<>(Objects.requireNonNull(attributes)));
120+
this.attributes = Map.copyOf(Objects.requireNonNull(attributes));
121121
this.indexSort = indexSort;
122122
}
123123

lucene/core/src/java/org/apache/lucene/index/SegmentReadState.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.apache.lucene.index;
1818

1919

20-
import java.util.Collections;
21-
import java.util.HashMap;
2220
import java.util.Map;
2321

2422
import org.apache.lucene.codecs.PostingsFormat; // javadocs
@@ -82,7 +80,7 @@ public SegmentReadState(Directory dir,
8280
this.context = context;
8381
this.segmentSuffix = segmentSuffix;
8482
this.openedFromWriter = openedFromWriter;
85-
this.readerAttributes = Collections.unmodifiableMap(new HashMap<>(readerAttributes));
83+
this.readerAttributes = Map.copyOf(readerAttributes);
8684
}
8785

8886
/** Create a {@code SegmentReadState}. */

lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class StandardDirectoryReader extends DirectoryReader {
5353
this.segmentInfos = sis;
5454
this.applyAllDeletes = applyAllDeletes;
5555
this.writeAllDeletes = writeAllDeletes;
56-
this.readerAttributes = Collections.unmodifiableMap(new HashMap<>(readerAttributes));
56+
this.readerAttributes = Map.copyOf(readerAttributes);
5757
}
5858

5959
/** called from DirectoryReader.open(...) methods */

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.IOException;
3030
import java.util.Collections;
3131
import java.util.HashMap;
32-
import java.util.LinkedHashMap;
3332
import java.util.Map;
3433

3534
public class TestSegmentInfos extends LuceneTestCase {
@@ -111,14 +110,10 @@ public void testToString() throws Throwable{
111110
Codec codec = Codec.getDefault();
112111

113112
// diagnostics map
114-
Map<String, String> diagnostics = new LinkedHashMap<>();
115-
diagnostics.put("key1", "value1");
116-
diagnostics.put("key2", "value2");
113+
Map<String, String> diagnostics = Map.of("key1", "value1", "key2", "value2");
117114

118115
// attributes map
119-
Map<String,String> attributes = new LinkedHashMap<>();
120-
attributes.put("key1", "value1");
121-
attributes.put("key2", "value2");
116+
Map<String,String> attributes = Map.of("akey1", "value1", "akey2", "value2");
122117

123118
// diagnostics X, attributes X
124119
si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, Collections.emptyMap(), StringHelper.randomId(), new HashMap<>(), Sort.INDEXORDER);
@@ -131,23 +126,22 @@ public void testToString() throws Throwable{
131126
assertEquals("TEST(" + Version.LATEST.toString() + ")" +
132127
":C10000" +
133128
":[indexSort=<doc>]" +
134-
":[diagnostics={key1=value1, key2=value2}]", si.toString());
129+
":[diagnostics=" + diagnostics + "]", si.toString());
135130

136131
// diagnostics X, attributes O
137132
si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, Collections.emptyMap(), StringHelper.randomId(), attributes, Sort.INDEXORDER);
138133
assertEquals("TEST(" + Version.LATEST.toString() + ")" +
139134
":C10000" +
140135
":[indexSort=<doc>]" +
141-
":[attributes={key1=value1, key2=value2}]", si.toString());
136+
":[attributes=" + attributes + "]", si.toString());
142137

143138
// diagnostics O, attributes O
144139
si = new SegmentInfo(dir, Version.LATEST, Version.LATEST, "TEST", 10000, false, codec, diagnostics, StringHelper.randomId(), attributes, Sort.INDEXORDER);
145-
System.out.println(si.toString());
146140
assertEquals("TEST(" + Version.LATEST.toString() + ")" +
147141
":C10000" +
148142
":[indexSort=<doc>]" +
149-
":[diagnostics={key1=value1, key2=value2}]" +
150-
":[attributes={key1=value1, key2=value2}]", si.toString());
143+
":[diagnostics=" + diagnostics + "]" +
144+
":[attributes=" + attributes + "]", si.toString());
151145

152146
dir.close();
153147
}

0 commit comments

Comments
 (0)