From 9185ec02c5d814ae22bed7d2348650fb1bbf7ec8 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 26 Oct 2024 16:34:40 +0000 Subject: [PATCH] immutable maps git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921575 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/section/GeometryRowTypes.java | 5 +- .../usermodel/section/XDGFSectionTypes.java | 5 +- .../apache/poi/hslf/record/RecordTypes.java | 6 ++- .../poi/hslf/util/LocaleDateFormat.java | 9 ++-- .../apache/poi/ddf/EscherPropertyTypes.java | 5 +- .../org/apache/poi/ddf/EscherRecordTypes.java | 5 +- .../poi/hssf/record/HSSFRecordTypes.java | 5 +- .../org/apache/poi/hssf/record/SubRecord.java | 5 +- .../apache/poi/ss/usermodel/FormulaError.java | 21 +++++--- .../apache/poi/ss/usermodel/PageMargin.java | 7 +-- .../java/org/apache/poi/ss/util/CellUtil.java | 48 ++++++++++--------- .../java/org/apache/poi/util/LocaleID.java | 10 ++-- 12 files changed, 76 insertions(+), 55 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java index 7dd4641107..4c64af53fd 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java @@ -17,6 +17,7 @@ package org.apache.poi.xdgf.usermodel.section; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -84,6 +85,6 @@ enum GeometryRowTypes { return l.constructor.apply(row); } - private static final Map LOOKUP = - Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity()))); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java index 35af8d52be..887dfa0333 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java @@ -17,6 +17,7 @@ package org.apache.poi.xdgf.usermodel.section; +import java.util.Collections; import java.util.Map; import java.util.function.BiFunction; import java.util.function.Function; @@ -72,6 +73,6 @@ enum XDGFSectionTypes { return l.constructor.apply(section, containingSheet); } - private static final Map LOOKUP = - Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity()))); } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/RecordTypes.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/RecordTypes.java index f3cbf0b106..74e7ccf846 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/RecordTypes.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/record/RecordTypes.java @@ -17,6 +17,7 @@ package org.apache.poi.hslf.record; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -181,10 +182,11 @@ public enum RecordTypes { private static final Map LOOKUP; static { - LOOKUP = new HashMap<>(); + final Map map = new HashMap<>(); for(RecordTypes s : values()) { - LOOKUP.put(s.typeID, s); + map.put(s.typeID, s); } + LOOKUP = Collections.unmodifiableMap(map); } public final short typeID; diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/util/LocaleDateFormat.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/util/LocaleDateFormat.java index eb156e0177..3542fbf264 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/util/LocaleDateFormat.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/util/LocaleDateFormat.java @@ -21,6 +21,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.format.FormatStyle; import java.util.AbstractMap; +import java.util.Collections; import java.util.Locale; import java.util.Map; import java.util.function.Function; @@ -64,8 +65,8 @@ public final class LocaleDateFormat { private final LocaleID lcid; private final Object[] mapping; - private static final Map LCID_LOOKUP = - Stream.of(values()).collect(Collectors.toMap(MapFormatPPT::getLocaleID, Function.identity())); + private static final Map LCID_LOOKUP = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(MapFormatPPT::getLocaleID, Function.identity()))); MapFormatPPT(LocaleID lcid, Object... mapping) { this.lcid = lcid; @@ -195,9 +196,9 @@ public final class LocaleDateFormat { private final LocaleID[] lcid; private final Object[] mapping; - private static final Map LCID_LOOKUP = + private static final Map LCID_LOOKUP = Collections.unmodifiableMap( 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) { this.lcid = lcid; diff --git a/poi/src/main/java/org/apache/poi/ddf/EscherPropertyTypes.java b/poi/src/main/java/org/apache/poi/ddf/EscherPropertyTypes.java index a48972faf3..7502dbc974 100644 --- a/poi/src/main/java/org/apache/poi/ddf/EscherPropertyTypes.java +++ b/poi/src/main/java/org/apache/poi/ddf/EscherPropertyTypes.java @@ -17,6 +17,7 @@ package org.apache.poi.ddf; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -365,8 +366,8 @@ public enum EscherPropertyTypes { return propNumber; } - private static final Map LOOKUP = - Stream.of(values()).collect(Collectors.toMap(EscherPropertyTypes::getPropertyId, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(EscherPropertyTypes::getPropertyId, Function.identity()))); public static EscherPropertyTypes forPropertyID(int propertyId) { EscherPropertyTypes rt = LOOKUP.get((short)(propertyId & 0x3FFF)); diff --git a/poi/src/main/java/org/apache/poi/ddf/EscherRecordTypes.java b/poi/src/main/java/org/apache/poi/ddf/EscherRecordTypes.java index 4f11dab6c0..a8b589d7cb 100644 --- a/poi/src/main/java/org/apache/poi/ddf/EscherRecordTypes.java +++ b/poi/src/main/java/org/apache/poi/ddf/EscherRecordTypes.java @@ -17,6 +17,7 @@ package org.apache.poi.ddf; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; @@ -108,8 +109,8 @@ public enum EscherRecordTypes { return typeID; } - private static final Map LOOKUP = - Stream.of(values()).collect(Collectors.toMap(EscherRecordTypes::getTypeId, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(EscherRecordTypes::getTypeId, Function.identity()))); public static EscherRecordTypes forTypeID(int typeID) { // Section 2.2.23: 0xF02A is treated as 0xF01D diff --git a/poi/src/main/java/org/apache/poi/hssf/record/HSSFRecordTypes.java b/poi/src/main/java/org/apache/poi/hssf/record/HSSFRecordTypes.java index e8edcd9beb..cf96237be4 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/HSSFRecordTypes.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/HSSFRecordTypes.java @@ -20,6 +20,7 @@ package org.apache.poi.hssf.record; import java.util.Arrays; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -211,8 +212,8 @@ public enum HSSFRecordTypes { T apply(RecordInputStream in); } - private static final Map LOOKUP = - Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity()))); public final short sid; public final Class clazz; diff --git a/poi/src/main/java/org/apache/poi/hssf/record/SubRecord.java b/poi/src/main/java/org/apache/poi/hssf/record/SubRecord.java index 230e47b16c..ca16540ec8 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/SubRecord.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/SubRecord.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.record; import java.util.Arrays; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; @@ -64,8 +65,8 @@ public abstract class SubRecord implements Duplicatable, GenericRecord { T apply(LittleEndianInput in, int size, int cmoOt); } - private static final Map LOOKUP = - Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity())); + private static final Map LOOKUP = Collections.unmodifiableMap( + Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity()))); public final short sid; public final RecordConstructor recordConstructor; diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/FormulaError.java b/poi/src/main/java/org/apache/poi/ss/usermodel/FormulaError.java index 5ffdd6e47e..785bfa6905 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/FormulaError.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/FormulaError.java @@ -16,6 +16,7 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; +import java.util.Collections; import java.util.Map; import org.apache.poi.util.Internal; @@ -149,15 +150,21 @@ public enum FormulaError { return repr; } - private static final Map smap = new HashMap<>(); - private static final Map bmap = new HashMap<>(); - private static final Map imap = new HashMap<>(); - static{ + private static final Map smap; + private static final Map bmap; + private static final Map imap; + static { + final Map mapS = new HashMap<>(); + final Map mapB = new HashMap<>(); + final Map mapI = new HashMap<>(); for (FormulaError error : values()) { - bmap.put(error.getCode(), error); - imap.put(error.getLongCode(), error); - smap.put(error.getString(), error); + mapB.put(error.getCode(), error); + mapI.put(error.getLongCode(), error); + mapS.put(error.getString(), error); } + smap = Collections.unmodifiableMap(mapS); + bmap = Collections.unmodifiableMap(mapB); + imap = Collections.unmodifiableMap(mapI); } public static boolean isValidCode(int errorCode) { diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/PageMargin.java b/poi/src/main/java/org/apache/poi/ss/usermodel/PageMargin.java index 34c732c32f..907886a97d 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/PageMargin.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/PageMargin.java @@ -16,6 +16,7 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -75,11 +76,11 @@ public enum PageMargin { private static final Map PAGE_MARGIN_BY_LEGACY_API_VALUE; static { - PAGE_MARGIN_BY_LEGACY_API_VALUE = new HashMap<>(); - + final Map map = new HashMap<>(); 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); } /** diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index 593dd6eef9..6d3ebb3678 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -265,31 +265,33 @@ public final class CellUtil { public static final String QUOTE_PREFIXED = "quotePrefixed"; // FIXME Must be deleted along with string constants - static final Map namePropertyMap = new HashMap<>(); + static final Map namePropertyMap; static { - namePropertyMap.put(ALIGNMENT, CellPropertyType.ALIGNMENT); - namePropertyMap.put(BORDER_BOTTOM, CellPropertyType.BORDER_BOTTOM); - namePropertyMap.put(BORDER_LEFT, CellPropertyType.BORDER_LEFT); - namePropertyMap.put(BORDER_RIGHT, CellPropertyType.BORDER_RIGHT); - namePropertyMap.put(BORDER_TOP, CellPropertyType.BORDER_TOP); - namePropertyMap.put(BOTTOM_BORDER_COLOR, CellPropertyType.BOTTOM_BORDER_COLOR); - namePropertyMap.put(LEFT_BORDER_COLOR, CellPropertyType.LEFT_BORDER_COLOR); - namePropertyMap.put(RIGHT_BORDER_COLOR, CellPropertyType.RIGHT_BORDER_COLOR); - namePropertyMap.put(TOP_BORDER_COLOR, CellPropertyType.TOP_BORDER_COLOR); - namePropertyMap.put(FILL_BACKGROUND_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR); - namePropertyMap.put(FILL_FOREGROUND_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR); - namePropertyMap.put(FILL_BACKGROUND_COLOR_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); - namePropertyMap.put(FILL_FOREGROUND_COLOR_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); - namePropertyMap.put(FILL_PATTERN, CellPropertyType.FILL_PATTERN); - namePropertyMap.put(FONT, CellPropertyType.FONT); - namePropertyMap.put(HIDDEN, CellPropertyType.HIDDEN); - namePropertyMap.put(INDENTION, CellPropertyType.INDENTION); - namePropertyMap.put(LOCKED, CellPropertyType.LOCKED); - namePropertyMap.put(ROTATION, CellPropertyType.ROTATION); - namePropertyMap.put(VERTICAL_ALIGNMENT, CellPropertyType.VERTICAL_ALIGNMENT); - namePropertyMap.put(SHRINK_TO_FIT, CellPropertyType.SHRINK_TO_FIT); - namePropertyMap.put(QUOTE_PREFIXED, CellPropertyType.QUOTE_PREFIXED); + final Map map = new HashMap<>(); + map.put(ALIGNMENT, CellPropertyType.ALIGNMENT); + map.put(BORDER_BOTTOM, CellPropertyType.BORDER_BOTTOM); + map.put(BORDER_LEFT, CellPropertyType.BORDER_LEFT); + map.put(BORDER_RIGHT, CellPropertyType.BORDER_RIGHT); + map.put(BORDER_TOP, CellPropertyType.BORDER_TOP); + map.put(BOTTOM_BORDER_COLOR, CellPropertyType.BOTTOM_BORDER_COLOR); + map.put(LEFT_BORDER_COLOR, CellPropertyType.LEFT_BORDER_COLOR); + map.put(RIGHT_BORDER_COLOR, CellPropertyType.RIGHT_BORDER_COLOR); + map.put(TOP_BORDER_COLOR, CellPropertyType.TOP_BORDER_COLOR); + map.put(FILL_BACKGROUND_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR); + map.put(FILL_FOREGROUND_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR); + map.put(FILL_BACKGROUND_COLOR_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); + map.put(FILL_FOREGROUND_COLOR_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); + map.put(FILL_PATTERN, CellPropertyType.FILL_PATTERN); + map.put(FONT, CellPropertyType.FONT); + map.put(HIDDEN, CellPropertyType.HIDDEN); + map.put(INDENTION, CellPropertyType.INDENTION); + map.put(LOCKED, CellPropertyType.LOCKED); + map.put(ROTATION, CellPropertyType.ROTATION); + map.put(VERTICAL_ALIGNMENT, CellPropertyType.VERTICAL_ALIGNMENT); + 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; diff --git a/poi/src/main/java/org/apache/poi/util/LocaleID.java b/poi/src/main/java/org/apache/poi/util/LocaleID.java index 6438adf535..9b843e05ed 100644 --- a/poi/src/main/java/org/apache/poi/util/LocaleID.java +++ b/poi/src/main/java/org/apache/poi/util/LocaleID.java @@ -21,6 +21,7 @@ import static java.util.Calendar.SATURDAY; import static java.util.Calendar.SUNDAY; import java.util.Calendar; +import java.util.Collections; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -508,11 +509,12 @@ public enum LocaleID { private final int defaultCodepage; private final int firstWeekday; - private static final Map languageTagLookup = - Stream.of(values()).filter(LocaleID::isValid).collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity())); + private static final Map languageTagLookup = Collections.unmodifiableMap( + Stream.of(values()).filter(LocaleID::isValid) + .collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity()))); - private static final Map lcidLookup = - Stream.of(values()).collect(Collectors.toMap(LocaleID::getLcid, Function.identity())); + private static final Map lcidLookup = Collections.unmodifiableMap( + Stream.of(values()).collect(Collectors.toMap(LocaleID::getLcid, Function.identity()))); LocaleID(int lcid, String windowsId, String languageTag, String description, int defaultCodepage, int firstWeekday) { this.lcid = lcid; -- 2.39.5