]> source.dussan.org Git - poi.git/commitdiff
immutable maps
authorPJ Fanning <fanningpj@apache.org>
Sat, 26 Oct 2024 16:34:40 +0000 (16:34 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 26 Oct 2024 16:34:40 +0000 (16:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921575 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java
poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java
poi-scratchpad/src/main/java/org/apache/poi/hslf/record/RecordTypes.java
poi-scratchpad/src/main/java/org/apache/poi/hslf/util/LocaleDateFormat.java
poi/src/main/java/org/apache/poi/ddf/EscherPropertyTypes.java
poi/src/main/java/org/apache/poi/ddf/EscherRecordTypes.java
poi/src/main/java/org/apache/poi/hssf/record/HSSFRecordTypes.java
poi/src/main/java/org/apache/poi/hssf/record/SubRecord.java
poi/src/main/java/org/apache/poi/ss/usermodel/FormulaError.java
poi/src/main/java/org/apache/poi/ss/usermodel/PageMargin.java
poi/src/main/java/org/apache/poi/ss/util/CellUtil.java
poi/src/main/java/org/apache/poi/util/LocaleID.java

index 7dd4641107bf2f00ee3112807b6a1a927c1e33df..4c64af53fd82a32899736722a39db9f8591aff8c 100644 (file)
@@ -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<String, GeometryRowTypes> LOOKUP =
-        Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity()));
+    private static final Map<String, GeometryRowTypes> LOOKUP = Collections.unmodifiableMap(
+        Stream.of(values()).collect(Collectors.toMap(GeometryRowTypes::getRowType, Function.identity())));
 }
index 35af8d52be30ed7a5b4c1a99b3f4f14ae515e839..887dfa033376d83d9617f0c0401672a1ca34db74 100644 (file)
@@ -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<String, XDGFSectionTypes> LOOKUP =
-        Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity()));
+    private static final Map<String, XDGFSectionTypes> LOOKUP = Collections.unmodifiableMap(
+        Stream.of(values()).collect(Collectors.toMap(XDGFSectionTypes::getSectionType, Function.identity())));
 }
index f3cbf0b1068b6fa8e4014521a02a55909d09774c..74e7ccf846a3a05544c1846bf5009a31a0d0329c 100644 (file)
@@ -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<Short,RecordTypes> LOOKUP;
 
     static {
-        LOOKUP = new HashMap<>();
+        final Map<Short,RecordTypes> 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;
index eb156e01776d038d3afb9d0123b07ac86b0bd2aa..3542fbf264c9411a1cc23bcdf265628d29add604 100644 (file)
@@ -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<LocaleID,MapFormatPPT> LCID_LOOKUP =
-            Stream.of(values()).collect(Collectors.toMap(MapFormatPPT::getLocaleID, Function.identity()));
+        private static final Map<LocaleID,MapFormatPPT> 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<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)))
-                .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;
index a48972faf3eb8e0210d27a47bd5e5f6a6e569864..7502dbc974fd08d4a695074ce9a38ea88ced9365 100644 (file)
@@ -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<Short, EscherPropertyTypes> LOOKUP =
-            Stream.of(values()).collect(Collectors.toMap(EscherPropertyTypes::getPropertyId, Function.identity()));
+    private static final Map<Short, EscherPropertyTypes> 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));
index 4f11dab6c090e8d223eb4626225702e862285e01..a8b589d7cb7e2593fb240dd289e7e1ca8d8d7a81 100644 (file)
@@ -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<Short, EscherRecordTypes> LOOKUP =
-        Stream.of(values()).collect(Collectors.toMap(EscherRecordTypes::getTypeId, Function.identity()));
+    private static final Map<Short, EscherRecordTypes> 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
index e8edcd9beb976ba5b8c6823bb8cfc83877d43955..cf96237be44a868e4fba67b26807b3ada2cecc90 100644 (file)
@@ -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<Short,HSSFRecordTypes> LOOKUP =
-        Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity()));
+    private static final Map<Short,HSSFRecordTypes> LOOKUP = Collections.unmodifiableMap(
+        Arrays.stream(values()).collect(Collectors.toMap(HSSFRecordTypes::getSid, Function.identity())));
 
     public final short sid;
     public final Class<? extends org.apache.poi.hssf.record.Record> clazz;
index 230e47b16ce9b740f3baaba01d8372bdcbe243fd..ca16540ec8d94fd2c8fc9dcbc1124c65f5100a86 100644 (file)
@@ -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<Short,SubRecordTypes> LOOKUP =
-            Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity()));
+        private static final Map<Short,SubRecordTypes> LOOKUP = Collections.unmodifiableMap(
+            Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity())));
 
         public final short sid;
         public final RecordConstructor<?> recordConstructor;
index 5ffdd6e47e70987e4a53c8b157c3e65d8d9f187f..785bfa69059126d5e6a9be43094e60b947d004c8 100644 (file)
@@ -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<String, FormulaError> smap = new HashMap<>();
-    private static final Map<Byte, FormulaError> bmap = new HashMap<>();
-    private static final Map<Integer, FormulaError> imap = new HashMap<>();
-    static{
+    private static final Map<String, FormulaError> smap;
+    private static final Map<Byte, FormulaError> bmap;
+    private static final Map<Integer, FormulaError> imap;
+    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()) {
-            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) {
index 34c732c32f97b9456758a50638cff47df497a6f3..907886a97d74bf7356b62bfe2a385f217a3df6c8 100644 (file)
@@ -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<Short, PageMargin> PAGE_MARGIN_BY_LEGACY_API_VALUE;
 
     static {
-        PAGE_MARGIN_BY_LEGACY_API_VALUE = new HashMap<>();
-
+        final Map<Short, PageMargin> 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);
     }
 
     /**
index 593dd6eef984d2fc69555bdd51e46c35f28de539..6d3ebb36787b7d1e99d08cd16f6fd06054120cd8 100644 (file)
@@ -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<String, CellPropertyType> namePropertyMap = new HashMap<>();
+    static final Map<String, CellPropertyType> 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<String, CellPropertyType> 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;
index 6438adf535dc22301956e97bce76af0e1f2d7386..9b843e05eda58de7efbaf7cd0d78a4f299568d0f 100644 (file)
@@ -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<String, LocaleID> languageTagLookup =
-        Stream.of(values()).filter(LocaleID::isValid).collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity()));
+    private static final Map<String, LocaleID> languageTagLookup = Collections.unmodifiableMap(
+        Stream.of(values()).filter(LocaleID::isValid)
+            .collect(Collectors.toMap(LocaleID::getLanguageTag, Function.identity())));
 
-    private static final Map<Integer, LocaleID> lcidLookup =
-        Stream.of(values()).collect(Collectors.toMap(LocaleID::getLcid, Function.identity()));
+    private static final Map<Integer, LocaleID> 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;