immutable maps

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921575 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2024-10-26 16:34:40 +00:00
parent 69d935d4db
commit 9185ec02c5
12 changed files with 76 additions and 55 deletions

View File

@ -17,6 +17,7 @@
package org.apache.poi.xdgf.usermodel.section; package org.apache.poi.xdgf.usermodel.section;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -84,6 +85,6 @@ enum GeometryRowTypes {
return l.constructor.apply(row); return l.constructor.apply(row);
} }
private static final Map<String, GeometryRowTypes> LOOKUP = private static final Map<String, GeometryRowTypes> LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity())); Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity())));
} }

View File

@ -17,6 +17,7 @@
package org.apache.poi.xdgf.usermodel.section; package org.apache.poi.xdgf.usermodel.section;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
@ -72,6 +73,6 @@ enum XDGFSectionTypes {
return l.constructor.apply(section, containingSheet); return l.constructor.apply(section, containingSheet);
} }
private static final Map<String, XDGFSectionTypes> LOOKUP = private static final Map<String, XDGFSectionTypes> LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity())); Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity())));
} }

View File

@ -17,6 +17,7 @@
package org.apache.poi.hslf.record; package org.apache.poi.hslf.record;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -181,10 +182,11 @@ public enum RecordTypes {
private static final Map<Short,RecordTypes> LOOKUP; private static final Map<Short,RecordTypes> LOOKUP;
static { static {
LOOKUP = new HashMap<>(); final Map<Short,RecordTypes> map = new HashMap<>();
for(RecordTypes s : values()) { for(RecordTypes s : values()) {
LOOKUP.put(s.typeID, s); map.put(s.typeID, s);
} }
LOOKUP = Collections.unmodifiableMap(map);
} }
public final short typeID; public final short typeID;

View File

@ -21,6 +21,7 @@ import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle; import java.time.format.FormatStyle;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Collections;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
@ -64,8 +65,8 @@ public final class LocaleDateFormat {
private final LocaleID lcid; private final LocaleID lcid;
private final Object[] mapping; private final Object[] mapping;
private static final Map<LocaleID,MapFormatPPT> LCID_LOOKUP = private static final Map<LocaleID,MapFormatPPT> LCID_LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(MapFormatPPT::getLocaleID, Function.identity())); Stream.of(values()).collect(Collectors.toMap(MapFormatPPT::getLocaleID, Function.identity())));
MapFormatPPT(LocaleID lcid, Object... mapping) { MapFormatPPT(LocaleID lcid, Object... mapping) {
this.lcid = lcid; this.lcid = lcid;
@ -195,9 +196,9 @@ public final class LocaleDateFormat {
private final LocaleID[] lcid; private final LocaleID[] lcid;
private final Object[] mapping; private final Object[] mapping;
private static final Map<LocaleID, MapFormatException> LCID_LOOKUP = private static final Map<LocaleID, MapFormatException> LCID_LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).flatMap(m -> Stream.of(m.lcid).map(l -> new AbstractMap.SimpleEntry<>(l, m))) Stream.of(values()).flatMap(m -> Stream.of(m.lcid).map(l -> new AbstractMap.SimpleEntry<>(l, m)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
MapFormatException(LocaleID[] lcid, Object... mapping) { MapFormatException(LocaleID[] lcid, Object... mapping) {
this.lcid = lcid; this.lcid = lcid;

View File

@ -17,6 +17,7 @@
package org.apache.poi.ddf; package org.apache.poi.ddf;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -365,8 +366,8 @@ public enum EscherPropertyTypes {
return propNumber; return propNumber;
} }
private static final Map<Short, EscherPropertyTypes> LOOKUP = private static final Map<Short, EscherPropertyTypes> LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(EscherPropertyTypes::getPropertyId, Function.identity())); Stream.of(values()).collect(Collectors.toMap(EscherPropertyTypes::getPropertyId, Function.identity())));
public static EscherPropertyTypes forPropertyID(int propertyId) { public static EscherPropertyTypes forPropertyID(int propertyId) {
EscherPropertyTypes rt = LOOKUP.get((short)(propertyId & 0x3FFF)); EscherPropertyTypes rt = LOOKUP.get((short)(propertyId & 0x3FFF));

View File

@ -17,6 +17,7 @@
package org.apache.poi.ddf; package org.apache.poi.ddf;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -108,8 +109,8 @@ public enum EscherRecordTypes {
return typeID; return typeID;
} }
private static final Map<Short, EscherRecordTypes> LOOKUP = private static final Map<Short, EscherRecordTypes> LOOKUP = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(EscherRecordTypes::getTypeId, Function.identity())); Stream.of(values()).collect(Collectors.toMap(EscherRecordTypes::getTypeId, Function.identity())));
public static EscherRecordTypes forTypeID(int typeID) { public static EscherRecordTypes forTypeID(int typeID) {
// Section 2.2.23: 0xF02A is treated as 0xF01D // Section 2.2.23: 0xF02A is treated as 0xF01D

View File

@ -20,6 +20,7 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -211,8 +212,8 @@ public enum HSSFRecordTypes {
T apply(RecordInputStream in); T apply(RecordInputStream in);
} }
private static final Map<Short,HSSFRecordTypes> LOOKUP = private static final Map<Short,HSSFRecordTypes> LOOKUP = Collections.unmodifiableMap(
Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity())); Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity())));
public final short sid; public final short sid;
public final Class<? extends org.apache.poi.hssf.record.Record> clazz; public final Class<? extends org.apache.poi.hssf.record.Record> clazz;

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -64,8 +65,8 @@ public abstract class SubRecord implements Duplicatable, GenericRecord {
T apply(LittleEndianInput in, int size, int cmoOt); T apply(LittleEndianInput in, int size, int cmoOt);
} }
private static final Map<Short,SubRecordTypes> LOOKUP = private static final Map<Short,SubRecordTypes> LOOKUP = Collections.unmodifiableMap(
Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity())); Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity())));
public final short sid; public final short sid;
public final RecordConstructor<?> recordConstructor; public final RecordConstructor<?> recordConstructor;

View File

@ -16,6 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
@ -149,15 +150,21 @@ public enum FormulaError {
return repr; return repr;
} }
private static final Map<String, FormulaError> smap = new HashMap<>(); private static final Map<String, FormulaError> smap;
private static final Map<Byte, FormulaError> bmap = new HashMap<>(); private static final Map<Byte, FormulaError> bmap;
private static final Map<Integer, FormulaError> imap = new HashMap<>(); private static final Map<Integer, FormulaError> imap;
static { static {
final Map<String, FormulaError> mapS = new HashMap<>();
final Map<Byte, FormulaError> mapB = new HashMap<>();
final Map<Integer, FormulaError> mapI = new HashMap<>();
for (FormulaError error : values()) { for (FormulaError error : values()) {
bmap.put(error.getCode(), error); mapB.put(error.getCode(), error);
imap.put(error.getLongCode(), error); mapI.put(error.getLongCode(), error);
smap.put(error.getString(), error); mapS.put(error.getString(), error);
} }
smap = Collections.unmodifiableMap(mapS);
bmap = Collections.unmodifiableMap(mapB);
imap = Collections.unmodifiableMap(mapI);
} }
public static boolean isValidCode(int errorCode) { public static boolean isValidCode(int errorCode) {

View File

@ -16,6 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -75,11 +76,11 @@ public enum PageMargin {
private static final Map<Short, PageMargin> PAGE_MARGIN_BY_LEGACY_API_VALUE; private static final Map<Short, PageMargin> PAGE_MARGIN_BY_LEGACY_API_VALUE;
static { static {
PAGE_MARGIN_BY_LEGACY_API_VALUE = new HashMap<>(); final Map<Short, PageMargin> map = new HashMap<>();
for (PageMargin margin : values()) { for (PageMargin margin : values()) {
PAGE_MARGIN_BY_LEGACY_API_VALUE.put(margin.legacyApiValue, margin); map.put(margin.legacyApiValue, margin);
} }
PAGE_MARGIN_BY_LEGACY_API_VALUE = Collections.unmodifiableMap(map);
} }
/** /**

View File

@ -265,31 +265,33 @@ public final class CellUtil {
public static final String QUOTE_PREFIXED = "quotePrefixed"; public static final String QUOTE_PREFIXED = "quotePrefixed";
// FIXME Must be deleted along with string constants // FIXME Must be deleted along with string constants
static final Map<String, CellPropertyType> namePropertyMap = new HashMap<>(); static final Map<String, CellPropertyType> namePropertyMap;
static { static {
namePropertyMap.put(ALIGNMENT, CellPropertyType.ALIGNMENT); final Map<String, CellPropertyType> map = new HashMap<>();
namePropertyMap.put(BORDER_BOTTOM, CellPropertyType.BORDER_BOTTOM); map.put(ALIGNMENT, CellPropertyType.ALIGNMENT);
namePropertyMap.put(BORDER_LEFT, CellPropertyType.BORDER_LEFT); map.put(BORDER_BOTTOM, CellPropertyType.BORDER_BOTTOM);
namePropertyMap.put(BORDER_RIGHT, CellPropertyType.BORDER_RIGHT); map.put(BORDER_LEFT, CellPropertyType.BORDER_LEFT);
namePropertyMap.put(BORDER_TOP, CellPropertyType.BORDER_TOP); map.put(BORDER_RIGHT, CellPropertyType.BORDER_RIGHT);
namePropertyMap.put(BOTTOM_BORDER_COLOR, CellPropertyType.BOTTOM_BORDER_COLOR); map.put(BORDER_TOP, CellPropertyType.BORDER_TOP);
namePropertyMap.put(LEFT_BORDER_COLOR, CellPropertyType.LEFT_BORDER_COLOR); map.put(BOTTOM_BORDER_COLOR, CellPropertyType.BOTTOM_BORDER_COLOR);
namePropertyMap.put(RIGHT_BORDER_COLOR, CellPropertyType.RIGHT_BORDER_COLOR); map.put(LEFT_BORDER_COLOR, CellPropertyType.LEFT_BORDER_COLOR);
namePropertyMap.put(TOP_BORDER_COLOR, CellPropertyType.TOP_BORDER_COLOR); map.put(RIGHT_BORDER_COLOR, CellPropertyType.RIGHT_BORDER_COLOR);
namePropertyMap.put(FILL_BACKGROUND_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR); map.put(TOP_BORDER_COLOR, CellPropertyType.TOP_BORDER_COLOR);
namePropertyMap.put(FILL_FOREGROUND_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR); map.put(FILL_BACKGROUND_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR);
namePropertyMap.put(FILL_BACKGROUND_COLOR_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); map.put(FILL_FOREGROUND_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR);
namePropertyMap.put(FILL_FOREGROUND_COLOR_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); map.put(FILL_BACKGROUND_COLOR_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR);
namePropertyMap.put(FILL_PATTERN, CellPropertyType.FILL_PATTERN); map.put(FILL_FOREGROUND_COLOR_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR);
namePropertyMap.put(FONT, CellPropertyType.FONT); map.put(FILL_PATTERN, CellPropertyType.FILL_PATTERN);
namePropertyMap.put(HIDDEN, CellPropertyType.HIDDEN); map.put(FONT, CellPropertyType.FONT);
namePropertyMap.put(INDENTION, CellPropertyType.INDENTION); map.put(HIDDEN, CellPropertyType.HIDDEN);
namePropertyMap.put(LOCKED, CellPropertyType.LOCKED); map.put(INDENTION, CellPropertyType.INDENTION);
namePropertyMap.put(ROTATION, CellPropertyType.ROTATION); map.put(LOCKED, CellPropertyType.LOCKED);
namePropertyMap.put(VERTICAL_ALIGNMENT, CellPropertyType.VERTICAL_ALIGNMENT); map.put(ROTATION, CellPropertyType.ROTATION);
namePropertyMap.put(SHRINK_TO_FIT, CellPropertyType.SHRINK_TO_FIT); map.put(VERTICAL_ALIGNMENT, CellPropertyType.VERTICAL_ALIGNMENT);
namePropertyMap.put(QUOTE_PREFIXED, CellPropertyType.QUOTE_PREFIXED); map.put(SHRINK_TO_FIT, CellPropertyType.SHRINK_TO_FIT);
map.put(QUOTE_PREFIXED, CellPropertyType.QUOTE_PREFIXED);
namePropertyMap = Collections.unmodifiableMap(map);
} }
private static final UnicodeMapping[] unicodeMappings; private static final UnicodeMapping[] unicodeMappings;

View File

@ -21,6 +21,7 @@ import static java.util.Calendar.SATURDAY;
import static java.util.Calendar.SUNDAY; import static java.util.Calendar.SUNDAY;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -508,11 +509,12 @@ public enum LocaleID {
private final int defaultCodepage; private final int defaultCodepage;
private final int firstWeekday; private final int firstWeekday;
private static final Map<String, LocaleID> languageTagLookup = private static final Map<String, LocaleID> languageTagLookup = Collections.unmodifiableMap(
Stream.of(values()).filter(LocaleID::isValid).collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity())); Stream.of(values()).filter(LocaleID::isValid)
.collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity())));
private static final Map<Integer, LocaleID> lcidLookup = private static final Map<Integer, LocaleID> lcidLookup = Collections.unmodifiableMap(
Stream.of(values()).collect(Collectors.toMap(LocaleID::getLcid, Function.identity())); Stream.of(values()).collect(Collectors.toMap(LocaleID::getLcid, Function.identity())));
LocaleID(int lcid, String windowsId, String languageTag, String description, int defaultCodepage, int firstWeekday) { LocaleID(int lcid, String windowsId, String languageTag, String description, int defaultCodepage, int firstWeekday) {
this.lcid = lcid; this.lcid = lcid;