]> source.dussan.org Git - poi.git/commitdiff
try to make ExcelAntWorkbookUtilFactory thread safe
authorPJ Fanning <fanningpj@apache.org>
Tue, 15 Oct 2024 16:13:17 +0000 (16:13 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 15 Oct 2024 16:13:17 +0000 (16:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921338 13f79535-47bb-0310-9956-ffa450edef68

poi-excelant/src/main/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilFactory.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java

index 55c48967055380c790c334ef845fa0963473edbf..7838a5d6eb0f8576b93c02d945ad588ebc4c1c2a 100644 (file)
@@ -27,7 +27,7 @@ import java.util.Map;
  */
 public final class ExcelAntWorkbookUtilFactory {
 
-    private static Map<String, ExcelAntWorkbookUtil> workbookUtilMap;
+    private static final Map<String, ExcelAntWorkbookUtil> workbookUtilMap = new HashMap<>();
 
     private ExcelAntWorkbookUtilFactory() {
     }
@@ -41,16 +41,14 @@ public final class ExcelAntWorkbookUtilFactory {
      *          a freshly instantiated one if none did exist before.
      */
     public static ExcelAntWorkbookUtil getInstance(String fileName) {
-        if(workbookUtilMap == null) {
-            workbookUtilMap = new HashMap<>();
+        synchronized (workbookUtilMap) {
+            if(workbookUtilMap.containsKey(fileName)) {
+                return workbookUtilMap.get(fileName);
+            }
+
+            ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName);
+            workbookUtilMap.put(fileName, wbu);
+            return wbu;
         }
-
-        if(workbookUtilMap.containsKey(fileName)) {
-            return workbookUtilMap.get(fileName);
-        }
-
-        ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName);
-        workbookUtilMap.put(fileName, wbu);
-        return wbu;
     }
 }
index 8e6df981b4e91b12f889f457cb1dd272a3851368..16a84cf26e7cb5c2acb97e382c327452522993a0 100644 (file)
@@ -16,6 +16,7 @@
 ==================================================================== */
 package org.apache.poi.xwpf.usermodel;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -45,12 +46,14 @@ public enum VerticalAlign {
      */
     SUBSCRIPT(3);
 
-    private static final Map<Integer, VerticalAlign> imap = new HashMap<>();
+    private static final Map<Integer, VerticalAlign> imap;
 
     static {
+        final Map<Integer, VerticalAlign> tempMap = new HashMap<>();
         for (VerticalAlign p : values()) {
-            imap.put(p.getValue(), p);
+            tempMap.put(p.getValue(), p);
         }
+        imap = Collections.unmodifiableMap(tempMap);
     }
 
     private final int value;