update some code based on sonar issues

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896504 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-12-29 13:08:56 +00:00
parent 79a1e6b1e9
commit 6784dd81c2
6 changed files with 32 additions and 9 deletions

View 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");
}
}
}
}

View 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);

View 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();
}

View 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;
}
};
}

View 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));
}

View 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();