]> source.dussan.org Git - poi.git/commitdiff
try to avoid casting to int
authorPJ Fanning <fanningpj@apache.org>
Sat, 14 Sep 2019 09:54:11 +0000 (09:54 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 14 Sep 2019 09:54:11 +0000 (09:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866933 13f79535-47bb-0310-9956-ffa450edef68

29 files changed:
src/examples/src/org/apache/poi/hssf/view/SVSheetTable.java
src/examples/src/org/apache/poi/ss/examples/ToCSV.java
src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
src/java/org/apache/poi/hpsf/Section.java
src/java/org/apache/poi/ss/formula/atp/ParityFunction.java
src/java/org/apache/poi/ss/util/CellReference.java
src/java/org/apache/poi/ss/util/ExpandedDouble.java
src/java/org/apache/poi/ss/util/IEEEDouble.java
src/java/org/apache/poi/util/HexDump.java
src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java
src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
src/ooxml/java/org/apache/poi/xddf/usermodel/Angles.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePercent.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFBulletSizePoints.java
src/ooxml/java/org/apache/poi/xddf/usermodel/text/XDDFRunProperties.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java
src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java
src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
src/ooxml/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java
src/ooxml/java/org/apache/poi/xssf/streaming/OpcOutputStream.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java
src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java

index d0889cc11fd544e605b52c6904353019f27cf49f..ebfb3922ea21f2718ed1977fee9451db7a785a95 100644 (file)
@@ -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);
       }
index 23da1e1f0265af1b06d74770e80d45d0da049d72..8b9ca2822b23e175833371a36002c0f8c39f8b2e 100644 (file)
@@ -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");
         }
     }
 
index 622a38b80f8a36e73a786400cb734dcbd2435d1d..74221715a9feb982c991a84c2e886241383d4b4e 100644 (file)
@@ -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<Integer, Integer> widths) {
index 0c1bcf263d93406ae5886d14722c92c582ef954b..5c78402701bff238440f68672617b8d0b8d5d996 100644 (file)
@@ -182,10 +182,10 @@ public class Section {
         final TreeBidiMap<Long,Long> 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);
     }
 
 
index 29012fe18cecd0ce9e12e7ee5925d746030e3f4f..b59b4808b5a1eb1ab4213b1166a5ca8cdbcfcf01 100644 (file)
@@ -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);
        }
 }
index 6e6dbe678d676702918d6f899137679bd47ded45..9f88bcaecb7706654909e368780b01633abf72b5 100644 (file)
@@ -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);
     }
 
     /**
index 24ef95cacc8ab98aa00562012c146a328445490f..40d4066af86ccba13afad7b22139671f60b4d2ed 100644 (file)
@@ -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);
index f5a42edca6314003f9d60be7572e3f776865237b..91d8fea43449387eba19a96515e91ab39b6a323f 100644 (file)
@@ -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);
        }
 }
index d3edc2843f8454d5c6c54c822f0aab1b872803b4..5afd1dbe181c5eb34dacab6d2a00428f5cbba28d 100644 (file)
@@ -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;
         }
index 363ba9d5a7609d3ab67fd4045154739aa976447e..0dafb1344380cadcfa880497877343bdc08a617e 100644 (file)
@@ -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<ZipArchiveEntry> zipArchiveEntryEnumeration = zipArchive.getEntries();
+      while (zipArchiveEntryEnumeration.hasMoreElements()) {
+         ZipArchiveEntry ze = zipArchiveEntryEnumeration.nextElement();
          if (normalizedPath.equalsIgnoreCase(ze.getName().replace('\\','/'))) {
             return ze;
          }
index ef8d20eb7651697fb591f0cacd34b835efa953c3..57103d290665d0f5275a34908473e8db6866bce1 100644 (file)
@@ -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<? extends ZipArchiveEntry> getEntries() {
-               return IteratorUtils.asEnumeration(zipEntries.values().iterator());
+               return Collections.enumeration(zipEntries.values());
        }
 
        @Override
index f31e6ae4647adfbe492de74561347ebd1572b72c..9f08483b60271a3217a9b1819743a3243b8cc6d2 100644 (file)
@@ -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) {
index 56e212579150c4334b58c4d025b0419c2831aa1d..6426c8e32b10d55d72b612a3138dceb864a32d58 100644 (file)
@@ -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)));
     }
 }
index 342ab72e0f83a26957c86d561cfe331b4cda9bcd..0845087b7ca592486d186281a6227e880854049c 100644 (file)
@@ -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)));
     }
 }
index 8025d7cb3205e789c8f2950154a8edd8b5d9bcd1..6a18cf6db225835d14d16c81807652e904e94ae1 100644 (file)
@@ -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));
         }
     }
 
index ff12d6aad2d70838074d63ea527a5030a561e036..2045d599691be115961a9384d93aa6b8fa41ec45 100644 (file)
@@ -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);
         }
index d6c230d436f7b61a834f4693af0283b2fdb696fc..3711fdfc1a8a4fc6e3466e0a4ccef44071e21f7e 100644 (file)
@@ -577,10 +577,10 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
         // 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<XSLFShape,XSLFTextParagraph> {
         }
         XmlCursor cur = styleLst.newCursor();
         XSLFFillProperties fp = null;
-        if (cur.toChild(childIdx)) {
+        if (cur.toChild(Math.toIntExact(childIdx))) {
             fp = XSLFPropertiesDelegate.getFillDelegate(cur.getObject());
         }
         cur.dispose();
index 64ff9a97668eb880a20c10c36793139c481391e5..4d98ecb2eb23728907e3e0f19721ca551a24d548 100644 (file)
@@ -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;
index 43c7f398e15cd1ea21cfaf78965e6dc5ee96ca0b..c03c5579bd09c4603a0b9e6299bcf3657f479fb3 100644 (file)
@@ -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) {
index 61a98e3ca9bffbd4e85a83d5f896552df791d668..fcc592333bdd289335ff0e701037789a9a18399d 100644 (file)
@@ -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
index e8975ecb3772acf4fbbfd04aba86ffebbe8c4578..583ef3c6a21e0e0cdf2b8e597a3ea1aed603b1c4 100644 (file)
@@ -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));
     }
     
 
index 5cbf536fb073c421dd515122eb4bb60ef7c985df..b8253c054d2a0190cd03d418b698365f471f3aa9 100644 (file)
@@ -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;
index 93e265d2506ebf5ed2494721de33f5dd8187f542..9976527511cfae96e8d561e4326e5ae1acc37265 100644 (file)
@@ -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());
index f92b6ed6684fd0288f1a77a958326dee5574ea13..ba366b5317ffd0313470e117fa5178f1ac83bfdb 100644 (file)
@@ -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();
index 80ff17b7e466490e6426bfdb8353d46c775b6f21..d13392e60892e7d06eec19b84516b2f4309250c8 100644 (file)
@@ -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());
     }
 
     /**
index cc1ae4ab8bbd0b75f8e3f7471a016e73db219be7..1ceb552a576adc0690de7f7c7bf783bdf071a884 100644 (file)
@@ -407,7 +407,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
      */
     @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<XSSFRow> {
 
        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;
        }
index f23e31c477305992c35c53bd406f861d1f683baa..cc2935337140d7661000f30c808e0ab85cb9abbd 100644 (file)
@@ -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;
                         }
                     }
index 1656a6f87899122bea3cde55aefbc538aa316169..81db9bc52c487166b4396c23626e96fd7f99b27e 100644 (file)
@@ -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);
index 54673cce3954d3f0d7a60f01e7036eb220d55def..377f78d82aa3234ec23a640d862c525de11d2527 100644 (file)
@@ -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<CellAddress> getCellAddresses(CellRangeAddress[] ranges) {
         final Set<CellAddress> set = new HashSet<>();
         for (final CellRangeAddress range : ranges) {
-            set.addAll(IteratorUtils.toList(range.iterator()));
+            for (Iterator<CellAddress> iter = range.iterator(); iter.hasNext(); ) {
+                set.add(iter.next());
+            }
         }
         return set;
     }