This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 4e40bc512b more instanceof (#1134)
4e40bc512b is described below
commit 4e40bc512bfbe98e74e69d883e807aed3e1666f4
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Jun 11 21:55:51 2026 +0100
more instanceof (#1134)
* more instanceof
* Update WorkbookEvaluator.java
---
.../org/apache/poi/extractor/ExtractorFactory.java | 4 +-
.../apache/poi/ss/extractor/EmbeddedExtractor.java | 14 +++--
.../apache/poi/ss/format/CellDateFormatter.java | 3 +-
.../java/org/apache/poi/ss/format/CellFormat.java | 14 +++--
.../org/apache/poi/ss/format/CellFormatPart.java | 6 +--
.../apache/poi/ss/format/CellGeneralFormatter.java | 4 +-
.../apache/poi/ss/format/CellNumberFormatter.java | 3 +-
.../apache/poi/ss/format/CellNumberStringMod.java | 2 +-
.../formula/CollaboratingWorkbooksEnvironment.java | 4 +-
.../formula/EvaluationConditionalFormatRule.java | 21 ++++----
.../poi/ss/formula/OperationEvaluatorFactory.java | 6 +--
.../org/apache/poi/ss/formula/PlainCellCache.java | 6 +--
.../apache/poi/ss/formula/UserDefinedFunction.java | 7 ++-
.../apache/poi/ss/formula/WorkbookEvaluator.java | 18 +++----
.../org/apache/poi/ss/usermodel/DataFormatter.java | 17 +++---
.../poi/ss/usermodel/ExcelGeneralNumberFormat.java | 4 +-
.../ss/usermodel/helpers/BaseRowColShifter.java | 3 +-
.../main/java/org/apache/poi/ss/util/CellUtil.java | 60 ++++++++++------------
.../apache/poi/ss/util/DateFormatConverter.java | 10 ++--
.../org/apache/poi/ss/util/PropertyTemplate.java | 8 +--
.../org/apache/poi/util/Dimension2DDouble.java | 3 +-
.../java/org/apache/poi/util/ExceptionUtil.java | 8 +--
.../apache/poi/util/GenericRecordJsonWriter.java | 60 +++++++++++-----------
poi/src/main/java/org/apache/poi/util/IntList.java | 24 ++++-----
24 files changed, 142 insertions(+), 167 deletions(-)
diff --git a/poi/src/main/java/org/apache/poi/extractor/ExtractorFactory.java
b/poi/src/main/java/org/apache/poi/extractor/ExtractorFactory.java
index 458c4ed65c..702fa43d27 100644
--- a/poi/src/main/java/org/apache/poi/extractor/ExtractorFactory.java
+++ b/poi/src/main/java/org/apache/poi/extractor/ExtractorFactory.java
@@ -388,8 +388,8 @@ public final class ExtractorFactory {
ArrayList<POITextExtractor> textExtractors = new ArrayList<>();
for (Entry dir : dirs) {
- if (dir instanceof DirectoryNode) {
- textExtractors.add(createExtractor((DirectoryNode) dir));
+ if (dir instanceof DirectoryNode dn) {
+ textExtractors.add(createExtractor(dn));
}
}
for (InputStream stream : nonPOIFS) {
diff --git
a/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
b/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
index 1b9cc2934d..faa3db2808 100644
--- a/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
+++ b/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
@@ -123,8 +123,7 @@ public class EmbeddedExtractor implements
Iterable<EmbeddedExtractor> {
protected void extractAll(ShapeContainer<?> parent, List<EmbeddedData>
embeddings) throws IOException {
for (Shape shape : parent) {
EmbeddedData data = null;
- if (shape instanceof ObjectData) {
- ObjectData od = (ObjectData)shape;
+ if (shape instanceof ObjectData od) {
try {
if (od.hasDirectoryEntry()) {
data = extractOne((DirectoryNode)od.getDirectory());
@@ -134,10 +133,10 @@ public class EmbeddedExtractor implements
Iterable<EmbeddedExtractor> {
} catch (Exception e) {
LOG.atWarn().withThrowable(e).log("Entry not found /
readable - ignoring OLE embedding");
}
- } else if (shape instanceof Picture) {
- data = extractOne((Picture)shape);
- } else if (shape instanceof ShapeContainer) {
- extractAll((ShapeContainer<?>)shape, embeddings);
+ } else if (shape instanceof Picture pict) {
+ data = extractOne(pict);
+ } else if (shape instanceof ShapeContainer<?> sc) {
+ extractAll(sc, embeddings);
}
if (data == null) {
@@ -354,8 +353,7 @@ public class EmbeddedExtractor implements
Iterable<EmbeddedExtractor> {
protected static void copyNodes(DirectoryNode src, DirectoryNode dest)
throws IOException {
for (Entry e : src) {
- if (e instanceof DirectoryNode) {
- DirectoryNode srcDir = (DirectoryNode)e;
+ if (e instanceof DirectoryNode srcDir) {
DirectoryNode destDir =
(DirectoryNode)dest.createDirectory(srcDir.getName());
destDir.setStorageClsid(srcDir.getStorageClsid());
copyNodes(srcDir, destDir);
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java
b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java
index 8deb4f2422..f74a4cf706 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java
@@ -178,8 +178,7 @@ public class CellDateFormatter extends CellFormatter {
public synchronized void formatValue(StringBuffer toAppendTo, Object
value) {
if (value == null)
value = 0.0;
- if (value instanceof Number) {
- Number num = (Number) value;
+ if (value instanceof Number num) {
// Convert from fractional days to milliseconds. Excel always
rounds up.
double v = Math.round(num.doubleValue() * NUM_MILLISECONDS_IN_DAY);
if (v == 0L) {
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
b/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
index 0c9a73c87a..5d404e3dbd 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
@@ -246,8 +246,7 @@ public class CellFormat {
* @return The result, in a {@link CellFormatResult}.
*/
public CellFormatResult apply(Object value) {
- if (value instanceof Number) {
- Number num = (Number) value;
+ if (value instanceof Number num) {
double val = num.doubleValue();
if (val < 0 &&
((formatPartCount == 2
@@ -261,10 +260,10 @@ public class CellFormat {
} else {
return getApplicableFormatPart(val).apply(val);
}
- } else if (value instanceof java.util.Date) {
+ } else if (value instanceof java.util.Date d) {
// Don't know (and can't get) the workbook date windowing (1900 or
1904)
// so assume 1900 date windowing
- double numericValue = DateUtil.getExcelDate((Date) value);
+ double numericValue = DateUtil.getExcelDate(d);
if (DateUtil.isValidExcelDate(numericValue)) {
return getApplicableFormatPart(numericValue).apply(value);
} else {
@@ -400,9 +399,9 @@ public class CellFormat {
*/
private CellFormatPart getApplicableFormatPart(Object value) {
- if (value instanceof Number) {
+ if (value instanceof Number num) {
- double val = ((Number) value).doubleValue();
+ double val = num.doubleValue();
if (formatPartCount == 1) {
if (!posNumFmt.hasCondition()
@@ -470,8 +469,7 @@ public class CellFormat {
public boolean equals(Object obj) {
if (this == obj)
return true;
- if (obj instanceof CellFormat) {
- CellFormat that = (CellFormat) obj;
+ if (obj instanceof CellFormat that) {
return format.equals(that.format);
}
return false;
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java
b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java
index d8f59b70a7..3bf263b821 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java
@@ -601,8 +601,7 @@ public class CellFormatPart {
int pos = 0;
while ((pos = fmt.indexOf("''", pos)) >= 0) {
fmt.delete(pos, pos + 2);
- if (partHandler instanceof CellDateFormatter.DatePartHandler) {
- CellDateFormatter.DatePartHandler datePartHandler =
(CellDateFormatter.DatePartHandler) partHandler;
+ if (partHandler instanceof CellDateFormatter.DatePartHandler
datePartHandler) {
datePartHandler.updatePositions(pos, -2);
}
}
@@ -611,8 +610,7 @@ public class CellFormatPart {
pos = 0;
while ((pos = fmt.indexOf("\u0000", pos)) >= 0) {
fmt.replace(pos, pos + 1, "''");
- if (partHandler instanceof CellDateFormatter.DatePartHandler) {
- CellDateFormatter.DatePartHandler datePartHandler =
(CellDateFormatter.DatePartHandler) partHandler;
+ if (partHandler instanceof CellDateFormatter.DatePartHandler
datePartHandler) {
datePartHandler.updatePositions(pos, 1);
}
}
diff --git
a/poi/src/main/java/org/apache/poi/ss/format/CellGeneralFormatter.java
b/poi/src/main/java/org/apache/poi/ss/format/CellGeneralFormatter.java
index c502d1ba42..c48b3c28f3 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellGeneralFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellGeneralFormatter.java
@@ -43,8 +43,8 @@ public class CellGeneralFormatter extends CellFormatter {
*/
@Override
public void formatValue(StringBuffer toAppendTo, Object value) {
- if (value instanceof Number) {
- double val = ((Number) value).doubleValue();
+ if (value instanceof Number num) {
+ double val = num.doubleValue();
if (val == 0) {
toAppendTo.append('0');
return;
diff --git
a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
index d27d8dea69..065996ddd7 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
@@ -87,8 +87,7 @@ public class CellNumberFormatter extends CellFormatter {
}
CellFormatter cf;
- if (value instanceof Number) {
- Number num = (Number) value;
+ if (value instanceof Number num) {
cf = (num.doubleValue() % 1.0 == 0) ? new
CellNumberFormatter(locale, "#") :
new CellNumberFormatter(locale, "#.#");
} else {
diff --git
a/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
b/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
index 30c10f1258..66b4bf5f2a 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
@@ -71,7 +71,7 @@ public class CellNumberStringMod implements
Comparable<CellNumberStringMod> {
@Override
public boolean equals(Object that) {
- return (that instanceof CellNumberStringMod) &&
compareTo((CellNumberStringMod) that) == 0;
+ return (that instanceof CellNumberStringMod mod) && compareTo(mod) ==
0;
}
@Override
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
b/poi/src/main/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
index 446149b113..1497ad58d8 100644
---
a/poi/src/main/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
+++
b/poi/src/main/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java
@@ -79,8 +79,8 @@ public final class CollaboratingWorkbooksEnvironment {
for (Map.Entry<String,FormulaEvaluator> swb : evaluators.entrySet()) {
String wbName = swb.getKey();
FormulaEvaluator eval = swb.getValue();
- if (eval instanceof WorkbookEvaluatorProvider) {
- evaluatorsByName.put(wbName,
((WorkbookEvaluatorProvider)eval)._getWorkbookEvaluator());
+ if (eval instanceof WorkbookEvaluatorProvider wep) {
+ evaluatorsByName.put(wbName, wep._getWorkbookEvaluator());
} else {
throw new IllegalArgumentException("Formula Evaluator " + eval
+
" provides no
WorkbookEvaluator access");
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java
b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java
index 3d0e441dbc..339856f0c9 100644
---
a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java
+++
b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java
@@ -387,8 +387,7 @@ public class EvaluationConditionalFormatRule implements
Comparable<EvaluationCon
private ValueEval unwrapEval(ValueEval eval) {
ValueEval comp = eval;
- while (comp instanceof RefEval) {
- RefEval ref = (RefEval) comp;
+ while (comp instanceof RefEval ref) {
comp = ref.getInnerValueEval(ref.getFirstSheetIndex());
}
return comp;
@@ -408,13 +407,13 @@ public class EvaluationConditionalFormatRule implements
Comparable<EvaluationCon
if (comp instanceof ErrorEval) {
return false;
}
- if (comp instanceof BoolEval) {
- return ((BoolEval) comp).getBooleanValue();
+ if (comp instanceof BoolEval bool) {
+ return bool.getBooleanValue();
}
// empirically tested in Excel - 0=false, any other number = true/valid
// see test file DataValidationEvaluations.xlsx
- if (comp instanceof NumberEval) {
- return ((NumberEval) comp).getNumberValue() != 0;
+ if (comp instanceof NumberEval num) {
+ return num.getNumberValue() != 0;
}
return false; // anything else is false, such as text
}
@@ -708,13 +707,13 @@ public class EvaluationConditionalFormatRule implements
Comparable<EvaluationCon
@Override
public boolean equals(Object obj) {
- if (!(obj instanceof ValueAndFormat)) {
+ if (obj instanceof ValueAndFormat o) {
+ return (Objects.equals(value, o.value)
+ && Objects.equals(format, o.format)
+ && Objects.equals(string, o.string));
+ } else {
return false;
}
- ValueAndFormat o = (ValueAndFormat) obj;
- return (Objects.equals(value, o.value)
- && Objects.equals(format, o.format)
- && Objects.equals(string, o.string));
}
/**
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
index cf12bbc66b..39ab12562a 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
@@ -104,8 +104,7 @@ final class OperationEvaluatorFactory {
Function result = _instancesByPtgClass.get(ptg.getSid());
FreeRefFunction udfFunc = null;
if (result == null) {
- if (ptg instanceof AbstractFunctionPtg) {
- AbstractFunctionPtg fptg = (AbstractFunctionPtg)ptg;
+ if (ptg instanceof AbstractFunctionPtg fptg) {
int functionIndex = fptg.getFunctionIndex();
switch (functionIndex) {
case FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT:
@@ -122,8 +121,7 @@ final class OperationEvaluatorFactory {
}
if (result != null) {
- if (result instanceof ArrayFunction) {
- ArrayFunction func = (ArrayFunction) result;
+ if (result instanceof ArrayFunction func) {
ValueEval eval = evaluateArrayFunction(func, args, ec);
if (eval != null) {
return eval;
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java
b/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java
index 19e00a7da5..29436d57d0 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/PlainCellCache.java
@@ -51,11 +51,11 @@ final class PlainCellCache {
@Override
public boolean equals(Object obj) {
- if (!(obj instanceof Loc)) {
+ if (obj instanceof Loc other) {
+ return _bookSheetColumn == other._bookSheetColumn && _rowIndex
== other._rowIndex;
+ } else {
return false;
}
- Loc other = (Loc) obj;
- return _bookSheetColumn == other._bookSheetColumn && _rowIndex ==
other._rowIndex;
}
public int getRowIndex() {
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/UserDefinedFunction.java
b/poi/src/main/java/org/apache/poi/ss/formula/UserDefinedFunction.java
index c0c0cc56fb..1ec96a9512 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/UserDefinedFunction.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/UserDefinedFunction.java
@@ -54,8 +54,8 @@ final class UserDefinedFunction implements FreeRefFunction {
ValueEval nameArg = args[0];
String functionName;
- if (nameArg instanceof FunctionNameEval) {
- functionName = ((FunctionNameEval) nameArg).getFunctionName();
+ if (nameArg instanceof FunctionNameEval fne) {
+ functionName = fne.getFunctionName();
} else {
throw new IllegalStateException("First argument should be a
NameEval, but got ("
+ nameArg.getClass().getName() + ")");
@@ -67,8 +67,7 @@ final class UserDefinedFunction implements FreeRefFunction {
int nOutGoingArgs = nIncomingArgs - 1;
ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
- if (targetFunc instanceof ArrayFunction) {
- ArrayFunction func = (ArrayFunction) targetFunc;
+ if (targetFunc instanceof ArrayFunction func) {
ValueEval eval =
OperationEvaluatorFactory.evaluateArrayFunction(func, outGoingArgs, ec);
if (eval != null) {
return eval;
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
b/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
index 4b703fc143..29d45fff9d 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
@@ -404,8 +404,7 @@ public final class WorkbookEvaluator {
if (dbgEvaluationOutputIndent > 0) {
EVAL_LOG.atInfo().log("{} * ptg {}: {}, stack: {}",
dbgIndentStr, box(i), ptg, stack);
}
- if (ptg instanceof AttrPtg) {
- AttrPtg attrPtg = (AttrPtg) ptg;
+ if (ptg instanceof AttrPtg attrPtg) {
if (attrPtg.isSum()) {
// Excel prefers to encode 'SUM()' as a tAttr token, but
this evaluator
// expects the equivalent function token
@@ -456,10 +455,10 @@ public final class WorkbookEvaluator {
int dist = attrPtg.getData();
i += countTokensToBeSkipped(ptgs, i, dist);
Ptg nextPtg = ptgs[i + 1];
- if (ptgs[i] instanceof AttrPtg && nextPtg
instanceof FuncVarPtg &&
+ if (ptgs[i] instanceof AttrPtg && nextPtg
instanceof FuncVarPtg fvp &&
// in order to verify that there is no
third param, we need to check
// if we really have the IF next or some
other FuncVarPtg as third param, e.g. ROW()/COLUMN()!
- ((FuncVarPtg) nextPtg).getFunctionIndex()
== FunctionMetadataRegistry.FUNCTION_INDEX_IF) {
+ fvp.getFunctionIndex() ==
FunctionMetadataRegistry.FUNCTION_INDEX_IF) {
// this is an if statement without a false
param (as opposed to MissingArgPtg as the false param)
//i++;
stack.push(arg0);
@@ -499,9 +498,7 @@ public final class WorkbookEvaluator {
}
ValueEval opResult;
- if (ptg instanceof OperationPtg) {
- OperationPtg optg = (OperationPtg) ptg;
-
+ if (ptg instanceof OperationPtg optg) {
int numops = optg.getNumberOfOperands();
ValueEval[] ops = new ValueEval[numops];
@@ -517,8 +514,7 @@ public final class WorkbookEvaluator {
boolean arrayMode = false;
if (areaArg) for (int ii = i; ii < iSize; ii++) {
- if (ptgs[ii] instanceof FuncVarPtg) {
- FuncVarPtg f = (FuncVarPtg) ptgs[ii];
+ if (ptgs[ii] instanceof FuncVarPtg f) {
try {
Function func =
FunctionEval.getBasicFunction(f.getFunctionIndex());
if (func instanceof ArrayMode) {
@@ -620,8 +616,8 @@ public final class WorkbookEvaluator {
EvaluationSheet evalSheet =
ec.getWorkbook().getSheet(ec.getSheetIndex());
EvaluationCell evalCell = evalSheet.getCell(ec.getRowIndex(),
ec.getColumnIndex());
- if (evalCell != null && evalCell.isPartOfArrayFormulaGroup() &&
evaluationResult instanceof AreaEval) {
- value = OperandResolver.getElementFromArray((AreaEval)
evaluationResult, evalCell);
+ if (evalCell != null && evalCell.isPartOfArrayFormulaGroup() &&
evaluationResult instanceof AreaEval ae) {
+ value = OperandResolver.getElementFromArray(ae, evalCell);
} else {
value = dereferenceResult(evaluationResult, ec.getRowIndex(),
ec.getColumnIndex());
}
diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
index 0acc9d86dd..21c99484c5 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
@@ -814,10 +814,10 @@ public class DataFormatter {
private Object scaleInput(Object obj) {
if (divider != null) {
- if (obj instanceof BigDecimal) {
- obj = ((BigDecimal) obj).divide(divider,
RoundingMode.HALF_UP);
- } else if (obj instanceof Double) {
- obj = (Double) obj / divider.doubleValue();
+ if (obj instanceof BigDecimal bd) {
+ obj = bd.divide(divider, RoundingMode.HALF_UP);
+ } else if (obj instanceof Double d) {
+ obj = d / divider.doubleValue();
} else {
throw new UnsupportedOperationException("cannot scaleInput
of type " + obj.getClass());
}
@@ -936,11 +936,9 @@ public class DataFormatter {
}
}
synchronized (dateFormat) {
- if(dateFormat instanceof ExcelStyleDateFormatter) {
+ if(dateFormat instanceof ExcelStyleDateFormatter formatter) {
// Hint about the raw excel value
- ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(
- cell.getNumericCellValue()
- );
+ formatter.setDateToBeFormatted(cell.getNumericCellValue());
}
Date d = cell.getDateCellValue();
return performDateFormatting(d, dateFormat);
@@ -970,8 +968,7 @@ public class DataFormatter {
return Double.toString(d);
}
String formatted = null;
- if (numberFormat instanceof InternalDecimalFormatWithScale) {
- InternalDecimalFormatWithScale idfws =
(InternalDecimalFormatWithScale) numberFormat;
+ if (numberFormat instanceof InternalDecimalFormatWithScale idfws) {
if (idfws.requiresScaling()) {
// hack for
https://bz.apache.org/bugzilla/show_bug.cgi?id=69812
// the https://github.com/apache/poi/pull/321 hack causes
problems here
diff --git
a/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
index bc380776d1..67c74ce0e3 100644
---
a/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
+++
b/poi/src/main/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
@@ -54,8 +54,8 @@ public class ExcelGeneralNumberFormat extends Format {
@SuppressWarnings("squid:S2111")
public StringBuffer format(Object number, StringBuffer toAppendTo,
FieldPosition pos) {
final double value;
- if (number instanceof Number) {
- value = ((Number)number).doubleValue();
+ if (number instanceof Number n) {
+ value = n.doubleValue();
if (Double.isInfinite(value) || Double.isNaN(value)) {
return integerFormat.format(number, toAppendTo, pos);
}
diff --git
a/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/BaseRowColShifter.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/BaseRowColShifter.java
index 5e2400c556..91a60b0a4f 100644
---
a/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/BaseRowColShifter.java
+++
b/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/BaseRowColShifter.java
@@ -82,8 +82,7 @@ public abstract class BaseRowColShifter {
return cra;
}
Ptg ptg0 = ptgs[0];
- if (ptg0 instanceof AreaPtg) {
- AreaPtg bptg = (AreaPtg) ptg0;
+ if (ptg0 instanceof AreaPtg bptg) {
return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(),
bptg.getFirstColumn(), bptg.getLastColumn());
}
if (ptg0 instanceof AreaErrPtg) {
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 faa1722b73..7173add4bd 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
@@ -465,8 +465,8 @@ public final class CellUtil {
if (policy.isMergeHyperlink()) {
// if srcCell doesn't have a hyperlink and destCell has a
hyperlink, don't clear destCell's hyperlink
if (srcHyperlink != null) {
- if (srcHyperlink instanceof Duplicatable) {
- Hyperlink newHyperlink = (Hyperlink) ((Duplicatable)
srcHyperlink).copy();
+ if (srcHyperlink instanceof Duplicatable duplicatable) {
+ Hyperlink newHyperlink = (Hyperlink) duplicatable.copy();
destCell.setHyperlink(newHyperlink);
} else {
throw new IllegalStateException("srcCell hyperlink is not
an instance of Duplicatable");
@@ -477,8 +477,8 @@ public final class CellUtil {
// if srcCell doesn't have a hyperlink, clear the hyperlink (if
one exists) at destCell
if (srcHyperlink == null) {
destCell.setHyperlink(null);
- } else if (srcHyperlink instanceof Duplicatable) {
- Hyperlink newHyperlink = (Hyperlink) ((Duplicatable)
srcHyperlink).copy();
+ } else if (srcHyperlink instanceof Duplicatable duplicatable) {
+ Hyperlink newHyperlink = (Hyperlink) duplicatable.copy();
destCell.setHyperlink(newHyperlink);
} else {
throw new IllegalStateException("srcCell hyperlink is not an
instance of Duplicatable");
@@ -909,19 +909,19 @@ public final class CellUtil {
*/
private static short getShort(Map<CellPropertyType, Object> properties,
CellPropertyType property) {
Object value = properties.get(property);
- if (value instanceof Number) {
- return ((Number) value).shortValue();
+ if (value instanceof Number n) {
+ return n.shortValue();
}
return 0;
}
private static Short nullableShort(Map<CellPropertyType, Object>
properties, CellPropertyType property) {
Object value = properties.get(property);
- if (value instanceof Short) {
- return (Short) value;
+ if (value instanceof Short s) {
+ return s;
}
- if (value instanceof Number) {
- return ((Number) value).shortValue();
+ if (value instanceof Number n) {
+ return n.shortValue();
}
return null;
}
@@ -936,8 +936,8 @@ public final class CellUtil {
*/
private static Color getColor(Map<CellPropertyType, Object> properties,
CellPropertyType property) {
Object value = properties.get(property);
- if (value instanceof Color) {
- return (Color) value;
+ if (value instanceof Color c) {
+ return c;
}
return null;
}
@@ -953,8 +953,8 @@ public final class CellUtil {
*/
private static int getInt(Map<CellPropertyType, Object> properties,
CellPropertyType property) {
Object value = properties.get(property);
- if (value instanceof Number) {
- return ((Number) value).intValue();
+ if (value instanceof Number n) {
+ return n.intValue();
}
return 0;
}
@@ -969,13 +969,12 @@ public final class CellUtil {
private static BorderStyle getBorderStyle(Map<CellPropertyType, Object>
properties, CellPropertyType property) {
Object value = properties.get(property);
BorderStyle border;
- if (value instanceof BorderStyle) {
- border = (BorderStyle) value;
+ if (value instanceof BorderStyle bs) {
+ border = bs;
}
// @deprecated 3.15 beta 2. getBorderStyle will only work on
BorderStyle enums instead of codes in the future.
- else if (value instanceof Short) {
+ else if (value instanceof Short code) {
LOGGER.atWarn().log("Deprecation warning: CellUtil properties map
uses Short values for {}. Should use BorderStyle enums instead.", property);
- short code = (Short) value;
border = BorderStyle.valueOf(code);
} else if (value == null) {
border = BorderStyle.NONE;
@@ -996,13 +995,12 @@ public final class CellUtil {
private static FillPatternType getFillPattern(Map<CellPropertyType,
Object> properties, CellPropertyType property) {
Object value = properties.get(property);
FillPatternType pattern;
- if (value instanceof FillPatternType) {
- pattern = (FillPatternType) value;
+ if (value instanceof FillPatternType fpt) {
+ pattern = fpt;
}
// @deprecated 3.15 beta 2. getFillPattern will only work on
FillPatternType enums instead of codes in the future.
- else if (value instanceof Short) {
+ else if (value instanceof Short code) {
LOGGER.atWarn().log("Deprecation warning: CellUtil properties map
uses Short values for {}. Should use FillPatternType enums instead.", property);
- short code = (Short) value;
pattern = FillPatternType.forInt(code);
} else if (value == null) {
pattern = FillPatternType.NO_FILL;
@@ -1023,13 +1021,12 @@ public final class CellUtil {
private static HorizontalAlignment
getHorizontalAlignment(Map<CellPropertyType, Object> properties,
CellPropertyType property) {
Object value = properties.get(property);
HorizontalAlignment align;
- if (value instanceof HorizontalAlignment) {
- align = (HorizontalAlignment) value;
+ if (value instanceof HorizontalAlignment ha) {
+ align = ha;
}
// @deprecated 3.15 beta 2. getHorizontalAlignment will only work on
HorizontalAlignment enums instead of codes in the future.
- else if (value instanceof Short) {
+ else if (value instanceof Short code) {
LOGGER.atWarn().log("Deprecation warning: CellUtil properties map
used a Short value for {}. Should use HorizontalAlignment enums instead.",
property);
- short code = (Short) value;
align = HorizontalAlignment.forInt(code);
} else if (value == null) {
align = HorizontalAlignment.GENERAL;
@@ -1050,13 +1047,12 @@ public final class CellUtil {
private static VerticalAlignment
getVerticalAlignment(Map<CellPropertyType, Object> properties, CellPropertyType
property) {
Object value = properties.get(property);
VerticalAlignment align;
- if (value instanceof VerticalAlignment) {
- align = (VerticalAlignment) value;
+ if (value instanceof VerticalAlignment va) {
+ align = va;
}
// @deprecated 3.15 beta 2. getVerticalAlignment will only work on
VerticalAlignment enums instead of codes in the future.
- else if (value instanceof Short) {
+ else if (value instanceof Short code) {
LOGGER.atWarn().log("Deprecation warning: CellUtil properties map
used a Short value for {}. Should use VerticalAlignment enums instead.",
property);
- short code = (Short) value;
align = VerticalAlignment.forInt(code);
} else if (value == null) {
align = VerticalAlignment.BOTTOM;
@@ -1077,8 +1073,8 @@ public final class CellUtil {
private static boolean getBoolean(Map<CellPropertyType, Object>
properties, CellPropertyType property) {
Object value = properties.get(property);
//noinspection SimplifiableIfStatement
- if (value instanceof Boolean) {
- return (Boolean) value;
+ if (value instanceof Boolean bool) {
+ return bool;
}
return false;
}
diff --git a/poi/src/main/java/org/apache/poi/ss/util/DateFormatConverter.java
b/poi/src/main/java/org/apache/poi/ss/util/DateFormatConverter.java
index de2a3e48d2..6bd8500347 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/DateFormatConverter.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/DateFormatConverter.java
@@ -192,7 +192,7 @@ public final class DateFormatConverter {
public static String getJavaDatePattern(int style, Locale locale) {
DateFormat df = DateFormat.getDateInstance(style, locale);
- if( df instanceof SimpleDateFormat sdf) {
+ if(df instanceof SimpleDateFormat sdf) {
return sdf.toPattern();
} else {
return switch (style) {
@@ -206,8 +206,8 @@ public final class DateFormatConverter {
public static String getJavaTimePattern(int style, Locale locale) {
DateFormat df = DateFormat.getTimeInstance(style, locale);
- if( df instanceof SimpleDateFormat ) {
- return ((SimpleDateFormat)df).toPattern();
+ if(df instanceof SimpleDateFormat sdf) {
+ return sdf.toPattern();
} else {
return switch (style) {
case DateFormat.SHORT -> "h:mm a";
@@ -218,8 +218,8 @@ public final class DateFormatConverter {
public static String getJavaDateTimePattern(int style, Locale locale) {
DateFormat df = DateFormat.getDateTimeInstance(style, style, locale);
- if( df instanceof SimpleDateFormat ) {
- return ((SimpleDateFormat)df).toPattern();
+ if(df instanceof SimpleDateFormat sdf) {
+ return sdf.toPattern();
} else {
return switch (style) {
case DateFormat.SHORT -> "M/d/yy h:mm a";
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 e00b785070..818aacf3d9 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
@@ -876,8 +876,8 @@ public final class PropertyTemplate {
Map<CellPropertyType, Object> cellProperties =
_propertyTemplate.get(cell);
if (cellProperties != null) {
Object obj = cellProperties.get(property);
- if (obj instanceof BorderStyle) {
- value = (BorderStyle) obj;
+ if (obj instanceof BorderStyle bs) {
+ value = bs;
}
}
return value;
@@ -961,8 +961,8 @@ public final class PropertyTemplate {
* @return short value, or 0 if not a short
*/
private static short getShort(Object value) {
- if (value instanceof Number) {
- return ((Number) value).shortValue();
+ if (value instanceof Number n) {
+ return n.shortValue();
}
return 0;
}
diff --git a/poi/src/main/java/org/apache/poi/util/Dimension2DDouble.java
b/poi/src/main/java/org/apache/poi/util/Dimension2DDouble.java
index 227c0cddd5..3b096e8b59 100644
--- a/poi/src/main/java/org/apache/poi/util/Dimension2DDouble.java
+++ b/poi/src/main/java/org/apache/poi/util/Dimension2DDouble.java
@@ -55,8 +55,7 @@ public class Dimension2DDouble extends Dimension2D {
@Override
public boolean equals(Object obj) {
- if (obj instanceof Dimension2DDouble) {
- Dimension2DDouble other = (Dimension2DDouble) obj;
+ if (obj instanceof Dimension2DDouble other) {
return width == other.width && height == other.height;
}
diff --git a/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java
b/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java
index 511efbd505..74d41d2348 100644
--- a/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java
+++ b/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java
@@ -54,11 +54,11 @@ public class ExceptionUtil {
* Otherwise wraps the throwable in a RuntimeException.
*/
public static void rethrow(Throwable throwable) throws Error,
RuntimeException {
- if (throwable instanceof Error) {
- throw (Error) throwable;
+ if (throwable instanceof Error err) {
+ throw err;
}
- if (throwable instanceof RuntimeException) {
- throw (RuntimeException) throwable;
+ if (throwable instanceof RuntimeException rex) {
+ throw rex;
}
throw new RuntimeException(throwable);
}
diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java
b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java
index 4aa0bd56bb..23420f650a 100644
--- a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java
+++ b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java
@@ -267,39 +267,41 @@ public class GenericRecordJsonWriter implements Closeable
{
@SuppressWarnings("java:S3516")
protected boolean printNumber(String name, Object o) {
- Number n = (Number)o;
printName(name);
- if (o instanceof Float) {
- fw.print(n.floatValue());
+ if (o instanceof Float f) {
+ fw.print(f.floatValue());
return true;
- } else if (o instanceof Double) {
- fw.print(n.doubleValue());
+ } else if (o instanceof Double d) {
+ fw.print(d.doubleValue());
return true;
}
- fw.print(n.longValue());
-
- final int size;
- if (n instanceof Byte) {
- size = 2;
- } else if (n instanceof Short) {
- size = 4;
- } else if (n instanceof Integer) {
- size = 8;
- } else if (n instanceof Long) {
- size = 16;
- } else {
- size = -1;
- }
+ if (o instanceof Number n) {
+ fw.print(n.longValue());
+
+ final int size;
+ if (n instanceof Byte) {
+ size = 2;
+ } else if (n instanceof Short) {
+ size = 4;
+ } else if (n instanceof Integer) {
+ size = 8;
+ } else if (n instanceof Long) {
+ size = 16;
+ } else {
+ size = -1;
+ }
- long l = n.longValue();
- if (withComments && size > 0 && (l < 0 || l > 9)) {
- fw.write(" /* 0x");
- fw.write(trimHex(l, size));
- fw.write(" */");
+ long l = n.longValue();
+ if (withComments && size > 0 && (l < 0 || l > 9)) {
+ fw.write(" /* 0x");
+ fw.write(trimHex(l, size));
+ fw.write(" */");
+ }
+ return true;
}
- return true;
+ return false;
}
protected boolean printBoolean(String name, Object o) {
@@ -576,8 +578,8 @@ public class GenericRecordJsonWriter implements Closeable {
@Override
public void flush() throws IOException {
Object o = (appender != null) ? appender : writer;
- if (o instanceof Flushable) {
- ((Flushable)o).flush();
+ if (o instanceof Flushable flushable) {
+ flushable.flush();
}
}
@@ -585,8 +587,8 @@ public class GenericRecordJsonWriter implements Closeable {
public void close() throws IOException {
flush();
Object o = (appender != null) ? appender : writer;
- if (o instanceof Closeable) {
- ((Closeable)o).close();
+ if (o instanceof Closeable cls) {
+ cls.close();
}
}
}
diff --git a/poi/src/main/java/org/apache/poi/util/IntList.java
b/poi/src/main/java/org/apache/poi/util/IntList.java
index 8d958d5934..765605e613 100644
--- a/poi/src/main/java/org/apache/poi/util/IntList.java
+++ b/poi/src/main/java/org/apache/poi/util/IntList.java
@@ -285,23 +285,21 @@ public class IntList
return true;
}
- if (!(o instanceof IntList)) {
- return false;
- }
+ if (o instanceof IntList other) {
+ if (other._limit != _limit) {
+ return false;
+ }
- IntList other = ( IntList ) o;
+ for (int i=0; i< _limit; i++) {
+ if (other._array[i] != _array[i]) {
+ return false;
+ }
+ }
- if (other._limit != _limit) {
+ return true;
+ } else {
return false;
}
-
- for (int i=0; i< _limit; i++) {
- if (other._array[i] != _array[i]) {
- return false;
- }
- }
-
- return true;
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]