From dc6a4a99e2d077ff9c9a343e218070d4df14d48d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 14 Sep 2019 09:54:11 +0000 Subject: [PATCH] try to avoid casting to int git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866933 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/view/SVSheetTable.java | 2 +- .../src/org/apache/poi/ss/examples/ToCSV.java | 2 +- .../apache/poi/ss/examples/html/ToHtml.java | 2 +- src/java/org/apache/poi/hpsf/Section.java | 16 ++++++------ .../poi/ss/formula/atp/ParityFunction.java | 2 +- .../org/apache/poi/ss/util/CellReference.java | 2 +- .../apache/poi/ss/util/ExpandedDouble.java | 2 +- .../org/apache/poi/ss/util/IEEEDouble.java | 2 +- src/java/org/apache/poi/util/HexDump.java | 2 +- .../openxml4j/util/ZipFileZipEntrySource.java | 7 +++-- .../util/ZipInputStreamZipEntrySource.java | 4 +-- .../org/apache/poi/xddf/usermodel/Angles.java | 2 +- .../usermodel/text/XDDFBulletSizePercent.java | 2 +- .../usermodel/text/XDDFBulletSizePoints.java | 2 +- .../usermodel/text/XDDFRunProperties.java | 2 +- .../poi/xslf/usermodel/XMLSlideShow.java | 3 +-- .../apache/poi/xslf/usermodel/XSLFShape.java | 6 ++--- .../poi/xslf/usermodel/XSLFSimpleShape.java | 5 ++-- .../org/apache/poi/xslf/util/PPTX2PNG.java | 4 +-- .../apache/poi/xssf/model/CommentsTable.java | 2 +- .../xssf/streaming/AutoSizeColumnTracker.java | 2 +- .../poi/xssf/streaming/OpcOutputStream.java | 2 +- .../apache/poi/xssf/usermodel/XSSFCell.java | 7 ++--- .../poi/xssf/usermodel/XSSFCellStyle.java | 18 ++++++------- .../poi/xssf/usermodel/XSSFClientAnchor.java | 8 +++--- .../apache/poi/xssf/usermodel/XSSFRow.java | 4 +-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 26 +++++++++---------- .../usermodel/helpers/XSSFRowColShifter.java | 2 +- .../apache/poi/ss/util/TestCellRangeUtil.java | 7 +++-- 29 files changed, 74 insertions(+), 73 deletions(-) diff --git a/src/examples/src/org/apache/poi/hssf/view/SVSheetTable.java b/src/examples/src/org/apache/poi/hssf/view/SVSheetTable.java index d0889cc11f..ebfb3922ea 100644 --- a/src/examples/src/org/apache/poi/hssf/view/SVSheetTable.java +++ b/src/examples/src/org/apache/poi/hssf/view/SVSheetTable.java @@ -182,7 +182,7 @@ public class SVSheetTable extends JTable { Row row = sheet.getRow(i - sheet.getFirstRowNum()); if (row != null) { short h = row.getHeight(); - int height = (int)Math.round(Math.max(1., h / (res / 70. * 20.) + 3.)); + int height = Math.toIntExact(Math.round(Math.max(1., h / (res / 70. * 20.) + 3.))); System.out.printf("%d: %d (%d @ %d)%n", i, height, h, res); setRowHeight(i, height); } diff --git a/src/examples/src/org/apache/poi/ss/examples/ToCSV.java b/src/examples/src/org/apache/poi/ss/examples/ToCSV.java index 23da1e1f02..8b9ca2822b 100644 --- a/src/examples/src/org/apache/poi/ss/examples/ToCSV.java +++ b/src/examples/src/org/apache/poi/ss/examples/ToCSV.java @@ -703,7 +703,7 @@ public class ToCSV { if (converted) { System.out.println("Conversion took " + - (int)((System.currentTimeMillis() - startTime)/1000) + " seconds"); + ((System.currentTimeMillis() - startTime)/1000) + " seconds"); } } diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java index 622a38b80f..74221715a9 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java @@ -399,7 +399,7 @@ public class ToHtml { * @return the approximate number of pixels for a typical display */ protected int widthToPixels(final double widthUnits) { - return (int) (Math.round(widthUnits * 9 / 256)); + return Math.toIntExact(Math.round(widthUnits * 9 / 256)); } private void printCols(Map widths) { diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java index 0c1bcf263d..5c78402701 100644 --- a/src/java/org/apache/poi/hpsf/Section.java +++ b/src/java/org/apache/poi/hpsf/Section.java @@ -182,10 +182,10 @@ public class Section { final TreeBidiMap offset2Id = new TreeBidiMap<>(); for (int i = 0; i < propertyCount; i++) { /* Read the property ID. */ - long id = (int)leis.readUInt(); + long id = leis.readUInt(); /* Offset from the section's start. */ - long off = (int)leis.readUInt(); + long off = leis.readUInt(); offset2Id.put(off, id); } @@ -196,7 +196,7 @@ public class Section { int codepage = -1; if (cpOffset != null) { /* Read the property's value type. It must be VT_I2. */ - leis.setReadIndex((int)(this._offset + cpOffset)); + leis.setReadIndex(Math.toIntExact(this._offset + cpOffset)); final long type = leis.readUInt(); if (type != Variant.VT_I2) { @@ -221,7 +221,7 @@ public class Section { } int pLen = propLen(offset2Id, off, size); - leis.setReadIndex((int)(this._offset + off)); + leis.setReadIndex(Math.toIntExact(this._offset + off)); if (id == PropertyIDMap.PID_DICTIONARY) { leis.mark(100000); @@ -242,7 +242,7 @@ public class Section { } } - sectionBytes.write(src, (int)_offset, size); + sectionBytes.write(src, Math.toIntExact(_offset), size); padSectionBytes(); } @@ -261,7 +261,7 @@ public class Section { Long nextKey = offset2Id.nextKey(entryOffset); long begin = entryOffset; long end = (nextKey != null) ? nextKey : maxSize; - return (int)(end - begin); + return Math.toIntExact(end - begin); } @@ -823,7 +823,7 @@ public class Section { /* Read the string - Strip 0x00 characters from the end of the string. */ int cp = (codepage == -1) ? Property.DEFAULT_CODEPAGE : codepage; - int nrBytes = (int)((sLength-1) * (cp == CodePageUtil.CP_UNICODE ? 2 : 1)); + int nrBytes = Math.toIntExact(((sLength-1) * (cp == CodePageUtil.CP_UNICODE ? 2 : 1))); if (nrBytes > 0xFFFFFF) { LOG.log(POILogger.WARN, errMsg); isCorrupted = true; @@ -946,7 +946,7 @@ public class Section { for (Property aPa : pa) { hashCode += aPa.hashCode(); } - return (int) (hashCode & 0x0ffffffffL); + return Math.toIntExact(hashCode & 0x0ffffffffL); } diff --git a/src/java/org/apache/poi/ss/formula/atp/ParityFunction.java b/src/java/org/apache/poi/ss/formula/atp/ParityFunction.java index 29012fe18c..b59b4808b5 100644 --- a/src/java/org/apache/poi/ss/formula/atp/ParityFunction.java +++ b/src/java/org/apache/poi/ss/formula/atp/ParityFunction.java @@ -62,6 +62,6 @@ final class ParityFunction implements FreeRefFunction { d = -d; } long v = (long) Math.floor(d); - return (int) (v & 0x0001); + return Math.toIntExact(v & 0x0001); } } diff --git a/src/java/org/apache/poi/ss/util/CellReference.java b/src/java/org/apache/poi/ss/util/CellReference.java index 6e6dbe678d..9f88bcaecb 100644 --- a/src/java/org/apache/poi/ss/util/CellReference.java +++ b/src/java/org/apache/poi/ss/util/CellReference.java @@ -354,7 +354,7 @@ public class CellReference { if(rowNum > Integer.MAX_VALUE) { return false; } - return isRowWithinRange((int)rowNum, ssVersion); + return isRowWithinRange(Math.toIntExact(rowNum), ssVersion); } /** diff --git a/src/java/org/apache/poi/ss/util/ExpandedDouble.java b/src/java/org/apache/poi/ss/util/ExpandedDouble.java index 24ef95cacc..40d4066af8 100644 --- a/src/java/org/apache/poi/ss/util/ExpandedDouble.java +++ b/src/java/org/apache/poi/ss/util/ExpandedDouble.java @@ -54,7 +54,7 @@ final class ExpandedDouble { private final int _binaryExponent; public ExpandedDouble(long rawBits) { - int biasedExp = (int) (rawBits >> 52); + int biasedExp = Math.toIntExact(rawBits >> 52); if (biasedExp == 0) { // sub-normal numbers BigInteger frac = BigInteger.valueOf(rawBits).and(BI_FRAC_MASK); diff --git a/src/java/org/apache/poi/ss/util/IEEEDouble.java b/src/java/org/apache/poi/ss/util/IEEEDouble.java index f5a42edca6..91d8fea434 100644 --- a/src/java/org/apache/poi/ss/util/IEEEDouble.java +++ b/src/java/org/apache/poi/ss/util/IEEEDouble.java @@ -39,6 +39,6 @@ final class IEEEDouble { * @return the top 12 bits (sign and biased exponent value) */ public static int getBiasedExponent(long rawBits) { - return (int) ((rawBits & EXPONENT_MASK) >> EXPONENT_SHIFT); + return Math.toIntExact((rawBits & EXPONENT_MASK) >> EXPONENT_SHIFT); } } diff --git a/src/java/org/apache/poi/util/HexDump.java b/src/java/org/apache/poi/util/HexDump.java index d3edc2843f..5afd1dbe18 100644 --- a/src/java/org/apache/poi/util/HexDump.java +++ b/src/java/org/apache/poi/util/HexDump.java @@ -401,7 +401,7 @@ public class HexDump { char[] buf = new char[nDigits]; long acc = value; for(int i=nDigits-1; i>=0; i--) { - int digit = (int)(acc & 0x0F); + int digit = Math.toIntExact(acc & 0x0F); buf[i] = (char) (digit < 10 ? ('0' + digit) : ('A' + digit - 10)); acc >>>= 4; } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java index 363ba9d5a7..0dafb13443 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java @@ -16,9 +16,6 @@ ==================================================================== */ package org.apache.poi.openxml4j.util; -import static org.apache.commons.collections4.IteratorUtils.asIterable; -import static org.apache.commons.collections4.IteratorUtils.asIterator; - import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; @@ -76,7 +73,9 @@ public class ZipFileZipEntrySource implements ZipEntrySource { } // the opc spec allows case-insensitive filename matching (see #49609) - for (final ZipArchiveEntry ze : asIterable(asIterator(zipArchive.getEntries()))) { + final Enumeration zipArchiveEntryEnumeration = zipArchive.getEntries(); + while (zipArchiveEntryEnumeration.hasMoreElements()) { + ZipArchiveEntry ze = zipArchiveEntryEnumeration.nextElement(); if (normalizedPath.equalsIgnoreCase(ze.getName().replace('\\','/'))) { return ze; } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java index ef8d20eb76..57103d2906 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java @@ -18,11 +18,11 @@ package org.apache.poi.openxml4j.util; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; /** @@ -58,7 +58,7 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource { @Override public Enumeration getEntries() { - return IteratorUtils.asEnumeration(zipEntries.values().iterator()); + return Collections.enumeration(zipEntries.values()); } @Override diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/Angles.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/Angles.java index f31e6ae464..9f08483b60 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/Angles.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/Angles.java @@ -27,7 +27,7 @@ public class Angles { public static final int OOXML_DEGREE = 60_000; public static final int degreesToAttribute(double angle) { - return (int) (OOXML_DEGREE * angle); + return Math.toIntExact(Math.round(OOXML_DEGREE * angle)); } public static final double attributeToDegrees(int angle) { diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePercent.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePercent.java index 56e2125791..6426c8e32b 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePercent.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePercent.java @@ -47,6 +47,6 @@ public class XDDFBulletSizePercent implements XDDFBulletSize { } public void setPercent(double value) { - percent.setVal((int) (1000 * value)); + percent.setVal(Math.toIntExact(Math.round(1000 * value))); } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePoints.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePoints.java index 342ab72e0f..0845087b7c 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePoints.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePoints.java @@ -45,6 +45,6 @@ public class XDDFBulletSizePoints implements XDDFBulletSize { } public void setPoints(double value) { - points.setVal((int) (100 * value)); + points.setVal(Math.toIntExact(Math.round(100 * value))); } } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFRunProperties.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFRunProperties.java index 8025d7cb32..6a18cf6db2 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFRunProperties.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFRunProperties.java @@ -142,7 +142,7 @@ public class XDDFRunProperties { } else if (size < 1 || 400 < size) { throw new IllegalArgumentException("Minimum inclusive = 1. Maximum inclusive = 400."); } else { - props.setSz((int) (100 * size)); + props.setSz((int)(100 * size)); } } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index ff12d6aad2..2045d59969 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -542,8 +542,7 @@ public class XMLSlideShow extends POIXMLDocument */ @Override public XSLFPictureData addPicture(File pict, PictureType format) throws IOException { - int length = (int) pict.length(); - byte[] data = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); + byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH); try (InputStream is = new FileInputStream(pict)) { IOUtils.readFully(is, data); } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java index d6c230d436..3711fdfc1a 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java @@ -577,10 +577,10 @@ public abstract class XSLFShape implements Shape { // value of 0 or 1000 indicates no background, // values 1-999 refer to the index of a fill style within the fillStyleLst element // values 1001 and above refer to the index of a background fill style within the bgFillStyleLst element. - int idx = (int)fillRef.getIdx(); + long idx = fillRef.getIdx(); CTStyleMatrix matrix = theme.getXmlObject().getThemeElements().getFmtScheme(); final XmlObject styleLst; - int childIdx; + long childIdx; if (idx >= 1 && idx <= 999) { childIdx = idx-1; styleLst = (isLineStyle) ? matrix.getLnStyleLst() : matrix.getFillStyleLst(); @@ -592,7 +592,7 @@ public abstract class XSLFShape implements Shape { } XmlCursor cur = styleLst.newCursor(); XSLFFillProperties fp = null; - if (cur.toChild(childIdx)) { + if (cur.toChild(Math.toIntExact(childIdx))) { fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject()); } cur.dispose(); diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java index 64ff9a9766..4d98ecb2eb 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java @@ -78,7 +78,6 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType; import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth; import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal; import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType; -import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder; /** * Represents a single (non-group) shape in a .pptx slide show @@ -242,7 +241,7 @@ public abstract class XSLFSimpleShape extends XSLFShape return null; } // 1-based index of a line style within the style matrix - int idx = (int)lnRef.getIdx(); + int idx = Math.toIntExact(lnRef.getIdx()); XSLFTheme theme = getSheet().getTheme(); if (theme == null) { @@ -359,7 +358,7 @@ public abstract class XSLFSimpleShape extends XSLFShape if (lnRef == null) { return null; } - int idx = (int)lnRef.getIdx(); + int idx = Math.toIntExact(lnRef.getIdx()); CTSchemeColor phClr = lnRef.getSchemeClr(); if(idx <= 0){ return null; diff --git a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java index 43c7f398e1..c03c5579bd 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java @@ -216,8 +216,8 @@ public class PPTX2PNG { break; } - final int width = (int) Math.rint(pgsize.getWidth() * scale / lenSide); - final int height = (int) Math.rint(pgsize.getHeight() * scale / lenSide); + final int width = Math.toIntExact(Math.round(pgsize.getWidth() * scale / lenSide)); + final int height = Math.toIntExact(Math.round(pgsize.getHeight() * scale / lenSide)); for (int slideNo : slidenum) { diff --git a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java index 61a98e3ca9..fcc592333b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java @@ -119,7 +119,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { @Override public String getAuthor(long authorId) { - return comments.getAuthors().getAuthorArray((int)authorId); + return comments.getAuthors().getAuthorArray(Math.toIntExact(authorId)); } @Override diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java b/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java index e8975ecb37..583ef3c6a2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java @@ -281,7 +281,7 @@ import org.apache.poi.util.Internal; } } final double width = maxColumnWidths.get(column).getMaxColumnWidth(useMergedCells); - return (int) (256*width); + return Math.toIntExact(Math.round(256*width)); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/OpcOutputStream.java b/src/ooxml/java/org/apache/poi/xssf/streaming/OpcOutputStream.java index 5cbf536fb0..b8253c054d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/OpcOutputStream.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/OpcOutputStream.java @@ -85,7 +85,7 @@ class OpcOutputStream extends DeflaterOutputStream { } current.size = def.getBytesRead(); - current.compressedSize = (int) def.getBytesWritten(); + current.compressedSize = Math.toIntExact(def.getBytesWritten()); current.crc = crc.getValue(); written += current.compressedSize; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 93e265d250..9976527511 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -465,7 +465,8 @@ public final class XSSFCell extends CellBase { if (f == null) { return null; } else if (f.getT() == STCellFormulaType.SHARED) { - return convertSharedFormula((int)f.getSi(), fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb); + return convertSharedFormula(Math.toIntExact(f.getSi()), + fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb); } else { return f.getStringValue(); } @@ -614,7 +615,7 @@ public final class XSSFCell extends CellBase { XSSFCellStyle style = null; if(_stylesSource.getNumCellStyles() > 0){ long idx = _cell.isSetS() ? _cell.getS() : 0; - style = _stylesSource.getStyleAt((int)idx); + style = _stylesSource.getStyleAt(Math.toIntExact(idx)); } return style; } @@ -1224,7 +1225,7 @@ public final class XSSFCell extends CellBase { } CalculationChain calcChain = getSheet().getWorkbook().getCalculationChain(); - int sheetId = (int)getSheet().sheet.getSheetId(); + int sheetId = Math.toIntExact(getSheet().sheet.getSheetId()); //remove the reference in the calculation chain if(calcChain != null) calcChain.removeItem(sheetId, getReference()); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index f92b6ed668..ba366b5317 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -236,7 +236,7 @@ public class XSSFCellStyle implements CellStyle { public BorderStyle getBorderBottom() { if(!_cellXf.getApplyBorder()) return BorderStyle.NONE; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder(); STBorderStyle.Enum ptrn = ct.isSetBottom() ? ct.getBottom().getStyle() : null; if (ptrn == null) { @@ -254,7 +254,7 @@ public class XSSFCellStyle implements CellStyle { public BorderStyle getBorderLeft() { if(!_cellXf.getApplyBorder()) return BorderStyle.NONE; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder(); STBorderStyle.Enum ptrn = ct.isSetLeft() ? ct.getLeft().getStyle() : null; if (ptrn == null) { @@ -270,7 +270,7 @@ public class XSSFCellStyle implements CellStyle { public BorderStyle getBorderRight() { if(!_cellXf.getApplyBorder()) return BorderStyle.NONE; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder(); STBorderStyle.Enum ptrn = ct.isSetRight() ? ct.getRight().getStyle() : null; if (ptrn == null) { @@ -288,7 +288,7 @@ public class XSSFCellStyle implements CellStyle { public BorderStyle getBorderTop() { if(!_cellXf.getApplyBorder()) return BorderStyle.NONE; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder(); STBorderStyle.Enum ptrn = ct.isSetTop() ? ct.getTop().getStyle() : null; if (ptrn == null) { @@ -323,7 +323,7 @@ public class XSSFCellStyle implements CellStyle { public XSSFColor getBottomBorderXSSFColor() { if(!_cellXf.getApplyBorder()) return null; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); XSSFCellBorder border = _stylesSource.getBorderAt(idx); return border.getBorderColor(BorderSide.BOTTOM); @@ -549,7 +549,7 @@ public class XSSFCellStyle implements CellStyle { public XSSFColor getLeftBorderXSSFColor() { if(!_cellXf.getApplyBorder()) return null; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); XSSFCellBorder border = _stylesSource.getBorderAt(idx); return border.getBorderColor(BorderSide.LEFT); @@ -592,7 +592,7 @@ public class XSSFCellStyle implements CellStyle { public XSSFColor getRightBorderXSSFColor() { if(!_cellXf.getApplyBorder()) return null; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); XSSFCellBorder border = _stylesSource.getBorderAt(idx); return border.getBorderColor(BorderSide.RIGHT); @@ -644,7 +644,7 @@ public class XSSFCellStyle implements CellStyle { public XSSFColor getTopBorderXSSFColor() { if(!_cellXf.getApplyBorder()) return null; - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); XSSFCellBorder border = _stylesSource.getBorderAt(idx); return border.getBorderColor(BorderSide.TOP); @@ -960,7 +960,7 @@ public class XSSFCellStyle implements CellStyle { private CTBorder getCTBorder(){ CTBorder ct; if(_cellXf.getApplyBorder()) { - int idx = (int)_cellXf.getBorderId(); + int idx = Math.toIntExact(_cellXf.getBorderId()); XSSFCellBorder cf = _stylesSource.getBorderAt(idx); ct = (CTBorder)cf.getCTBorder().copy(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java index 80ff17b7e4..d13392e608 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java @@ -256,7 +256,7 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor { } public int getDx1() { - return (int) getCell1().getColOff(); + return Math.toIntExact(getCell1().getColOff()); } /** @@ -268,7 +268,7 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor { } public int getDy1() { - return (int) getCell1().getRowOff(); + return Math.toIntExact(getCell1().getRowOff()); } /** @@ -280,7 +280,7 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor { } public int getDy2() { - return (int) getCell2().getRowOff(); + return Math.toIntExact(getCell2().getRowOff()); } /** @@ -292,7 +292,7 @@ public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor { } public int getDx2() { - return (int) getCell2().getColOff(); + return Math.toIntExact(getCell2().getColOff()); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index cc1ae4ab8b..1ceb552a57 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -407,7 +407,7 @@ public class XSSFRow implements Row, Comparable { */ @Override public int getRowNum() { - return (int) (_row.getR() - 1); + return Math.toIntExact(_row.getR() - 1); } /** @@ -469,7 +469,7 @@ public class XSSFRow implements Row, Comparable { StylesTable stylesSource = getSheet().getWorkbook().getStylesSource(); if(stylesSource.getNumCellStyles() > 0) { - return stylesSource.getStyleAt((int)_row.getS()); + return stylesSource.getStyleAt(Math.toIntExact(_row.getS())); } else { return null; } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index f23e31c477..cc29353371 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -559,7 +559,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { if (width > maxColumnWidth) { width = maxColumnWidth; } - setColumnWidth(column, (int)(width)); + setColumnWidth(column, Math.toIntExact(Math.round(width))); columnHelper.setColBestFit(column, true); } } @@ -900,7 +900,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { CTBreak[] brkArray = ctPageBreak.getBrkArray(); int[] breaks = new int[brkArray.length]; for (int i = 0 ; i < brkArray.length ; i++) { - breaks[i] = (int) brkArray[i].getId() - 1; + breaks[i] = Math.toIntExact(brkArray[i].getId() - 1); } return breaks; } @@ -944,7 +944,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { public int getColumnWidth(int columnIndex) { CTCol col = columnHelper.getColumn(columnIndex, false); double width = col == null || !col.isSetWidth() ? getDefaultColumnWidth() : col.getWidth(); - return (int)(width*256); + return Math.toIntExact(Math.round(width*256)); } /** @@ -973,7 +973,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { @Override public int getDefaultColumnWidth() { CTSheetFormatPr pr = worksheet.getSheetFormatPr(); - return pr == null ? 8 : (int)pr.getBaseColWidth(); + return pr == null ? 8 : Math.toIntExact(pr.getBaseColWidth()); } /** @@ -1686,7 +1686,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { //col must exist short outlineLevel=col.getOutlineLevel(); col.setOutlineLevel((short)(outlineLevel+1)); - index=(int)col.getMax(); + index = Math.toIntExact(col.getMax()); } worksheet.setColsArray(0,ctCols); setSheetFormatPrOutlineLevelCol(); @@ -2256,7 +2256,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // split to 3 records CTCol ciMid = columnHelper.cloneCol(cols, ci); CTCol ciEnd = columnHelper.cloneCol(cols, ci); - int lastcolumn = (int) ciMax; + int lastcolumn = Math.toIntExact(ciMax); ci.setMax(targetColumnIx - 1); @@ -2308,7 +2308,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } idx++; } - return (int) columnInfo.getMax(); + return Math.toIntExact(columnInfo.getMax()); } private boolean isAdjacentBefore(CTCol col, CTCol otherCol) { @@ -2363,7 +2363,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { CTCol col = columnHelper.getColumn(columnIndex, false); int colInfoIx = columnHelper.getIndexOfColumn(cols, col); - int idx = findColInfoIdx((int) col.getMax(), colInfoIx); + int idx = findColInfoIdx(Math.toIntExact(col.getMax()), colInfoIx); if (idx == -1) { return; } @@ -2408,7 +2408,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } // Write collapse flag (stored in a single col info record after this // outline group) - setColumn((int) columnInfo.getMax() + 1, null, null, + setColumn(Math.toIntExact(columnInfo.getMax() + 1), null, null, Boolean.FALSE, Boolean.FALSE); } @@ -3286,7 +3286,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { if (col != null) { short outlineLevel = col.getOutlineLevel(); col.setOutlineLevel((short) (outlineLevel - 1)); - index = (int) col.getMax(); + index = Math.toIntExact(col.getMax()); if (col.getOutlineLevel() <= 0) { int colIndex = columnHelper.getIndexOfColumn(cols, col); @@ -3492,7 +3492,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // the sheet has (i.e. sheet 1 -> comments 1) try { sheetComments = (CommentsTable)createRelationship( - XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId()); + XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), Math.toIntExact(sheet.getSheetId())); } catch(PartAlreadyExistsException e) { // Technically a sheet doesn't need the same number as // it's comments, and clearly someone has already pinched @@ -3563,7 +3563,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { sf.setRef(effectiveRef); } - sharedFormulas.put((int)f.getSi(), sf); + sharedFormulas.put(Math.toIntExact(f.getSi()), sf); } if (f != null && f.getT() == STCellFormulaType.ARRAY && f.getRef() != null) { arrayFormulas.add(CellRangeAddress.valueOf(f.getRef())); @@ -4649,7 +4649,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { nextCell.getColumnIndex(), ref.getLastColumn()); nextF.setRef(nextRef.formatAsString()); - sharedFormulas.put((int)nextF.getSi(), nextF); + sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF); break DONE; } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java index 1656a6f878..81db9bc52c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java @@ -109,7 +109,7 @@ import java.util.List; if (shiftedFormula != null) { f.setStringValue(shiftedFormula); if(f.getT() == STCellFormulaType.SHARED){ - int si = (int)f.getSi(); + int si = Math.toIntExact(f.getSi()); CTCellFormula sf = sheet.getSharedFormula(si); sf.setStringValue(shiftedFormula); updateRefInCTCellFormula(row, formulaShifter, sf); diff --git a/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java index 54673cce39..377f78d82a 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java @@ -20,9 +20,10 @@ package org.apache.poi.ss.util; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; + +import java.util.Iterator; import java.util.Set; import java.util.HashSet; -import org.apache.commons.collections4.IteratorUtils; /** * Tests CellRangeUtil. @@ -76,7 +77,9 @@ public final class TestCellRangeUtil { private static Set getCellAddresses(CellRangeAddress[] ranges) { final Set set = new HashSet<>(); for (final CellRangeAddress range : ranges) { - set.addAll(IteratorUtils.toList(range.iterator())); + for (Iterator iter = range.iterator(); iter.hasNext(); ) { + set.add(iter.next()); + } } return set; }