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 aa4719db3f use pattern matching (#1214)
aa4719db3f is described below
commit aa4719db3fbe900378be214d31915ed1e46bf53a
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Aug 14 10:57:09 2026 +0100
use pattern matching (#1214)
* use pattern matching
* Update AbstractWordConverter.java
* replace asserts
---
.../org/apache/poi/xssf/streaming/SXSSFCell.java | 6 +-
.../apache/poi/xwpf/usermodel/XWPFTableCell.java | 23 ++++---
.../apache/poi/hslf/usermodel/HSLFHyperlink.java | 5 +-
.../org/apache/poi/hslf/usermodel/HSLFSheet.java | 21 +++---
.../poi/hslf/usermodel/HSLFSlideShowEncrypted.java | 5 +-
.../poi/hwpf/converter/AbstractWordConverter.java | 21 +++---
.../org/apache/poi/hwpf/usermodel/Picture.java | 6 +-
poi/src/main/java/org/apache/poi/hpsf/Section.java | 20 +++---
.../org/apache/poi/hssf/usermodel/HSSFChart.java | 75 ++++++++++------------
.../poi/hssf/usermodel/HSSFShapeFactory.java | 16 ++---
.../apache/poi/ss/formula/WorkbookEvaluator.java | 44 ++++++-------
.../ss/formula/eval/RelationalOperationEval.java | 27 +++-----
.../apache/poi/ss/formula/functions/Countif.java | 24 +++----
.../poi/ss/formula/functions/LookupUtils.java | 24 +++----
.../poi/ss/formula/functions/NumericFunction.java | 5 +-
.../org/apache/poi/util/GenericRecordUtil.java | 4 +-
16 files changed, 156 insertions(+), 170 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java
index 4c11900cbe..4667d7f787 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java
@@ -259,8 +259,10 @@ public class SXSSFCell extends CellBase {
if (_value instanceof PlainStringValue) {
_value = new StringFormulaValue(formula,
getStringCellValue());
} else {
- assert(_value instanceof RichTextValue);
- _value = new RichTextStringFormulaValue(formula,
((RichTextValue) _value).getValue());
+ if (!(_value instanceof RichTextValue richTextValue)) {
+ throw new AssertionError("Expected RichTextValue");
+ }
+ _value = new RichTextStringFormulaValue(formula,
richTextValue.getValue());
}
break;
case BOOLEAN:
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
index 20e33682e3..c5d6095db0 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
@@ -99,22 +99,22 @@ public class XWPFTableCell implements IBody, ICell {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
- if (o instanceof CTP) {
- XWPFParagraph p = new XWPFParagraph((CTP) o, this);
+ if (o instanceof CTP ctp) {
+ XWPFParagraph p = new XWPFParagraph(ctp, this);
paragraphs.add(p);
bodyElements.add(p);
}
- if (o instanceof CTTbl) {
- XWPFTable t = new XWPFTable((CTTbl) o, this, false);
+ if (o instanceof CTTbl ctTbl) {
+ XWPFTable t = new XWPFTable(ctTbl, this, false);
tables.add(t);
bodyElements.add(t);
}
- if (o instanceof CTSdtBlock) {
- XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
+ if (o instanceof CTSdtBlock sdtBlock) {
+ XWPFSDT c = new XWPFSDT(sdtBlock, this);
bodyElements.add(c);
}
- if (o instanceof CTSdtRun) {
- XWPFSDT c = new XWPFSDT((CTSdtRun) o, this);
+ if (o instanceof CTSdtRun sdtRun) {
+ XWPFSDT c = new XWPFSDT(sdtRun, this);
bodyElements.add(c);
}
}
@@ -508,17 +508,16 @@ public class XWPFTableCell implements IBody, ICell {
try (final XmlCursor cursor = cell.newCursor()) {
cursor.toParent();
o = cursor.getObject();
- if (!(o instanceof CTRow)) {
+ if (!(o instanceof CTRow ctRow)) {
return null;
}
- row = (CTRow) o;
+ row = ctRow;
cursor.toParent();
o = cursor.getObject();
}
- if (!(o instanceof CTTbl)) {
+ if (!(o instanceof CTTbl tbl)) {
return null;
}
- CTTbl tbl = (CTTbl) o;
XWPFTable table = getTable(tbl);
if (table == null) {
return null;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFHyperlink.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFHyperlink.java
index 2268ccde8e..8c8d23722d 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFHyperlink.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFHyperlink.java
@@ -167,8 +167,9 @@ public final class HSLFHyperlink implements
Hyperlink<HSLFShape,HSLFTextParagrap
@Override
public void linkToSlide(Slide<HSLFShape,HSLFTextParagraph> slide) {
- assert(slide instanceof HSLFSlide);
- HSLFSlide sl = (HSLFSlide)slide;
+ if (!(slide instanceof HSLFSlide sl)) {
+ throw new AssertionError("Expected HSLFSlide");
+ }
int slideNum = slide.getSlideNumber();
String alias = "Slide "+slideNum;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSheet.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSheet.java
index 4edaf94a3d..1b8c344702 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSheet.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSheet.java
@@ -172,17 +172,16 @@ public abstract class HSLFSheet implements
HSLFShapeContainer, Sheet<HSLFShape,H
continue;
}
- if (!(r instanceof EscherContainerRecord)) {
+ if (!(r instanceof EscherContainerRecord sp)) {
throw new IllegalArgumentException("Did not have a
EscherContainerRecord: " + r);
}
- EscherContainerRecord sp = (EscherContainerRecord)r;
HSLFShape sh = HSLFShapeFactory.createShape(sp, null);
sh.setSheet(this);
- if (sh instanceof HSLFSimpleShape) {
+ if (sh instanceof HSLFSimpleShape simpleShape) {
HSLFHyperlink link = HSLFHyperlink.find(sh);
if (link != null) {
- ((HSLFSimpleShape)sh).setHyperlink(link);
+ simpleShape.setHyperlink(link);
}
}
@@ -300,8 +299,7 @@ public abstract class HSLFSheet implements
HSLFShapeContainer, Sheet<HSLFShape,H
*/
public HSLFTextShape getPlaceholderByTextType(int type){
for (HSLFShape shape : getShapes()) {
- if(shape instanceof HSLFTextShape){
- HSLFTextShape tx = (HSLFTextShape)shape;
+ if(shape instanceof HSLFTextShape tx){
if (tx.getRunType() == type) {
return tx;
}
@@ -318,8 +316,7 @@ public abstract class HSLFSheet implements
HSLFShapeContainer, Sheet<HSLFShape,H
*/
public HSLFSimpleShape getPlaceholder(Placeholder type){
for (HSLFShape shape : getShapes()) {
- if (shape instanceof HSLFSimpleShape) {
- HSLFSimpleShape ss = (HSLFSimpleShape)shape;
+ if (shape instanceof HSLFSimpleShape ss) {
if (type == ss.getPlaceholder()) {
return ss;
}
@@ -428,10 +425,10 @@ public abstract class HSLFSheet implements
HSLFShapeContainer, Sheet<HSLFShape,H
@Override
public HSLFPictureShape createPicture(PictureData pictureData) {
- if (!(pictureData instanceof HSLFPictureData)) {
+ if (!(pictureData instanceof HSLFPictureData hslfPictureData)) {
throw new IllegalArgumentException("pictureData needs to be of
type HSLFPictureData");
}
- HSLFPictureShape s = new
HSLFPictureShape((HSLFPictureData)pictureData);
+ HSLFPictureShape s = new HSLFPictureShape(hslfPictureData);
s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s);
return s;
@@ -450,10 +447,10 @@ public abstract class HSLFSheet implements
HSLFShapeContainer, Sheet<HSLFShape,H
@Override
public HSLFObjectShape createOleShape(PictureData pictureData) {
- if (!(pictureData instanceof HSLFPictureData)) {
+ if (!(pictureData instanceof HSLFPictureData hslfPictureData)) {
throw new IllegalArgumentException("pictureData needs to be of
type HSLFPictureData");
}
- HSLFObjectShape s = new HSLFObjectShape((HSLFPictureData)pictureData);
+ HSLFObjectShape s = new HSLFObjectShape(hslfPictureData);
s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
addShape(s);
return s;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
index 8243b044b1..af29c2de6a 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
@@ -458,8 +458,9 @@ public class HSLFSlideShowEncrypted implements Closeable {
List<Integer> obsoleteOffsets = new ArrayList<>();
int duplicatedCount = 0;
for (org.apache.poi.hslf.record.Record r : records) {
- assert(r instanceof PositionDependentRecord);
- PositionDependentRecord pdr = (PositionDependentRecord)r;
+ if (!(r instanceof PositionDependentRecord pdr)) {
+ throw new AssertionError("Expected PositionDependentRecord");
+ }
if (pdr instanceof UserEditAtom) {
uea = (UserEditAtom)pdr;
continue;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java
index 0eca5fb49a..1ae8600746 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java
@@ -281,8 +281,7 @@ public abstract class AbstractWordConverter {
* reconstruct the structure of range -- sergey
*/
List<Structure> structures = new LinkedList<>();
- if (wordDocument instanceof HWPFDocument) {
- final HWPFDocument doc = (HWPFDocument) wordDocument;
+ if (wordDocument instanceof HWPFDocument doc) {
Map<Integer, List<Bookmark>> rangeBookmarks = doc.getBookmarks()
.getBookmarksStartedBetween(range.getStartOffset(),
@@ -354,10 +353,12 @@ public abstract class AbstractWordConverter {
}
if (structure.structure instanceof Bookmark) {
- assert(wordDocument instanceof HWPFDocument);
+ if (!(wordDocument instanceof HWPFDocument doc)) {
+ throw new AssertionError("Expected HWPFDocument");
+ }
// other bookmarks with same boundaries
List<Bookmark> bookmarks = new LinkedList<>();
- for (Bookmark bookmark : ((HWPFDocument) wordDocument)
+ for (Bookmark bookmark : doc
.getBookmarks()
.getBookmarksStartedBetween(structure.start,
structure.start + 1).values().iterator()
@@ -383,12 +384,12 @@ public abstract class AbstractWordConverter {
} finally {
bookmarkStack.removeAll(bookmarks);
}
- } else if (structure.structure instanceof Field) {
- assert(wordDocument instanceof HWPFDocument);
- Field field = (Field) structure.structure;
- processField((HWPFDocument) wordDocument, range,
currentTableLevel, field, block);
- } else if (structure.structure instanceof DeadFieldBoundaries) {
- DeadFieldBoundaries boundaries = (DeadFieldBoundaries)
structure.structure;
+ } else if (structure.structure instanceof Field field) {
+ if (!(wordDocument instanceof HWPFDocument doc)) {
+ throw new AssertionError("Expected HWPFDocument");
+ }
+ processField(doc, range, currentTableLevel, field, block);
+ } else if (structure.structure instanceof DeadFieldBoundaries
boundaries) {
processDeadField(wordDocument, block, range,
currentTableLevel, boundaries.beginMark,
boundaries.separatorMark, boundaries.endMark);
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/Picture.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/Picture.java
index 0713cf574b..e52047fb1b 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/Picture.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/Picture.java
@@ -590,8 +590,10 @@ public final class Picture {
}
Enum<?> recordType = escherRecord.getGenericRecordType();
- assert (recordType instanceof EscherRecordTypes);
- switch ((EscherRecordTypes)recordType) {
+ if (!(recordType instanceof EscherRecordTypes escherRecordType)) {
+ throw new AssertionError("Expected EscherRecordTypes");
+ }
+ switch (escherRecordType) {
case BLIP_EMF:
return PictureType.EMF;
case BLIP_WMF:
diff --git a/poi/src/main/java/org/apache/poi/hpsf/Section.java
b/poi/src/main/java/org/apache/poi/hpsf/Section.java
index e3d53275e4..32d4cf4c74 100644
--- a/poi/src/main/java/org/apache/poi/hpsf/Section.java
+++ b/poi/src/main/java/org/apache/poi/hpsf/Section.java
@@ -457,16 +457,16 @@ public class Section {
* supported by HPSF.
*/
public void setProperty(final int id, final Object value) {
- if (value instanceof String) {
- setProperty(id, (String) value);
- } else if (value instanceof Long) {
- setProperty(id, ((Long) value).longValue());
- } else if (value instanceof Integer) {
- setProperty(id, ((Integer) value).intValue());
- } else if (value instanceof Short) {
- setProperty(id, ((Short) value).intValue());
- } else if (value instanceof Boolean) {
- setProperty(id, ((Boolean) value).booleanValue());
+ if (value instanceof String s) {
+ setProperty(id, s);
+ } else if (value instanceof Long l) {
+ setProperty(id, l.longValue());
+ } else if (value instanceof Integer i) {
+ setProperty(id, i.intValue());
+ } else if (value instanceof Short s) {
+ setProperty(id, s.intValue());
+ } else if (value instanceof Boolean b) {
+ setProperty(id, b.booleanValue());
} else if (value instanceof Date) {
setProperty(id, Variant.VT_FILETIME, value);
} else {
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFChart.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFChart.java
index bdda8baf2f..fe40d1368c 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFChart.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFChart.java
@@ -192,12 +192,11 @@ public final class HSSFChart {
List<RecordBase> records = sheet.getSheet().getRecords();
for(RecordBase r : records) {
- if(r instanceof ChartRecord) {
+ if(r instanceof ChartRecord chartRecord) {
lastSeries = null;
- lastChart = new HSSFChart(sheet,(ChartRecord)r);
+ lastChart = new HSSFChart(sheet, chartRecord);
charts.add(lastChart);
- } else if (r instanceof LinkedDataRecord) {
- LinkedDataRecord linkedDataRecord = (LinkedDataRecord) r;
+ } else if (r instanceof LinkedDataRecord linkedDataRecord) {
if (lastSeries != null) {
lastSeries.insertData(linkedDataRecord);
}
@@ -207,27 +206,25 @@ public final class HSSFChart {
continue;
}
- if (r instanceof LegendRecord) {
- lastChart.legendRecord = (LegendRecord)r;
- } else if(r instanceof SeriesRecord) {
- HSSFSeries series = new HSSFSeries( (SeriesRecord)r );
+ if (r instanceof LegendRecord legendRecord) {
+ lastChart.legendRecord = legendRecord;
+ } else if(r instanceof SeriesRecord seriesRecord) {
+ HSSFSeries series = new HSSFSeries(seriesRecord);
lastChart.series.add(series);
lastSeries = series;
- } else if(r instanceof ChartTitleFormatRecord) {
- lastChart.chartTitleFormat = (ChartTitleFormatRecord)r;
- } else if(r instanceof SeriesTextRecord) {
+ } else if(r instanceof ChartTitleFormatRecord
chartTitleFormatRecord) {
+ lastChart.chartTitleFormat = chartTitleFormatRecord;
+ } else if(r instanceof SeriesTextRecord str) {
// Applies to a series, unless we've seen a legend already
- SeriesTextRecord str = (SeriesTextRecord)r;
if(lastChart.legendRecord == null &&
!lastChart.series.isEmpty()) {
HSSFSeries series =
lastChart.series.get(lastChart.series.size()-1);
series.seriesTitleText = str;
} else {
lastChart.chartTitleText = str;
}
- } else if(r instanceof ValueRangeRecord){
- lastChart.valueRanges.add((ValueRangeRecord)r);
- } else if (r instanceof Record) {
- Record record = (Record) r;
+ } else if(r instanceof ValueRangeRecord valueRangeRecord){
+ lastChart.valueRanges.add(valueRangeRecord);
+ } else if (r instanceof Record record) {
for (HSSFChartType type : HSSFChartType.values()) {
if (type == HSSFChartType.Unknown) {
continue;
@@ -1073,9 +1070,7 @@ public final class HSSFChart {
int lastCol = 0;
for (Ptg ptg : linkedDataRecord.getFormulaOfLink()) {
- if (ptg instanceof AreaPtgBase) {
- AreaPtgBase areaPtg = (AreaPtgBase) ptg;
-
+ if (ptg instanceof AreaPtgBase areaPtg) {
firstRow = areaPtg.getFirstRow();
lastRow = areaPtg.getLastRow();
@@ -1108,9 +1103,7 @@ public final class HSSFChart {
int colCount = (range.getLastColumn() - range.getFirstColumn()) +
1;
for (Ptg ptg : linkedDataRecord.getFormulaOfLink()) {
- if (ptg instanceof AreaPtgBase) {
- AreaPtgBase areaPtg = (AreaPtgBase) ptg;
-
+ if (ptg instanceof AreaPtgBase areaPtg) {
areaPtg.setFirstRow(range.getFirstRow());
areaPtg.setLastRow(range.getLastRow());
@@ -1220,31 +1213,31 @@ public final class HSSFChart {
newRecord = new BeginRecord();
} else if (record instanceof EndRecord) {
newRecord = new EndRecord();
- } else if (record instanceof SeriesRecord) {
- SeriesRecord seriesRecord = ((SeriesRecord) record).copy();
- newSeries = new HSSFSeries(seriesRecord);
- newRecord = seriesRecord;
- } else if (record instanceof LinkedDataRecord) {
- LinkedDataRecord linkedDataRecord = ((LinkedDataRecord)
record).copy();
+ } else if (record instanceof SeriesRecord seriesRecord) {
+ SeriesRecord copy = seriesRecord.copy();
+ newSeries = new HSSFSeries(copy);
+ newRecord = copy;
+ } else if (record instanceof LinkedDataRecord linkedDataRecord) {
+ LinkedDataRecord copy = linkedDataRecord.copy();
if (newSeries != null) {
- newSeries.insertData(linkedDataRecord);
+ newSeries.insertData(copy);
}
- newRecord = linkedDataRecord;
- } else if (record instanceof DataFormatRecord) {
- DataFormatRecord dataFormatRecord = ((DataFormatRecord)
record).copy();
+ newRecord = copy;
+ } else if (record instanceof DataFormatRecord dataFormatRecord) {
+ DataFormatRecord copy = dataFormatRecord.copy();
- dataFormatRecord.setSeriesIndex((short) seriesIdx) ;
- dataFormatRecord.setSeriesNumber((short) seriesIdx) ;
+ copy.setSeriesIndex((short) seriesIdx) ;
+ copy.setSeriesNumber((short) seriesIdx) ;
- newRecord = dataFormatRecord;
- } else if (record instanceof SeriesTextRecord) {
- SeriesTextRecord seriesTextRecord = ((SeriesTextRecord)
record).copy();
+ newRecord = copy;
+ } else if (record instanceof SeriesTextRecord seriesTextRecord) {
+ SeriesTextRecord copy = seriesTextRecord.copy();
if (newSeries != null) {
- newSeries.setSeriesTitleText(seriesTextRecord);
+ newSeries.setSeriesTitleText(copy);
}
- newRecord = seriesTextRecord;
- } else if (record instanceof Record) {
- newRecord = ((Record) record).copy();
+ newRecord = copy;
+ } else if (record instanceof Record rec) {
+ newRecord = rec.copy();
}
if (newRecord != null)
diff --git
a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
index 0dc114ac67..7bbff23636 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
@@ -52,16 +52,16 @@ public class HSSFShapeFactory {
if (container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
ObjRecord obj = null;
EscherRecord child = container.getChild(0);
- if (!(child instanceof EscherContainerRecord)) {
+ if (!(child instanceof EscherContainerRecord childContainer)) {
throw new IllegalArgumentException("Had unexpected type of
child: " + child.getClass());
}
- EscherClientDataRecord clientData = ((EscherContainerRecord)
child).getChildById(EscherClientDataRecord.RECORD_ID);
+ EscherClientDataRecord clientData =
childContainer.getChildById(EscherClientDataRecord.RECORD_ID);
if (null != clientData) {
Record record = agg.getShapeToObjMapping().get(clientData);
- if (!(record instanceof ObjRecord)) {
+ if (!(record instanceof ObjRecord objRec)) {
throw new IllegalArgumentException("Had unexpected type of
clientData: " + (record == null ? "<null>" : record.getClass()));
}
- obj = (ObjRecord) record;
+ obj = objRec;
}
HSSFShapeGroup group = new HSSFShapeGroup(container, obj);
List<EscherContainerRecord> children =
container.getChildContainers();
@@ -79,18 +79,18 @@ public class HSSFShapeFactory {
switch (EscherRecordTypes.forTypeID(record.getRecordId())) {
case CLIENT_DATA: {
Record subRecord = shapeToObj.get(record);
- if (!(subRecord instanceof ObjRecord)) {
+ if (!(subRecord instanceof ObjRecord objRec)) {
throw new RecordFormatException("Did not have a
ObjRecord: " + subRecord);
}
- objRecord = (ObjRecord) subRecord;
+ objRecord = objRec;
break;
}
case CLIENT_TEXTBOX: {
Record subRecord = shapeToObj.get(record);
- if (!(subRecord instanceof TextObjectRecord)) {
+ if (!(subRecord instanceof TextObjectRecord txtRec)) {
throw new RecordFormatException("Did not have a
TextObjRecord: " + subRecord);
}
- txtRecord = (TextObjectRecord) subRecord;
+ txtRecord = txtRec;
break;
}
default:
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 2b081fadb9..150c42cb08 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
@@ -654,29 +654,29 @@ public final class WorkbookEvaluator {
EvaluationName nameRecord = _workbook.getName(namePtg);
return getEvalForNameRecord(nameRecord, ec);
}
- if (ptg instanceof NameXPtg) {
+ if (ptg instanceof NameXPtg nameXPtg) {
// Externally defined named ranges or macro functions
- return processNameEval(ec.getNameXEval((NameXPtg) ptg), ec);
+ return processNameEval(ec.getNameXEval(nameXPtg), ec);
}
- if (ptg instanceof NameXPxg) {
+ if (ptg instanceof NameXPxg nameXPxg) {
// Externally defined named ranges or macro functions
- return processNameEval(ec.getNameXEval((NameXPxg) ptg), ec);
+ return processNameEval(ec.getNameXEval(nameXPxg), ec);
}
- if (ptg instanceof IntPtg) {
- return new NumberEval(((IntPtg) ptg).getValue());
+ if (ptg instanceof IntPtg intPtg) {
+ return new NumberEval(intPtg.getValue());
}
- if (ptg instanceof NumberPtg) {
- return new NumberEval(((NumberPtg) ptg).getValue());
+ if (ptg instanceof NumberPtg numberPtg) {
+ return new NumberEval(numberPtg.getValue());
}
- if (ptg instanceof StringPtg) {
- return new StringEval(((StringPtg) ptg).getValue());
+ if (ptg instanceof StringPtg stringPtg) {
+ return new StringEval(stringPtg.getValue());
}
- if (ptg instanceof BoolPtg) {
- return BoolEval.valueOf(((BoolPtg) ptg).getValue());
+ if (ptg instanceof BoolPtg boolPtg) {
+ return BoolEval.valueOf(boolPtg.getValue());
}
- if (ptg instanceof ErrPtg) {
- return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
+ if (ptg instanceof ErrPtg errPtg) {
+ return ErrorEval.valueOf(errPtg.getErrorCode());
}
if (ptg instanceof MissingArgPtg) {
return MissingArgEval.instance;
@@ -685,17 +685,17 @@ public final class WorkbookEvaluator {
|| ptg instanceof DeletedArea3DPtg || ptg instanceof
DeletedRef3DPtg) {
return ErrorEval.REF_INVALID;
}
- if (ptg instanceof Ref3DPtg) {
- return ec.getRef3DEval((Ref3DPtg) ptg);
+ if (ptg instanceof Ref3DPtg ref3DPtg) {
+ return ec.getRef3DEval(ref3DPtg);
}
- if (ptg instanceof Ref3DPxg) {
- return ec.getRef3DEval((Ref3DPxg) ptg);
+ if (ptg instanceof Ref3DPxg ref3DPxg) {
+ return ec.getRef3DEval(ref3DPxg);
}
- if (ptg instanceof Area3DPtg) {
- return ec.getArea3DEval((Area3DPtg) ptg);
+ if (ptg instanceof Area3DPtg area3DPtg) {
+ return ec.getArea3DEval(area3DPtg);
}
- if (ptg instanceof Area3DPxg) {
- return ec.getArea3DEval((Area3DPxg) ptg);
+ if (ptg instanceof Area3DPxg area3DPxg) {
+ return ec.getArea3DEval(area3DPxg);
}
if (ptg instanceof RefPtg rptg) {
return ec.getRefEval(rptg.getRow(), rptg.getColumn());
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
b/poi/src/main/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
index dafc5e0ed0..92f2da33c3 100644
---
a/poi/src/main/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
+++
b/poi/src/main/java/org/apache/poi/ss/formula/eval/RelationalOperationEval.java
@@ -92,10 +92,8 @@ public abstract class RelationalOperationEval extends
Fixed2ArgFunction implemen
return -compareBlank(va);
}
- if (va instanceof BoolEval) {
- if (vb instanceof BoolEval) {
- BoolEval bA = (BoolEval) va;
- BoolEval bB = (BoolEval) vb;
+ if (va instanceof BoolEval bA) {
+ if (vb instanceof BoolEval bB) {
if (bA.getBooleanValue() == bB.getBooleanValue()) {
return 0;
}
@@ -106,10 +104,8 @@ public abstract class RelationalOperationEval extends
Fixed2ArgFunction implemen
if (vb instanceof BoolEval) {
return -1;
}
- if (va instanceof StringEval) {
- if (vb instanceof StringEval) {
- StringEval sA = (StringEval) va;
- StringEval sB = (StringEval) vb;
+ if (va instanceof StringEval sA) {
+ if (vb instanceof StringEval sB) {
return
sA.getStringValue().compareToIgnoreCase(sB.getStringValue());
}
return 1;
@@ -117,10 +113,8 @@ public abstract class RelationalOperationEval extends
Fixed2ArgFunction implemen
if (vb instanceof StringEval) {
return -1;
}
- if (va instanceof NumberEval) {
- if (vb instanceof NumberEval) {
- NumberEval nA = (NumberEval) va;
- NumberEval nB = (NumberEval) vb;
+ if (va instanceof NumberEval nA) {
+ if (vb instanceof NumberEval nB) {
return NumberComparer.compare(nA.getNumberValue(),
nB.getNumberValue());
}
}
@@ -132,16 +126,13 @@ public abstract class RelationalOperationEval extends
Fixed2ArgFunction implemen
if (v == BlankEval.instance || v instanceof MissingArgEval) {
return 0;
}
- if (v instanceof BoolEval) {
- BoolEval boolEval = (BoolEval) v;
+ if (v instanceof BoolEval boolEval) {
return boolEval.getBooleanValue() ? -1 : 0;
}
- if (v instanceof NumberEval) {
- NumberEval ne = (NumberEval) v;
+ if (v instanceof NumberEval ne) {
return NumberComparer.compare(0.0, ne.getNumberValue());
}
- if (v instanceof StringEval) {
- StringEval se = (StringEval) v;
+ if (v instanceof StringEval se) {
return se.getStringValue().length() < 1 ? 0 : -1;
}
throw new IllegalArgumentException("bad value class (" +
v.getClass().getName() + ")");
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
index ce4d72a096..642f6298d2 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Countif.java
@@ -323,13 +323,13 @@ public final class Countif extends Fixed2ArgFunction {
false;
};
}
- if(!(x instanceof StringEval)) {
+ if(!(x instanceof StringEval stringEval)) {
// must always be string
// even if match str is wild, but contains only digits
// e.g. '4*7', NumberEval(4567) does not match
return false;
}
- String testedValue = ((StringEval) x).getStringValue();
+ String testedValue = stringEval.getStringValue();
if (testedValue.isEmpty() && _value.isEmpty()) {
// odd case: criteria '=' behaves differently to criteria ''
@@ -419,10 +419,10 @@ public final class Countif extends Fixed2ArgFunction {
*/
private double countMatchingCellsInArea(ValueEval rangeArg,
I_MatchPredicate criteriaPredicate) {
- if (rangeArg instanceof RefEval) {
- return CountUtils.countMatchingCellsInRef((RefEval) rangeArg,
criteriaPredicate);
- } else if (rangeArg instanceof ThreeDEval) {
- return CountUtils.countMatchingCellsInArea((ThreeDEval) rangeArg,
criteriaPredicate);
+ if (rangeArg instanceof RefEval refEval) {
+ return CountUtils.countMatchingCellsInRef(refEval,
criteriaPredicate);
+ } else if (rangeArg instanceof ThreeDEval threeDEval) {
+ return CountUtils.countMatchingCellsInArea(threeDEval,
criteriaPredicate);
} else {
throw new IllegalArgumentException("Bad range arg type (" +
rangeArg.getClass().getName() + ")");
}
@@ -436,15 +436,15 @@ public final class Countif extends Fixed2ArgFunction {
ValueEval evaluatedCriteriaArg = evaluateCriteriaArg(arg, srcRowIndex,
srcColumnIndex);
- if(evaluatedCriteriaArg instanceof NumberEval) {
- return new
NumberMatcher(((NumberEval)evaluatedCriteriaArg).getNumberValue(),
CmpOp.OP_NONE);
+ if(evaluatedCriteriaArg instanceof NumberEval numberEval) {
+ return new NumberMatcher(numberEval.getNumberValue(),
CmpOp.OP_NONE);
}
- if(evaluatedCriteriaArg instanceof BoolEval) {
- return new
BooleanMatcher(((BoolEval)evaluatedCriteriaArg).getBooleanValue(),
CmpOp.OP_NONE);
+ if(evaluatedCriteriaArg instanceof BoolEval boolEval) {
+ return new BooleanMatcher(boolEval.getBooleanValue(),
CmpOp.OP_NONE);
}
- if(evaluatedCriteriaArg instanceof StringEval) {
- return
createGeneralMatchPredicate((StringEval)evaluatedCriteriaArg);
+ if(evaluatedCriteriaArg instanceof StringEval stringEval) {
+ return createGeneralMatchPredicate(stringEval);
}
if(evaluatedCriteriaArg instanceof ErrorEval) {
return new
ErrorMatcher(((ErrorEval)evaluatedCriteriaArg).getErrorCode(), CmpOp.OP_NONE);
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
index 0c260e3975..db3885ea33 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
@@ -428,8 +428,8 @@ public final class LookupUtils {
private static final class TolerantStringLookupComparer extends
StringLookupComparer {
static StringEval convertToStringEval(ValueEval eval) {
- if (eval instanceof StringEval) {
- return (StringEval)eval;
+ if (eval instanceof StringEval se) {
+ return se;
}
String sv = OperandResolver.coerceValueToString(eval);
return new StringEval(sv);
@@ -525,8 +525,7 @@ public final class LookupUtils {
throw EvaluationException.invalidRef();
}
int oneBasedIndex;
- if(veRowColIndexArg instanceof StringEval) {
- StringEval se = (StringEval) veRowColIndexArg;
+ if(veRowColIndexArg instanceof StringEval se) {
String strVal = se.getStringValue();
Double dVal = OperandResolver.parseDouble(strVal);
if(dVal == null) {
@@ -552,12 +551,11 @@ public final class LookupUtils {
* which case it is interpreted as a 1x1 area ref. Other scalar values
cause #VALUE! error.
*/
public static TwoDEval resolveTableArrayArg(ValueEval eval) throws
EvaluationException {
- if (eval instanceof TwoDEval) {
- return (TwoDEval) eval;
+ if (eval instanceof TwoDEval twoDEval) {
+ return twoDEval;
}
- if(eval instanceof RefEval) {
- RefEval refEval = (RefEval) eval;
+ if(eval instanceof RefEval refEval) {
// Make this cell ref look like a 1x1 area ref.
// It doesn't matter if eval is a 2D or 3D ref, because that
detail is never asked of AreaEval.
@@ -585,14 +583,13 @@ public final class LookupUtils {
// this does not get the default value
return false;
}
- if(valEval instanceof BoolEval) {
+ if(valEval instanceof BoolEval boolEval) {
// Happy day flow
- BoolEval boolEval = (BoolEval) valEval;
return boolEval.getBooleanValue();
}
- if (valEval instanceof StringEval) {
- String stringValue = ((StringEval) valEval).getStringValue();
+ if (valEval instanceof StringEval stringEval) {
+ String stringValue = stringEval.getStringValue();
if(stringValue.isEmpty()) {
// More trickiness:
// Empty string is not the same as BlankEval. It causes
#VALUE! error
@@ -611,8 +608,7 @@ public final class LookupUtils {
// This is in contrast to the code below,, where NumberEvals
values (for
// example 0.01) *do* resolve to equivalent boolean values.
}
- if (valEval instanceof NumericValueEval) {
- NumericValueEval nve = (NumericValueEval) valEval;
+ if (valEval instanceof NumericValueEval nve) {
// zero is FALSE, everything else is TRUE
return 0.0 != nve.getNumberValue();
}
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
index e0f828abc0..ce66981b66 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
@@ -261,8 +261,9 @@ public abstract class NumericFunction implements Function {
if (res instanceof ErrorEval) {
return (ErrorEval)res;
}
- assert(res instanceof Double);
- double d = (Double)res;
+ if (!(res instanceof Double d)) {
+ throw new AssertionError("Expected Double");
+ }
return (Double.isNaN(d) || Double.isInfinite(d)) ?
ErrorEval.NUM_ERROR : new NumberEval(d);
} catch (EvaluationException e) {
return e.getErrorEval();
diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java
b/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java
index a341b060c3..1bb6bffb18 100644
--- a/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java
+++ b/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java
@@ -127,7 +127,9 @@ public final class GenericRecordUtil {
assert(sups[i] != null);
if ("base".equals(vals[i])) {
Object baseMap = sups[i].get();
- assert(baseMap instanceof Map);
+ if (!(baseMap instanceof Map)) {
+ throw new AssertionError("Expected Map");
+ }
//noinspection unchecked
m.putAll((Map<String,Supplier<?>>)baseMap);
} else {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]