Skip to content

Commit

Permalink
Backport: Make static final Map constants immutable (#13100)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabi0 authored Feb 14, 2024
1 parent e51ca16 commit dd2fc3f
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 244 deletions.
3 changes: 2 additions & 1 deletion lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ New Features

Improvements
---------------------
(No changes)

* GITHUB#13092: `static final Map` constants have been made immutable (Dmitry Cherniachenko)

Optimizations
---------------------
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.XMLConstants;
Expand Down Expand Up @@ -68,7 +67,7 @@ private class Parser extends DefaultHandler implements Runnable {
private boolean stopped = false;
private String[] tuple;
private NoMoreDataException nmde;
private StringBuilder contents = new StringBuilder();
private final StringBuilder contents = new StringBuilder();
private String title;
private String body;
private String time;
Expand Down Expand Up @@ -262,7 +261,6 @@ private void stop() {
}
}

private static final Map<String, Integer> ELEMENTS = new HashMap<>();
private static final int TITLE = 0;
private static final int DATE = TITLE + 1;
private static final int BODY = DATE + 1;
Expand All @@ -272,32 +270,32 @@ private void stop() {
// should not be part of the tuple, we should define them after LENGTH.
private static final int PAGE = LENGTH + 1;

private static final Map<String, Integer> ELEMENTS =
Map.of(
"page", PAGE,
"text", BODY,
"timestamp", DATE,
"title", TITLE,
"id", ID);

private static final String[] months = {
"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
};

static {
ELEMENTS.put("page", Integer.valueOf(PAGE));
ELEMENTS.put("text", Integer.valueOf(BODY));
ELEMENTS.put("timestamp", Integer.valueOf(DATE));
ELEMENTS.put("title", Integer.valueOf(TITLE));
ELEMENTS.put("id", Integer.valueOf(ID));
}

/**
* Returns the type of the element if defined, otherwise returns -1. This method is useful in
* startElement and endElement, by not needing to compare the element qualified name over and
* over.
*/
private static final int getElementType(String elem) {
private static int getElementType(String elem) {
Integer val = ELEMENTS.get(elem);
return val == null ? -1 : val.intValue();
}

private Path file;
private boolean keepImages = true;
private InputStream is;
private Parser parser = new Parser();
private final Parser parser = new Parser();

@Override
public void close() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand All @@ -40,22 +42,28 @@ public enum ParsePathType {
/** trec parser type used for unknown extensions */
public static final ParsePathType DEFAULT_PATH_TYPE = ParsePathType.GOV2;

static final Map<ParsePathType, TrecDocParser> pathType2parser = new HashMap<>();
static final Map<ParsePathType, TrecDocParser> pathType2Parser;

static {
pathType2parser.put(ParsePathType.GOV2, new TrecGov2Parser());
pathType2parser.put(ParsePathType.FBIS, new TrecFBISParser());
pathType2parser.put(ParsePathType.FR94, new TrecFR94Parser());
pathType2parser.put(ParsePathType.FT, new TrecFTParser());
pathType2parser.put(ParsePathType.LATIMES, new TrecLATimesParser());
pathType2Parser =
Collections.unmodifiableMap(
new EnumMap<>(
Map.of(
ParsePathType.GOV2, new TrecGov2Parser(),
ParsePathType.FBIS, new TrecFBISParser(),
ParsePathType.FR94, new TrecFR94Parser(),
ParsePathType.FT, new TrecFTParser(),
ParsePathType.LATIMES, new TrecLATimesParser())));
}

static final Map<String, ParsePathType> pathName2Type = new HashMap<>();
static final Map<String, ParsePathType> pathName2Type;

static {
Map<String, ParsePathType> name2Type = new HashMap<>();
for (ParsePathType ppt : ParsePathType.values()) {
pathName2Type.put(ppt.name().toUpperCase(Locale.ROOT), ppt);
name2Type.put(ppt.name().toUpperCase(Locale.ROOT), ppt);
}
pathName2Type = Collections.unmodifiableMap(name2Type);
}

/** max length of walk up from file to its ancestors when looking for a known path type */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public DocData parse(
StringBuilder docBuf,
ParsePathType pathType)
throws IOException {
return pathType2parser.get(pathType).parse(docData, name, trecSrc, docBuf, pathType);
return pathType2Parser.get(pathType).parse(docData, name, trecSrc, docBuf, pathType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.compress.compressors.CompressorException;
Expand Down Expand Up @@ -70,15 +69,9 @@ private OutputStream outputStream(OutputStream os) throws IOException {
}
}

private static final Map<String, Type> extensionToType = new HashMap<>();

static {
// these in are lower case, we will lower case at the test as well
extensionToType.put(".bz2", Type.BZIP2);
extensionToType.put(".bzip", Type.BZIP2);
extensionToType.put(".gz", Type.GZIP);
extensionToType.put(".gzip", Type.GZIP);
}
// these are in lower case, we will lower case at the test as well
private static final Map<String, Type> extensionToType =
Map.of(".bz2", Type.BZIP2, ".bzip", Type.BZIP2, ".gz", Type.GZIP, ".gzip", Type.GZIP);

/**
* Returns an {@link InputStream} over the requested file. This method attempts to identify the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -404,21 +405,23 @@ public enum ShapeType {
ENVELOPE("envelope"); // not part of the actual WKB spec

private final String shapeName;
private static Map<String, ShapeType> shapeTypeMap = new HashMap<>();
private static final Map<String, ShapeType> shapeTypeMap;
private static final String BBOX = "BBOX";

static {
Map<String, ShapeType> shapeTypes = new HashMap<>();
for (ShapeType type : values()) {
shapeTypeMap.put(type.shapeName, type);
shapeTypes.put(type.shapeName, type);
}
shapeTypeMap.put(ENVELOPE.wktName().toLowerCase(Locale.ROOT), ENVELOPE);
shapeTypes.put(ENVELOPE.wktName().toLowerCase(Locale.ROOT), ENVELOPE);
shapeTypeMap = Collections.unmodifiableMap(shapeTypes);
}

ShapeType(String shapeName) {
this.shapeName = shapeName;
}

protected String typename() {
String typename() {
return shapeName;
}

Expand All @@ -429,10 +432,9 @@ public String wktName() {

public static ShapeType forName(String shapename) {
String typename = shapename.toLowerCase(Locale.ROOT);
for (ShapeType type : values()) {
if (type.shapeName.equals(typename)) {
return type;
}
ShapeType type = shapeTypeMap.get(typename);
if (type != null) {
return type;
}
throw new IllegalArgumentException("unknown geo_shape [" + shapename + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public LeafCollector getLeafCollector(LeafReaderContext context) throws IOExcept
}

// Test data - format is artist, song, weeks at top of charts
private static String[] hitsOfThe60s = {
private static final String[] hitsOfThe60s = {
"1966\tSPENCER DAVIS GROUP\tKEEP ON RUNNING\t1",
"1966\tOVERLANDERS\tMICHELLE\t3",
"1966\tNANCY SINATRA\tTHESE BOOTS ARE MADE FOR WALKIN'\t4",
Expand Down Expand Up @@ -317,7 +317,7 @@ public LeafCollector getLeafCollector(LeafReaderContext context) throws IOExcept
"1969\tARCHIES\tSUGAR, SUGAR\t4"
};

private static final Map<String, Record> parsedRecords = new HashMap<String, Record>();
private static final Map<String, Record> parsedRecords = new HashMap<>();
private Directory dir;
private IndexReader reader;
private IndexSearcher searcher;
Expand Down Expand Up @@ -452,7 +452,7 @@ public void tearDown() throws Exception {

private int getMaxNumRecordsPerArtist(ScoreDoc[] sd) throws IOException {
int result = 0;
HashMap<String, Integer> artistCounts = new HashMap<String, Integer>();
HashMap<String, Integer> artistCounts = new HashMap<>();
for (int i = 0; i < sd.length; i++) {
Document doc = reader.storedFields().document(sd[i].doc);
Record record = parsedRecords.get(doc.get("id"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.apache.lucene.queries.payloads;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery.MatchOperation;
import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery.PayloadType;
import org.apache.lucene.util.ArrayUtil;
Expand All @@ -30,32 +32,45 @@
*/
public class PayloadMatcherFactory {

private static final EnumMap<PayloadType, EnumMap<MatchOperation, PayloadMatcher>>
private static final Map<PayloadType, Map<MatchOperation, PayloadMatcher>>
payloadCheckerOpTypeMap;

static {
payloadCheckerOpTypeMap = new EnumMap<>(PayloadType.class);
// ints
EnumMap<MatchOperation, PayloadMatcher> intCheckers = new EnumMap<>(MatchOperation.class);
intCheckers.put(MatchOperation.LT, new LTIntPayloadMatcher());
intCheckers.put(MatchOperation.LTE, new LTEIntPayloadMatcher());
intCheckers.put(MatchOperation.GT, new GTIntPayloadMatcher());
intCheckers.put(MatchOperation.GTE, new GTEIntPayloadMatcher());
EnumMap<MatchOperation, PayloadMatcher> floatCheckers = new EnumMap<>(MatchOperation.class);
floatCheckers.put(MatchOperation.LT, new LTFloatPayloadMatcher());
floatCheckers.put(MatchOperation.LTE, new LTEFloatPayloadMatcher());
floatCheckers.put(MatchOperation.GT, new GTFloatPayloadMatcher());
floatCheckers.put(MatchOperation.GTE, new GTEFloatPayloadMatcher());
Map<MatchOperation, PayloadMatcher> intCheckers =
Collections.unmodifiableMap(
new EnumMap<>(
Map.of(
MatchOperation.LT, new LTIntPayloadMatcher(),
MatchOperation.LTE, new LTEIntPayloadMatcher(),
MatchOperation.GT, new GTIntPayloadMatcher(),
MatchOperation.GTE, new GTEIntPayloadMatcher())));
// floats
Map<MatchOperation, PayloadMatcher> floatCheckers =
Collections.unmodifiableMap(
new EnumMap<>(
Map.of(
MatchOperation.LT, new LTFloatPayloadMatcher(),
MatchOperation.LTE, new LTEFloatPayloadMatcher(),
MatchOperation.GT, new GTFloatPayloadMatcher(),
MatchOperation.GTE, new GTEFloatPayloadMatcher())));
// strings
EnumMap<MatchOperation, PayloadMatcher> stringCheckers = new EnumMap<>(MatchOperation.class);
stringCheckers.put(MatchOperation.LT, new LTStringPayloadMatcher());
stringCheckers.put(MatchOperation.LTE, new LTEStringPayloadMatcher());
stringCheckers.put(MatchOperation.GT, new GTStringPayloadMatcher());
stringCheckers.put(MatchOperation.GTE, new GTEStringPayloadMatcher());
Map<MatchOperation, PayloadMatcher> stringCheckers =
Collections.unmodifiableMap(
new EnumMap<>(
Map.of(
MatchOperation.LT, new LTStringPayloadMatcher(),
MatchOperation.LTE, new LTEStringPayloadMatcher(),
MatchOperation.GT, new GTStringPayloadMatcher(),
MatchOperation.GTE, new GTEStringPayloadMatcher())));
// load the matcher maps per payload type
payloadCheckerOpTypeMap.put(PayloadType.INT, intCheckers);
payloadCheckerOpTypeMap.put(PayloadType.FLOAT, floatCheckers);
payloadCheckerOpTypeMap.put(PayloadType.STRING, stringCheckers);
payloadCheckerOpTypeMap =
Collections.unmodifiableMap(
new EnumMap<>(
Map.of(
PayloadType.INT, intCheckers,
PayloadType.FLOAT, floatCheckers,
PayloadType.STRING, stringCheckers)));
}

/**
Expand All @@ -75,7 +90,7 @@ public static PayloadMatcher createMatcherForOpAndType(
return new EQPayloadMatcher();
}
// otherwise, we need to pay attention to the payload type and operation
EnumMap<MatchOperation, PayloadMatcher> opMap = payloadCheckerOpTypeMap.get(payloadType);
Map<MatchOperation, PayloadMatcher> opMap = payloadCheckerOpTypeMap.get(payloadType);
if (opMap != null) {
return opMap.get(op);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.geometry.S2CellId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,7 +64,7 @@ class S2PrefixTreeCell implements CellCanPrune {
for (int i = 0; i < TOKENS.length; i++) {
pixels.put(TOKENS[i], i);
}
PIXELS = Map.copyOf(pixels);
PIXELS = Collections.unmodifiableMap(pixels);
}

S2CellId cellId;
Expand Down

0 comments on commit dd2fc3f

Please sign in to comment.