From 691c8baa5a2fe35f59420e25a3348d798ddefea3 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 24 Apr 2025 11:21:36 +0000 Subject: [PATCH] [bug-69658] use EnumMap elsewhere git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1925250 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/ss/util/CellUtil.java | 5 ++- .../apache/poi/ss/util/PropertyTemplate.java | 36 ++++++++++--------- .../apache/poi/ss/util/BaseTestCellUtil.java | 5 +-- 3 files changed, 25 insertions(+), 21 deletions(-) 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 69e1f9084b..d5f8dc3d2c 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 @@ -571,7 +571,7 @@ public final class CellUtil { @Deprecated @Removal(version = "7.0.0") public static void setCellStyleProperties(Cell cell, Map properties) { - Map strPropMap = new HashMap<>(properties.size()); + EnumMap strPropMap = new EnumMap<>(CellPropertyType.class); properties.forEach((k, v) -> strPropMap.put(namePropertyMap.get(k), v)); setCellStyleProperties(cell, strPropMap, false); } @@ -611,7 +611,7 @@ public final class CellUtil { CellStyle originalStyle = cell.getCellStyle(); CellStyle newStyle = null; - Map values = getFormatProperties(originalStyle); + EnumMap values = getFormatProperties(originalStyle); if (properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) && properties.get(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) == null) { values.remove(CellPropertyType.FILL_FOREGROUND_COLOR); } @@ -777,7 +777,6 @@ public final class CellUtil { * * @param src the property map to copy from (read-only) * @param dest the property map to copy into - * @since POI 3.15 beta 3 */ private static void putAll(final Map src, Map dest) { for (final CellPropertyType key : src.keySet()) { diff --git a/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java b/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java index 2f74af55d7..e06b7e8aef 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java +++ b/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java @@ -28,7 +28,10 @@ import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.Removal; +import java.util.EnumMap; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -42,9 +45,9 @@ import java.util.Set; * sheet in any workbook. * * This class requires the full spreadsheet to be in memory, so - * {@link org.apache.poi.xssf.streaming.SXSSFWorkbook} Spreadsheets are not + * SXSSFWorkbook Spreadsheets are not * supported. The same PropertyTemplate can, however, be applied to both - * {@link HSSFWorkbook} and {@link org.apache.poi.xssf.usermodel.XSSFWorkbook} + * {@link HSSFWorkbook} and XSSFWorkbook * objects if necessary. Portions of the border that fall outside the max range * of the {@link Workbook} sheet are ignored. *

@@ -59,7 +62,7 @@ public final class PropertyTemplate { * This is a list of cell properties for one shot application to a range of * cells at a later time. */ - private final Map> _propertyTemplate; + private final Map> _propertyTemplate; /** * Create a PropertyTemplate object @@ -75,17 +78,17 @@ public final class PropertyTemplate { */ public PropertyTemplate(PropertyTemplate template) { this(); - for (Map.Entry> entry : template.getTemplate().entrySet()) { + for (Map.Entry> entry : template.getTemplate().entrySet()) { _propertyTemplate.put(new CellAddress(entry.getKey()), cloneCellProperties(entry.getValue())); } } - private Map> getTemplate() { + private Map> getTemplate() { return _propertyTemplate; } - private static Map cloneCellProperties(Map properties) { - return new HashMap<>(properties); + private static EnumMap cloneCellProperties(EnumMap properties) { + return new EnumMap<>(properties); } /** @@ -433,7 +436,7 @@ public final class PropertyTemplate { */ public void applyBorders(Sheet sheet) { Workbook wb = sheet.getWorkbook(); - for (Map.Entry> entry : _propertyTemplate + for (Map.Entry> entry : _propertyTemplate .entrySet()) { CellAddress cellAddress = entry.getKey(); if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows() @@ -756,11 +759,11 @@ public final class PropertyTemplate { * @param range - {@link CellRangeAddress} range of cells to remove borders. */ private void removeBorderColors(CellRangeAddress range) { - Set properties = new HashSet<>(); - properties.add(CellPropertyType.TOP_BORDER_COLOR); - properties.add(CellPropertyType.BOTTOM_BORDER_COLOR); - properties.add(CellPropertyType.LEFT_BORDER_COLOR); - properties.add(CellPropertyType.RIGHT_BORDER_COLOR); + Set properties = EnumSet.of( + CellPropertyType.TOP_BORDER_COLOR, + CellPropertyType.BOTTOM_BORDER_COLOR, + CellPropertyType.LEFT_BORDER_COLOR, + CellPropertyType.RIGHT_BORDER_COLOR); for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) { for (int col = range.getFirstColumn(); col <= range .getLastColumn(); col++) { @@ -781,9 +784,9 @@ public final class PropertyTemplate { */ private void addProperty(int row, int col, CellPropertyType property, Object value) { CellAddress cell = new CellAddress(row, col); - Map cellProperties = _propertyTemplate.get(cell); + EnumMap cellProperties = _propertyTemplate.get(cell); if (cellProperties == null) { - cellProperties = new HashMap<>(); + cellProperties = new EnumMap<>(CellPropertyType.class); } cellProperties.put(property, value); _propertyTemplate.put(cell, cellProperties); @@ -795,7 +798,7 @@ public final class PropertyTemplate { */ private void removeProperties(int row, int col, Set properties) { CellAddress cell = new CellAddress(row, col); - Map cellProperties = _propertyTemplate.get(cell); + EnumMap cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { cellProperties.keySet().removeAll(properties); if (cellProperties.isEmpty()) { @@ -946,6 +949,7 @@ public final class PropertyTemplate { * @deprecated See {@link #getTemplateProperty(int, int, CellPropertyType)} */ @Deprecated + @Removal(version = "7.0.0") public short getTemplateProperty(int row, int col, String propertyName) { return getTemplateProperty(new CellAddress(row, col), CellUtil.namePropertyMap.get(propertyName)); } diff --git a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java index 9784e080b5..fb2200ba57 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java +++ b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java @@ -22,6 +22,7 @@ import org.apache.poi.ss.usermodel.*; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; @@ -339,7 +340,7 @@ public abstract class BaseTestCellUtil { // Add multiple border properties to cell should create a single new style int styCnt1 = wb.getNumCellStyles(); - Map props = new HashMap<>(); + EnumMap props = new EnumMap<>(CellPropertyType.class); props.put(CellPropertyType.BORDER_TOP, BorderStyle.THIN); props.put(CellPropertyType.BORDER_BOTTOM, BorderStyle.THIN); props.put(CellPropertyType.BORDER_LEFT, BorderStyle.THIN); @@ -574,7 +575,7 @@ public abstract class BaseTestCellUtil { protected void setFillForegroundColorBeforeFillBackgroundColorEnumByEnum() throws IOException { try (Workbook wb1 = _testDataProvider.createWorkbook()) { Cell A1 = wb1.createSheet().createRow(0).createCell(0); - Map properties = new HashMap<>(); + EnumMap properties = new EnumMap<>(CellPropertyType.class); properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.BRICKS); properties.put(CellPropertyType.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index); properties.put(CellPropertyType.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);