]> source.dussan.org Git - poi.git/commitdiff
update some code based on sonar issues
authorPJ Fanning <fanningpj@apache.org>
Wed, 29 Dec 2021 13:08:56 +0000 (13:08 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 29 Dec 2021 13:08:56 +0000 (13:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896504 13f79535-47bb-0310-9956-ffa450edef68

poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java
poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfPicture.java
poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java
poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
poi/src/main/java/org/apache/poi/ss/util/CellUtil.java

index bca850186d590ea2a80da625167b850372edf42f..06bc962d5d33c2e4b3b6cc04d0a6739b3af11703 100644 (file)
@@ -34,6 +34,8 @@ import java.util.Random;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipFile;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.openxml4j.opc.internal.ZipHelper;
 import org.apache.poi.ss.usermodel.DateUtil;
 import org.apache.poi.ss.usermodel.FillPatternType;
@@ -73,6 +75,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  * See <a href="https://poi.apache.org/spreadsheet/how-to.html#sxssf">SXSSF (Streaming Usermodel API)</a>.
  */
 public final class BigGridDemo {
+    private static final Logger LOG = LogManager.getLogger(BigGridDemo.class);
     private static final String XML_ENCODING = "UTF-8";
 
     private static final Random rnd = new Random();
@@ -111,7 +114,11 @@ public final class BigGridDemo {
                 substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out);
             }
         } finally {
-            if (tmp != null) tmp.delete();
+            if (tmp != null && tmp.exists()) {
+                if (!tmp.delete()) {
+                    LOG.atInfo().log("failed to delete temp file");
+                }
+            }
         }
     }
 
index cd5f61fdafbf826ec9ffd1a5675f9e139ecb336d..899b053ddc7a7cdf3d81a4e0dd033c9e96623e41 100644 (file)
@@ -35,6 +35,7 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Spliterator;
 import java.util.function.Consumer;
 import java.util.function.Function;
@@ -205,6 +206,12 @@ public class HemfPicture implements Iterable<HemfRecord>, GenericRecord {
         final Rectangle2D b = getBoundsInPoints();
         return new Dimension2DDouble(abs(b.getWidth()), abs(b.getHeight()));
     }
+
+    /**
+     * @param ctx
+     * @param graphicsBounds
+     * @throws IllegalStateException if the draw fails
+     */
     public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) {
         final Shape clip = ctx.getClip();
         final AffineTransform at = ctx.getTransform();
@@ -232,8 +239,13 @@ public class HemfPicture implements Iterable<HemfRecord>, GenericRecord {
                         ? winBounds
                         : emfBounds;
             } else {
-                b = Stream.of(emfBounds, winBounds, viewBounds).
-                    min(comparingDouble(r -> diff(r, recBounds))).get();
+                Optional<Rectangle2D> result = Stream.of(emfBounds, winBounds, viewBounds).
+                    min(comparingDouble(r -> diff(r, recBounds)));
+                if (result.isPresent()) {
+                    b = result.get();
+                } else {
+                    throw new IllegalStateException("Failed to create Rectangle2D for drawing");
+                }
             }
 
             ctx.translate(graphicsBounds.getCenterX(), graphicsBounds.getCenterY());
@@ -245,7 +257,7 @@ public class HemfPicture implements Iterable<HemfRecord>, GenericRecord {
 
             HemfGraphics g = new HemfGraphics(ctx, b);
 
-            int idx=0;
+            int idx = 0;
             for (HemfRecord r : getRecords()) {
                 try {
                     g.draw(r);
index d627b1287f19677509502ba77e5e12bc141288a1..f29a393f5f33896bec590d93df70e8a607a1793b 100644 (file)
@@ -94,7 +94,7 @@ final class XMatchFunction implements FreeRefFunction {
                 vector = LookupUtils.createRowVector(tableArray, 0);
             }
             int matchedIdx = LookupUtils.xlookupIndexOfValue(lookupValue, vector, matchMode, searchMode);
-            return new NumberEval(matchedIdx + 1);
+            return new NumberEval((double)matchedIdx + 1);
         } catch (EvaluationException e) {
             return e.getErrorEval();
         }
index a393e13b6501b254b92d2aa7541eedf026715bfb..a74901ab2081d06440c2f746dc23736a23203f7c 100644 (file)
@@ -21,6 +21,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.regex.Matcher;
@@ -142,7 +143,9 @@ public final class LookupUtils {
 
                 @Override
                 public Integer next() {
-                    return pos--;
+                    pos--;
+                    if (pos < 0) throw new NoSuchElementException();
+                    return pos;
                 }
             };
         }
index 1ad1c052fd39c1376ab277935f61c59b3559c16d..1ba040e4da86156bf33910b285b1a78a4ba72894 100644 (file)
@@ -177,7 +177,7 @@ public abstract class NumericFunction implements Function {
         }
         double dpm = Math.abs(d)+1;
         long x = ((long) dpm) & PARITY_MASK;
-        return MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x-1 : x+1);
+        return (double) MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x-1 : x+1);
     }
 
 
@@ -190,7 +190,7 @@ public abstract class NumericFunction implements Function {
 
         double dpm = Math.abs(d);
         long x = ((long) dpm) & PARITY_MASK;
-        return MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x : (x + 2));
+        return (double) MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x : (x + 2));
     }
 
 
index ed468e1b84fa0f70b481897181b3d2fb0b40265e..76d272c49533209296862487d51f8e38d7b733ca 100644 (file)
@@ -243,7 +243,8 @@ public final class CellUtil {
 
         // Copy CellStyle
         if (policy.isCopyCellStyle()) {
-            if (destCell.getSheet().getWorkbook() == srcCell.getSheet().getWorkbook()) {
+            if (srcCell.getSheet() != null && destCell.getSheet() != null &&
+                    destCell.getSheet().getWorkbook() == srcCell.getSheet().getWorkbook()) {
                 destCell.setCellStyle(srcCell.getCellStyle());
             } else {
                 CellStyle srcStyle = srcCell.getCellStyle();