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 c192e8c54d use StringUtil.equalsIgnoreCase (#1138)
c192e8c54d is described below
commit c192e8c54dc6c33801532dff92f71c4f3e80e27e
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Jun 12 13:49:15 2026 +0100
use StringUtil.equalsIgnoreCase (#1138)
* use StringUtil.equalsIgnoreCase
* Update TestCellFormatPart.java
---
.../poi/ss/tests/format/TestCellFormatPart.java | 2 +
.../apache/poi/hssf/extractor/ExcelExtractor.java | 3 +-
.../apache/poi/hssf/model/InternalWorkbook.java | 5 +-
.../java/org/apache/poi/hssf/model/LinkTable.java | 5 +-
.../apache/poi/hssf/usermodel/HSSFDataFormat.java | 3 +-
.../poi/hssf/usermodel/HSSFEvaluationWorkbook.java | 3 +-
.../org/apache/poi/hssf/usermodel/HSSFName.java | 6 +-
.../apache/poi/hssf/usermodel/HSSFWorkbook.java | 5 +-
.../org/apache/poi/poifs/crypt/HashAlgorithm.java | 3 +-
.../java/org/apache/poi/poifs/dev/POIFSDump.java | 23 ++--
.../java/org/apache/poi/poifs/dev/POIFSLister.java | 6 +-
.../apache/poi/sl/draw/BitmapImageRenderer.java | 4 +-
.../poi/sl/extractor/SlideShowExtractor.java | 35 ++---
.../org/apache/poi/ss/format/CellFormatPart.java | 3 +-
.../poi/ss/formula/DataValidationEvaluator.java | 23 ++--
.../formula/EvaluationConditionalFormatRule.java | 2 +-
.../org/apache/poi/ss/formula/FormulaParser.java | 62 ++++-----
.../org/apache/poi/ss/formula/FormulaShifter.java | 56 +++-----
.../apache/poi/ss/formula/SheetNameFormatter.java | 13 +-
.../poi/ss/formula/eval/OperandResolver.java | 34 +++--
.../apache/poi/ss/formula/functions/Countif.java | 147 +++++++++------------
.../poi/ss/formula/functions/DStarRunner.java | 27 ++--
.../poi/ss/formula/functions/NumericFunction.java | 3 +-
.../apache/poi/ss/usermodel/BuiltinFormats.java | 4 +-
.../org/apache/poi/ss/usermodel/DataFormatter.java | 4 +-
.../java/org/apache/poi/ss/util/SheetUtil.java | 3 +-
26 files changed, 227 insertions(+), 257 deletions(-)
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java
b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java
index 18254af451..2dc908a292 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/format/TestCellFormatPart.java
@@ -43,6 +43,7 @@ import org.apache.poi.xssf.XSSFITestDataProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
/**
* Class for spreadsheet-based tests, such as are used for cell formatting.
@@ -55,6 +56,7 @@ import org.junit.jupiter.api.Test;
* flag "Categories" is not empty, only tests that have at least one category
* listed in "Categories" are run.
*/
+@Isolated // modifies the default locale and we don't want to affect other
tests running in parallel
class TestCellFormatPart {
private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile(
"([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))",
diff --git
a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
index 02315ffb29..b6f23d0bc4 100644
--- a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
+++ b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
@@ -38,6 +38,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
+import org.apache.poi.util.StringUtil;
/**
* A text extractor for Excel files.
@@ -114,7 +115,7 @@ public class ExcelExtractor implements
POIOLE2TextExtractor, org.apache.poi.ss.e
boolean headersFooters = true;
for (int i=0; i<nArgs; i++) {
String arg = args[i];
- if ("-help".equalsIgnoreCase(arg)) {
+ if (StringUtil.equalsIgnoreCase("-help", arg)) {
requestHelp = true;
break;
}
diff --git a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
index d1d86af0ff..48549efcfd 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java
@@ -60,6 +60,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.RecordFormatException;
+import org.apache.poi.util.StringUtil;
/**
* Low level model implementation of a Workbook. Provides creational methods
@@ -603,7 +604,7 @@ public final class InternalWorkbook {
if (bName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
bName = bName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
}
- if (aName.equalsIgnoreCase(bName)) {
+ if (StringUtil.equalsIgnoreCase(aName, bName)) {
return true;
}
}
@@ -725,7 +726,7 @@ public final class InternalWorkbook {
for (int k = 0; k < size; k++) {
String sheet = getSheetName(k);
- if (sheet.equalsIgnoreCase(name)) {
+ if (StringUtil.equalsIgnoreCase(sheet, name)) {
retval = k;
break;
}
diff --git a/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java
b/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java
index 79ec7f0b9f..05bf6f2439 100644
--- a/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java
+++ b/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java
@@ -38,6 +38,7 @@ import org.apache.poi.ss.formula.ptg.NameXPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.Ref3DPtg;
import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.StringUtil;
/**
* Link Table (OOO pdf reference: 4.10.3 ) <p>
@@ -167,7 +168,7 @@ final class LinkTable {
*/
public int getIndexOfName(String name) {
for (int i = 0; i < _externalNameRecords.length; i++) {
- if (_externalNameRecords[i].getText().equalsIgnoreCase(name)) {
+ if
(StringUtil.equalsIgnoreCase(_externalNameRecords[i].getText(), name)) {
return i;
}
}
@@ -359,7 +360,7 @@ final class LinkTable {
}
private static boolean isDuplicatedNames(NameRecord firstName, NameRecord
lastName) {
- return lastName.getNameText().equalsIgnoreCase(firstName.getNameText())
+ return StringUtil.equalsIgnoreCase(lastName.getNameText(),
firstName.getNameText())
&& isSameSheetNames(firstName, lastName);
}
diff --git
a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFDataFormat.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFDataFormat.java
index 0badfb51d1..7f1c1dd95c 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFDataFormat.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFDataFormat.java
@@ -26,6 +26,7 @@ import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.FormatRecord;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormat;
+import org.apache.poi.util.StringUtil;
/**
* Identifies both built-in and user defined formats within a workbook.<p>
@@ -85,7 +86,7 @@ public final class HSSFDataFormat implements DataFormat {
public short getFormat(String pFormat) {
// Normalise the format string
String format;
- if (pFormat.equalsIgnoreCase("TEXT")) {
+ if (StringUtil.equalsIgnoreCase(pFormat, "TEXT")) {
format = "@";
} else {
format = pFormat;
diff --git
a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
index 5306b63520..efd766b1d0 100644
---
a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
+++
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
@@ -39,6 +39,7 @@ import org.apache.poi.ss.usermodel.Table;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Internal;
+import org.apache.poi.util.StringUtil;
/**
* Internal POI use only
@@ -113,7 +114,7 @@ public final class HSSFEvaluationWorkbook implements
FormulaRenderingWorkbook, E
public EvaluationName getName(String name, int sheetIndex) {
for(int i=0; i < _iBook.getNumNames(); i++) {
NameRecord nr = _iBook.getNameRecord(i);
- if (nr.getSheetNumber() == sheetIndex+1 &&
name.equalsIgnoreCase(nr.getNameText())) {
+ if (nr.getSheetNumber() == sheetIndex+1 &&
StringUtil.equalsIgnoreCase(name, nr.getNameText())) {
return new Name(nr, i);
}
}
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFName.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFName.java
index 60fdffa599..2a7cf536f6 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFName.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFName.java
@@ -26,6 +26,7 @@ import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.util.StringUtil;
/**
* High Level Representation of a 'defined name' which could be a 'built-in'
name,
@@ -144,7 +145,8 @@ public final class HSSFName implements Name {
{
NameRecord rec = wb.getNameRecord(i);
if (rec != _definedNameRec) {
- if (rec.getNameText().equalsIgnoreCase(nameName) &&
sheetNumber == rec.getSheetNumber()){
+ if (StringUtil.equalsIgnoreCase(rec.getNameText(), nameName) &&
+ sheetNumber == rec.getSheetNumber()) {
String msg = "The "+(sheetNumber == 0 ? "workbook" :
"sheet")+" already contains this name: " + nameName;
_definedNameRec.setNameText(nameName + "(2)");
throw new IllegalArgumentException(msg);
@@ -186,7 +188,7 @@ public final class HSSFName implements Name {
if (name.length() > 255) {
throw new IllegalArgumentException("Invalid name: '"+name+"':
cannot exceed 255 characters in length");
}
- if (name.equalsIgnoreCase("R") || name.equalsIgnoreCase("C")) {
+ if (StringUtil.equalsIgnoreCase(name, "R") ||
StringUtil.equalsIgnoreCase(name, "C")) {
throw new IllegalArgumentException("Invalid name: '"+name+"':
cannot be special shorthand R or C");
}
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index 50a9efc74f..352ca6afee 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -114,6 +114,7 @@ import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
import org.apache.poi.util.Removal;
+import org.apache.poi.util.StringUtil;
/**
* High level representation of a workbook. This is the first object most
users
@@ -1163,7 +1164,7 @@ public final class HSSFWorkbook extends POIDocument
implements Workbook {
for (int k = 0; k < _sheets.size(); k++) {
String sheetname = workbook.getSheetName(k);
- if (sheetname.equalsIgnoreCase(name)) {
+ if (StringUtil.equalsIgnoreCase(sheetname, name)) {
return _sheets.get(k);
}
}
@@ -1934,7 +1935,7 @@ public final class HSSFWorkbook extends POIDocument
implements Workbook {
for (int k = 0; k < names.size(); k++) {
String nameName = getNameName(k);
- if (nameName.equalsIgnoreCase(name)) {
+ if (StringUtil.equalsIgnoreCase(nameName, name)) {
return k;
}
}
diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/HashAlgorithm.java
b/poi/src/main/java/org/apache/poi/poifs/crypt/HashAlgorithm.java
index 10daa97b01..8475d2f3ac 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/HashAlgorithm.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/HashAlgorithm.java
@@ -18,6 +18,7 @@
package org.apache.poi.poifs.crypt;
import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.util.StringUtil;
public enum HashAlgorithm {
none ( "", 0x0000, "", 0, "", false,
""),
@@ -83,7 +84,7 @@ public enum HashAlgorithm {
public static HashAlgorithm fromString(String string) {
for (HashAlgorithm ha : values()) {
- if (ha.ecmaString.equalsIgnoreCase(string) ||
ha.jceId.equalsIgnoreCase(string)) {
+ if (StringUtil.equalsIgnoreCase(ha.ecmaString, string) ||
StringUtil.equalsIgnoreCase(ha.jceId, string)) {
return ha;
}
}
diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
index c06647c41f..8074a6d599 100644
--- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
+++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
@@ -35,6 +35,9 @@ import org.apache.poi.poifs.filesystem.POIFSStream;
import org.apache.poi.poifs.property.PropertyTable;
import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.StringUtil;
+
+import static org.apache.poi.util.StringUtil.equalsIgnoreCase;
/**
* Dump internal structure of a OLE2 file into file system
@@ -51,16 +54,16 @@ public final class POIFSDump {
boolean dumpProps = false, dumpMini = false;
for (String filename : args) {
- if (filename.equalsIgnoreCase("-dumprops") ||
- filename.equalsIgnoreCase("-dump-props") ||
- filename.equalsIgnoreCase("-dump-properties")) {
+ if (equalsIgnoreCase(filename, "-dumprops") ||
+ equalsIgnoreCase(filename, "-dump-props") ||
+ equalsIgnoreCase(filename, "-dump-properties")) {
dumpProps = true;
continue;
}
- if (filename.equalsIgnoreCase("-dumpmini") ||
- filename.equalsIgnoreCase("-dump-mini") ||
- filename.equalsIgnoreCase("-dump-ministream") ||
- filename.equalsIgnoreCase("-dump-mini-stream")) {
+ if (equalsIgnoreCase(filename, "-dumpmini") ||
+ equalsIgnoreCase(filename, "-dump-mini") ||
+ equalsIgnoreCase(filename, "-dump-ministream") ||
+ equalsIgnoreCase(filename, "-dump-mini-stream")) {
dumpMini = true;
continue;
}
@@ -98,8 +101,7 @@ public final class POIFSDump {
public static void dump(DirectoryEntry root, File parent) throws
IOException {
for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
Entry entry = it.next();
- if(entry instanceof DocumentNode){
- final DocumentNode node = (DocumentNode) entry;
+ if(entry instanceof DocumentNode node) {
final byte[] bytes;
try (DocumentInputStream is = new DocumentInputStream(node)) {
bytes = IOUtils.toByteArray(is);
@@ -108,8 +110,7 @@ public final class POIFSDump {
IOUtils.newFile(parent,
node.getName().trim()).toPath())) {
out.write(bytes);
}
- } else if (entry instanceof DirectoryEntry){
- DirectoryEntry dir = (DirectoryEntry)entry;
+ } else if (entry instanceof DirectoryEntry dir) {
File file = IOUtils.newFile(parent, entry.getName());
if (!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " +
file);
diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java
b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java
index 5c2932bcfe..4a5d7e6ea2 100644
--- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java
+++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java
@@ -29,6 +29,8 @@ import org.apache.poi.poifs.filesystem.DocumentNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import static org.apache.poi.util.StringUtil.equalsIgnoreCase;
+
/**
* A lister of the entries in POIFS files.
*
@@ -49,9 +51,9 @@ public class POIFSLister {
boolean withSizes = false;
boolean newPOIFS = true;
for (String arg : args) {
- if (arg.equalsIgnoreCase("-size") || arg.equalsIgnoreCase("-sizes")) {
+ if (equalsIgnoreCase(arg, "-size") || equalsIgnoreCase(arg,
"-sizes")) {
withSizes = true;
- } else if (arg.equalsIgnoreCase("-old") ||
arg.equalsIgnoreCase("-old-poifs")) {
+ } else if (equalsIgnoreCase(arg, "-old") || equalsIgnoreCase(arg,
"-old-poifs")) {
newPOIFS = false;
} else {
if (newPOIFS) {
diff --git a/poi/src/main/java/org/apache/poi/sl/draw/BitmapImageRenderer.java
b/poi/src/main/java/org/apache/poi/sl/draw/BitmapImageRenderer.java
index e9920f720f..87775e72d5 100644
--- a/poi/src/main/java/org/apache/poi/sl/draw/BitmapImageRenderer.java
+++ b/poi/src/main/java/org/apache/poi/sl/draw/BitmapImageRenderer.java
@@ -48,6 +48,7 @@ import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.MathUtil;
+import org.apache.poi.util.StringUtil;
/**
* For now this class renders only images supported by the
javax.imageio.ImageIO framework.
@@ -79,7 +80,8 @@ public class BitmapImageRenderer implements ImageRenderer {
@Override
public boolean canRender(String contentType) {
- return Stream.of(ALLOWED_TYPES).anyMatch(t ->
t.contentType.equalsIgnoreCase(contentType));
+ return Stream.of(ALLOWED_TYPES).anyMatch(t ->
+ StringUtil.equalsIgnoreCase(t.contentType, contentType));
}
@Override
diff --git
a/poi/src/main/java/org/apache/poi/sl/extractor/SlideShowExtractor.java
b/poi/src/main/java/org/apache/poi/sl/extractor/SlideShowExtractor.java
index 5fe11234ee..b3214a97b8 100644
--- a/poi/src/main/java/org/apache/poi/sl/extractor/SlideShowExtractor.java
+++ b/poi/src/main/java/org/apache/poi/sl/extractor/SlideShowExtractor.java
@@ -45,6 +45,7 @@ import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.sl.usermodel.TextShape;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.StringUtil;
/**
* Common SlideShow extractor
@@ -169,16 +170,15 @@ public class SlideShowExtractor<
return;
}
for (final Shape<S,P> shape : master) {
- if (shape instanceof TextShape) {
- final TextShape<S,P> ts = (TextShape<S,P>)shape;
+ if (shape instanceof TextShape<S,P> ts) {
final String text = ts.getText();
if (text == null || text.isEmpty() || "*".equals(text)) {
continue;
}
if (ts.isPlaceholder()) {
- // don't bother about boiler plate text on master sheets
- LOG.atInfo().log("Ignoring boiler plate (placeholder) text
on slide master: {}", text);
+ // don't bother about boilerplate text on master sheets
+ LOG.atInfo().log("Ignoring boilerplate (placeholder) text
on slide master: {}", text);
continue;
}
@@ -367,16 +367,11 @@ public class SlideShowExtractor<
txt = txt.replace('\r', '\n');
txt = txt.replace((char) 0x0B, sep);
- switch (tr.getTextCap()) {
- case ALL:
- txt = txt.toUpperCase(LocaleUtil.getUserLocale());
- break;
- case SMALL:
- txt = txt.toLowerCase(LocaleUtil.getUserLocale());
- break;
- }
-
- return txt;
+ return switch (tr.getTextCap()) {
+ case ALL -> txt.toUpperCase(LocaleUtil.getUserLocale());
+ case SMALL -> txt.toLowerCase(LocaleUtil.getUserLocale());
+ default -> txt;
+ };
}
/**
@@ -406,14 +401,12 @@ public class SlideShowExtractor<
return glyphs;
}
private static boolean filterFonts(Object o, String typeface, Boolean
italic, Boolean bold) {
- if (!(o instanceof TextRun)) {
- return false;
+ if (o instanceof TextRun tr) {
+ return StringUtil.equalsIgnoreCase(typeface, tr.getFontFamily()) &&
+ (italic == null || tr.isItalic() == italic) &&
+ (bold == null || tr.isBold() == bold);
}
- TextRun tr = (TextRun)o;
- return
- typeface.equalsIgnoreCase(tr.getFontFamily()) &&
- (italic == null || tr.isItalic() == italic) &&
- (bold == null || tr.isBold() == bold);
+ return false;
}
@Override
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 3bf263b821..11e1b770b7 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
@@ -32,6 +32,7 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.apache.poi.ss.format.CellFormatter.quote;
+import static org.apache.poi.util.StringUtil.equalsIgnoreCase;
/**
* Objects of this class represent a single part of a cell format expression.
@@ -390,7 +391,7 @@ public class CellFormatPart {
*/
private CellFormatType formatType(String fdesc) {
fdesc = fdesc.trim();
- if (fdesc.isEmpty() || fdesc.equalsIgnoreCase("General"))
+ if (fdesc.isEmpty() || equalsIgnoreCase(fdesc, "General"))
return CellFormatType.GENERAL;
Matcher m = SPECIFICATION_PAT.matcher(fdesc);
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
index b2e4bc1e06..4296861cb9 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
@@ -44,6 +44,8 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.util.StringUtil;
+import static org.apache.poi.util.StringUtil.equalsIgnoreCase;
+
/**
* Evaluates Data Validation constraints.<p>
*
@@ -60,7 +62,7 @@ public class DataValidationEvaluator {
/**
* Expensive to compute, so cache them as they are retrieved.
* <p>
- * Sheets don't implement equals, and since its an interface,
+ * Sheets don't implement equals, and since it's an interface,
* there's no guarantee instances won't be recreated on the fly by some
implementation.
* So we use sheet name.
*/
@@ -318,11 +320,11 @@ public class DataValidationEvaluator {
continue; // check the rest
}
}
- if (comp instanceof StringEval) {
+ if (comp instanceof StringEval se) {
// interestingly, in testing, a validation value of
the string "TRUE" or "true"
// did not match a boolean cell value of TRUE - so
apparently cell type matters
// also, Excel validation is case insensitive - "true"
is valid for the list value "TRUE"
- if (isType(cell, CellType.STRING) && ((StringEval)
comp).getStringValue().equalsIgnoreCase(cell.getStringCellValue())) {
+ if (isType(cell, CellType.STRING) &&
equalsIgnoreCase(se.getStringValue(), cell.getStringCellValue())) {
return true;
} else {
continue; // check the rest;
@@ -338,7 +340,7 @@ public class DataValidationEvaluator {
public boolean isValidValue(Cell cell, DataValidationContext
context) {
if (! isType(cell, CellType.STRING)) return false;
String v = cell.getStringCellValue();
- return isValidNumericValue(Double.valueOf(v.length()),
context);
+ return isValidNumericValue((double) v.length(), context);
}
},
FORMULA {
@@ -383,8 +385,7 @@ public class DataValidationEvaluator {
protected boolean isValidNumericCell(Cell cell, DataValidationContext
context) {
if ( ! isType(cell, CellType.NUMERIC)) return false;
- Double value = Double.valueOf(cell.getNumericCellValue());
- return isValidNumericValue(value, context);
+ return isValidNumericValue(cell.getNumericCellValue(), context);
}
/**
@@ -427,13 +428,13 @@ public class DataValidationEvaluator {
}
// note the call to the "unwrapped" version, which returns a
single value
ValueEval eval =
context.getEvaluator().getWorkbookEvaluator().evaluate(formula,
context.getTarget(), context.getRegion());
- if (eval instanceof RefEval) {
- eval = ((RefEval) eval).getInnerValueEval(((RefEval)
eval).getFirstSheetIndex());
+ if (eval instanceof RefEval re) {
+ eval = re.getInnerValueEval(re.getFirstSheetIndex());
}
if (eval instanceof BlankEval) return null;
- if (eval instanceof NumberEval) return
Double.valueOf(((NumberEval) eval).getNumberValue());
- if (eval instanceof StringEval) {
- final String value = ((StringEval) eval).getStringValue();
+ if (eval instanceof NumberEval ne) return ne.getNumberValue();
+ if (eval instanceof StringEval se) {
+ final String value = se.getStringValue();
if (StringUtil.isBlank(value)) return null;
// try to parse the cell value as a double and return it
return Double.valueOf(value);
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 339856f0c9..d3c78b9408 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
@@ -248,7 +248,7 @@ public class EvaluationConditionalFormatRule implements
Comparable<EvaluationCon
return false;
}
final EvaluationConditionalFormatRule r =
(EvaluationConditionalFormatRule) obj;
- return
getSheet().getSheetName().equalsIgnoreCase(r.getSheet().getSheetName())
+ return StringUtil.equalsIgnoreCase(getSheet().getSheetName(),
r.getSheet().getSheetName())
&& getFormattingIndex() == r.getFormattingIndex()
&& getRuleIndex() == r.getRuleIndex();
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
index c748231838..3c070d16e8 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
@@ -82,6 +82,8 @@ import org.apache.poi.ss.util.CellReference.NameType;
import org.apache.poi.util.Internal;
import org.apache.poi.util.StringUtil;
+import static org.apache.poi.util.StringUtil.equalsIgnoreCase;
+
/**
* This class parses a formula string into a List of tokens in RPN order.
* Inspired by "Lets Build a Compiler" by Jack Crenshaw
@@ -320,7 +322,7 @@ public final class FormulaParser {
value.appendCodePoint(this.look);
nextChar();
}
- return value.length() == 0 ? null : value.toString();
+ return value.isEmpty() ? null : value.toString();
}
private ParseNode parseRangeExpression() {
@@ -412,8 +414,7 @@ public final class FormulaParser {
}
// next 2 are special cases of OperationPtg
- if (tkn instanceof AbstractFunctionPtg) {
- AbstractFunctionPtg afp = (AbstractFunctionPtg) tkn;
+ if (tkn instanceof AbstractFunctionPtg afp) {
byte returnClass = afp.getDefaultOperandClass();
//CLASS_VALUE was added as valid to support example 6 in
//https://support.microsoft.com/en-us/office/xlookup-function-b7fd680e-6d10-43e6-84f9-88eae8bf5929
@@ -929,8 +930,8 @@ public final class FormulaParser {
if(look == '['){
return parseStructuredReference(name);
}
- if (name.equalsIgnoreCase("TRUE") || name.equalsIgnoreCase("FALSE")) {
- return new
ParseNode(BoolPtg.valueOf(name.equalsIgnoreCase("TRUE")));
+ if (equalsIgnoreCase(name, "TRUE") || equalsIgnoreCase(name, "FALSE"))
{
+ return new ParseNode(BoolPtg.valueOf(equalsIgnoreCase(name,
"TRUE")));
}
if (_book == null) {
// Only test cases omit the book (expecting it not to be needed)
@@ -978,15 +979,13 @@ public final class FormulaParser {
if (ch > 128) {
return true;
}
- switch (ch) {
- case '.':
- case '_':
- case '?':
- case '\\': // of all things
- return true;
- }
- // includes special non-name control characters like ! $ : , ( ) [ ]
and space
- return false;
+ return switch (ch) {
+ case '.', '_', '?', '\\' -> // of all things
+ true;
+ default ->
+ // includes special non-name control characters like ! $ : , (
) [ ] and space
+ false;
+ };
}
/**
@@ -1285,13 +1284,12 @@ public final class FormulaParser {
if (ch > 128) {
return true;
}
- switch(ch) {
- case '.': // dot is OK
- case '_': // underscore is OK
- case ' ': // space is OK
- return true;
- }
- return false;
+ return switch (ch) { // dot is OK
+ // underscore is OK
+ case '.', '_', ' ' -> // space is OK
+ true;
+ default -> false;
+ };
}
/**
@@ -1688,10 +1686,10 @@ public final class FormulaParser {
private Boolean parseBooleanLiteral() {
String iden = parseUnquotedIdentifier();
- if ("TRUE".equalsIgnoreCase(iden)) {
+ if (StringUtil.equalsIgnoreCase("TRUE", iden)) {
return Boolean.TRUE;
}
- if ("FALSE".equalsIgnoreCase(iden)) {
+ if (StringUtil.equalsIgnoreCase("FALSE", iden)) {
return Boolean.FALSE;
}
throw expected("'TRUE' or 'FALSE'");
@@ -1825,7 +1823,7 @@ public final class FormulaParser {
sb.appendCodePoint(look);
nextChar();
}
- if (sb.length() < 1) {
+ if (sb.isEmpty()) {
return null;
}
@@ -1993,15 +1991,17 @@ public final class FormulaParser {
}
return GreaterThanPtg.instance;
}
- switch(look) {
- case '=':
+ return switch (look) {
+ case '=' -> {
match('=');
- return LessEqualPtg.instance;
- case '>':
+ yield LessEqualPtg.instance;
+ }
+ case '>' -> {
match('>');
- return NotEqualPtg.instance;
- }
- return LessThanPtg.instance;
+ yield NotEqualPtg.instance;
+ }
+ default -> LessThanPtg.instance;
+ };
}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaShifter.java
b/poi/src/main/java/org/apache/poi/ss/formula/FormulaShifter.java
index 0af837c30a..a2edfccaff 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaShifter.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaShifter.java
@@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.ptg.Ref3DPxg;
import org.apache.poi.ss.formula.ptg.RefErrorPtg;
import org.apache.poi.ss.formula.ptg.RefPtg;
import org.apache.poi.ss.formula.ptg.RefPtgBase;
+import org.apache.poi.util.StringUtil;
/**
@@ -188,16 +189,14 @@ public final class FormulaShifter {
* or null (if no Ptg change is needed)
*/
private Ptg adjustPtgDueToMove(Ptg ptg, int currentExternSheetIx, boolean
isRowMove) {
- if(ptg instanceof RefPtg) {
+ if(ptg instanceof RefPtg rptg) {
if (currentExternSheetIx != _externSheetIndex) {
// local refs on other sheets are unaffected
return null;
}
- RefPtg rptg = (RefPtg)ptg;
return isRowMove ? rowMoveRefPtg(rptg) : columnMoveRefPtg(rptg);
}
- if(ptg instanceof Ref3DPtg) {
- Ref3DPtg rptg = (Ref3DPtg)ptg;
+ if(ptg instanceof Ref3DPtg rptg) {
if (_externSheetIndex != rptg.getExternSheetIndex()) {
// only move 3D refs that refer to the sheet with cells being
moved
// (currentExternSheetIx is irrelevant)
@@ -205,25 +204,22 @@ public final class FormulaShifter {
}
return isRowMove ? rowMoveRefPtg(rptg) : columnMoveRefPtg(rptg);
}
- if(ptg instanceof Ref3DPxg) {
- Ref3DPxg rpxg = (Ref3DPxg)ptg;
+ if(ptg instanceof Ref3DPxg rpxg) {
if (rpxg.getExternalWorkbookNumber() > 0 ||
- ! _sheetName.equalsIgnoreCase(rpxg.getSheetName())) {
+ !StringUtil.equalsIgnoreCase(_sheetName,
rpxg.getSheetName())) {
// only move 3D refs that refer to the sheet with cells being
moved
return null;
}
return isRowMove ? rowMoveRefPtg(rpxg) : columnMoveRefPtg(rpxg);
}
- if(ptg instanceof Area2DPtgBase) {
+ if(ptg instanceof Area2DPtgBase aptg) {
if (currentExternSheetIx != _externSheetIndex) {
// local refs on other sheets are unaffected
return ptg;
}
- Area2DPtgBase aptg = (Area2DPtgBase) ptg;
return isRowMove ? rowMoveAreaPtg(aptg) : columnMoveAreaPtg(aptg);
}
- if(ptg instanceof Area3DPtg) {
- Area3DPtg aptg = (Area3DPtg)ptg;
+ if(ptg instanceof Area3DPtg aptg) {
if (_externSheetIndex != aptg.getExternSheetIndex()) {
// only move 3D refs that refer to the sheet with cells being
moved
// (currentExternSheetIx is irrelevant)
@@ -231,10 +227,9 @@ public final class FormulaShifter {
}
return isRowMove ? rowMoveAreaPtg(aptg) : columnMoveAreaPtg(aptg);
}
- if(ptg instanceof Area3DPxg) {
- Area3DPxg apxg = (Area3DPxg)ptg;
+ if(ptg instanceof Area3DPxg apxg) {
if (apxg.getExternalWorkbookNumber() > 0 ||
- ! _sheetName.equalsIgnoreCase(apxg.getSheetName())) {
+ !StringUtil.equalsIgnoreCase(_sheetName,
apxg.getSheetName())) {
// only move 3D refs that refer to the sheet with cells being
moved
return null;
}
@@ -273,28 +268,22 @@ public final class FormulaShifter {
* If Ptg doesn't need to be changed, returns <code>null</code>
*/
private Ptg adjustPtgDueToCopy(Ptg ptg, boolean isRowCopy) {
- if(ptg instanceof RefPtg) {
- RefPtg rptg = (RefPtg)ptg;
+ if(ptg instanceof RefPtg rptg) {
return isRowCopy ? rowCopyRefPtg(rptg) : columnCopyRefPtg(rptg);
}
- if(ptg instanceof Ref3DPtg) {
- Ref3DPtg rptg = (Ref3DPtg)ptg;
+ if(ptg instanceof Ref3DPtg rptg) {
return isRowCopy ? rowCopyRefPtg(rptg) : columnCopyRefPtg(rptg);
}
- if(ptg instanceof Ref3DPxg) {
- Ref3DPxg rpxg = (Ref3DPxg)ptg;
+ if(ptg instanceof Ref3DPxg rpxg) {
return isRowCopy ? rowCopyRefPtg(rpxg) : columnCopyRefPtg(rpxg);
}
- if(ptg instanceof Area2DPtgBase) {
- Area2DPtgBase aptg = (Area2DPtgBase) ptg;
+ if(ptg instanceof Area2DPtgBase aptg) {
return isRowCopy ? rowCopyAreaPtg(aptg) : columnCopyAreaPtg(aptg);
}
- if(ptg instanceof Area3DPtg) {
- Area3DPtg aptg = (Area3DPtg)ptg;
+ if(ptg instanceof Area3DPtg aptg) {
return isRowCopy ? rowCopyAreaPtg(aptg) : columnCopyAreaPtg(aptg);
}
- if(ptg instanceof Area3DPxg) {
- Area3DPxg apxg = (Area3DPxg)ptg;
+ if(ptg instanceof Area3DPxg apxg) {
return isRowCopy ? rowCopyAreaPtg(apxg) : columnCopyAreaPtg(apxg);
}
return null;
@@ -332,8 +321,7 @@ public final class FormulaShifter {
private Ptg adjustPtgDueToSheetMove(Ptg ptg) {
- if(ptg instanceof Ref3DPtg) {
- Ref3DPtg ref = (Ref3DPtg)ptg;
+ if(ptg instanceof Ref3DPtg ref) {
int oldSheetIndex = ref.getExternSheetIndex();
// we have to handle a few cases here
@@ -811,23 +799,19 @@ public final class FormulaShifter {
if (ptg instanceof RefPtg) {
return new RefErrorPtg();
}
- if (ptg instanceof Ref3DPtg) {
- Ref3DPtg rptg = (Ref3DPtg) ptg;
+ if (ptg instanceof Ref3DPtg rptg) {
return new DeletedRef3DPtg(rptg.getExternSheetIndex());
}
if (ptg instanceof AreaPtg) {
return new AreaErrPtg();
}
- if (ptg instanceof Area3DPtg) {
- Area3DPtg area3DPtg = (Area3DPtg) ptg;
+ if (ptg instanceof Area3DPtg area3DPtg) {
return new DeletedArea3DPtg(area3DPtg.getExternSheetIndex());
}
- if (ptg instanceof Ref3DPxg) {
- Ref3DPxg pxg = (Ref3DPxg)ptg;
+ if (ptg instanceof Ref3DPxg pxg) {
return new Deleted3DPxg(pxg.getExternalWorkbookNumber(),
pxg.getSheetName());
}
- if (ptg instanceof Area3DPxg) {
- Area3DPxg pxg = (Area3DPxg)ptg;
+ if (ptg instanceof Area3DPxg pxg) {
return new Deleted3DPxg(pxg.getExternalWorkbookNumber(),
pxg.getSheetName());
}
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java
b/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java
index c6b5e82a95..09a3d39e8e 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java
@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.util.StringUtil;
/**
* Formats sheet names for use in formula expressions.
@@ -173,13 +174,11 @@ public final class SheetNameFormatter {
}
private static boolean nameLooksLikeBooleanLiteral(String rawSheetName) {
- switch(rawSheetName.charAt(0)) {
- case 'T': case 't':
- return "TRUE".equalsIgnoreCase(rawSheetName);
- case 'F': case 'f':
- return "FALSE".equalsIgnoreCase(rawSheetName);
- }
- return false;
+ return switch (rawSheetName.charAt(0)) {
+ case 'T', 't' -> StringUtil.equalsIgnoreCase("TRUE", rawSheetName);
+ case 'F', 'f' -> StringUtil.equalsIgnoreCase("FALSE",
rawSheetName);
+ default -> false;
+ };
}
/**
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java
b/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java
index cb87e0aad4..70e3f1375f 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java
@@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.LazyRefEval;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.MathUtil;
+import org.apache.poi.util.StringUtil;
import java.time.DateTimeException;
import java.util.regex.Pattern;
@@ -256,12 +257,12 @@ public final class OperandResolver {
if (ev == BlankEval.instance) {
return 0.0;
}
- if (ev instanceof NumericValueEval) {
+ if (ev instanceof NumericValueEval num) {
// this also handles booleans
- return ((NumericValueEval)ev).getNumberValue();
+ return num.getNumberValue();
}
- if (ev instanceof StringEval) {
- String sval = ((StringEval) ev).getStringValue();
+ if (ev instanceof StringEval sve) {
+ String sval = sve.getStringValue();
Double dd = parseDouble(sval);
if(dd == null) dd = parseDateTime(sval);
if (dd == null) {
@@ -269,8 +270,7 @@ public final class OperandResolver {
}
return dd;
}
- if (ev instanceof LazyRefEval) {
- final LazyRefEval lre = (LazyRefEval) ev;
+ if (ev instanceof LazyRefEval lre) {
final ValueEval innerValueEval =
lre.getInnerValueEvalForFirstSheet();
if (innerValueEval == ev) {
throw new IllegalStateException("Circular lazy reference " +
lre);
@@ -328,8 +328,7 @@ public final class OperandResolver {
* @return the converted string value. never {@code null}
*/
public static String coerceValueToString(ValueEval ve) {
- if (ve instanceof StringValueEval) {
- StringValueEval sve = (StringValueEval) ve;
+ if (ve instanceof StringValueEval sve) {
return sve.getStringValue();
}
if (ve == BlankEval.instance) {
@@ -348,35 +347,34 @@ public final class OperandResolver {
// TODO - remove 've == null' condition once AreaEval is fixed
return null;
}
- if (ve instanceof BoolEval) {
- return ((BoolEval) ve).getBooleanValue();
+ if (ve instanceof BoolEval be) {
+ return be.getBooleanValue();
}
- if (ve instanceof StringEval) {
+ if (ve instanceof StringEval se) {
if (stringsAreBlanks) {
return null;
}
- String str = ((StringEval) ve).getStringValue();
- if (str.equalsIgnoreCase("true")) {
+ String str = se.getStringValue();
+ if (StringUtil.equalsIgnoreCase(str, "true")) {
return Boolean.TRUE;
}
- if (str.equalsIgnoreCase("false")) {
+ if (StringUtil.equalsIgnoreCase(str, "false")) {
return Boolean.FALSE;
}
// else - string cannot be converted to boolean
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
- if (ve instanceof NumericValueEval) {
- NumericValueEval ne = (NumericValueEval) ve;
+ if (ve instanceof NumericValueEval ne) {
double d = ne.getNumberValue();
if (Double.isNaN(d)) {
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
return d != 0;
}
- if (ve instanceof ErrorEval) {
- throw new EvaluationException((ErrorEval) ve);
+ if (ve instanceof ErrorEval ee) {
+ throw new EvaluationException(ee);
}
throw new IllegalStateException("Unexpected eval (" +
ve.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 ecb57e5e1a..ce4d72a096 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
@@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate;
import org.apache.poi.ss.usermodel.FormulaError;
+import org.apache.poi.util.StringUtil;
/**
* Implementation for the function COUNTIF
@@ -87,54 +88,50 @@ public final class Countif extends Fixed2ArgFunction {
char firstChar = value.charAt(0);
- switch(firstChar) {
- case '=':
- return OP_EQ;
- case '>':
+ return switch (firstChar) {
+ case '=' -> OP_EQ;
+ case '>' -> {
if (len > 1) {
- switch(value.charAt(1)) {
+ switch (value.charAt(1)) {
case '=':
- return OP_GE;
+ yield OP_GE;
}
}
- return OP_GT;
- case '<':
+ yield OP_GT;
+ }
+ case '<' -> {
if (len > 1) {
- switch(value.charAt(1)) {
+ switch (value.charAt(1)) {
case '=':
- return OP_LE;
+ yield OP_LE;
case '>':
- return OP_NE;
+ yield OP_NE;
}
}
- return OP_LT;
- }
- return OP_NONE;
+ yield OP_LT;
+ }
+ default -> OP_NONE;
+ };
}
public boolean evaluate(boolean cmpResult) {
- switch (_code) {
- case NONE:
- case EQ:
- return cmpResult;
- case NE:
- return !cmpResult;
- }
- throw new IllegalStateException("Cannot call boolean evaluate on
non-equality operator '"
- + _representation + "'");
+ return switch (_code) {
+ case NONE, EQ -> cmpResult;
+ case NE -> !cmpResult;
+ default -> throw new IllegalStateException("Cannot call
boolean evaluate on non-equality operator '"
+ + _representation + "'");
+ };
}
public boolean evaluate(int cmpResult) {
- switch (_code) {
- case NONE:
- case EQ:
- return cmpResult == 0;
- case NE: return cmpResult != 0;
- case LT: return cmpResult < 0;
- case LE: return cmpResult <= 0;
- case GT: return cmpResult > 0;
- case GE: return cmpResult >= 0;
- }
- throw new IllegalStateException("Cannot call boolean evaluate on
non-equality operator '"
- + _representation + "'");
+ return switch (_code) {
+ case NONE, EQ -> cmpResult == 0;
+ case NE -> cmpResult != 0;
+ case LT -> cmpResult < 0;
+ case LE -> cmpResult <= 0;
+ case GT -> cmpResult > 0;
+ case GE -> cmpResult >= 0;
+ default -> throw new IllegalStateException("Cannot call
boolean evaluate on non-equality operator '"
+ + _representation + "'");
+ };
}
@Override
public String toString() {
@@ -183,7 +180,7 @@ public final class Countif extends Fixed2ArgFunction {
@Override
public boolean matches(ValueEval x) {
double testValue;
- if(x instanceof StringEval) {
+ if(x instanceof StringEval se) {
// if the target(x) is a string, but parses as a number
// it may still count as a match, only for the equality
operator
switch (getCode()) {
@@ -199,24 +196,16 @@ public final class Countif extends Fixed2ArgFunction {
// for example '>5' does not match '6',
return false;
}
- StringEval se = (StringEval)x;
Double val = OperandResolver.parseDouble(se.getStringValue());
if(val == null) {
// x is text that is not a number
return false;
}
return _value == val;
- } else if((x instanceof NumberEval)) {
- NumberEval ne = (NumberEval) x;
+ } else if(x instanceof NumberEval ne) {
testValue = ne.getNumberValue();
- } else if((x instanceof BlankEval)) {
- switch (getCode()) {
- case CmpOp.NE:
- // Excel counts blank values in range as not equal to
any value. See Bugzilla 51498
- return true;
- default:
- return false;
- }
+ } else if(x instanceof BlankEval) {
+ return getCode() == CmpOp.NE; // Excel counts blank values in
range as not equal to any value. See Bugzilla 51498
} else {
return false;
}
@@ -255,25 +244,12 @@ public final class Countif extends Fixed2ArgFunction {
// return false;
// }
// testValue = boolToInt(val.booleanValue());
- } else if((x instanceof BoolEval)) {
- BoolEval be = (BoolEval) x;
+ } else if(x instanceof BoolEval be) {
testValue = boolToInt(be.getBooleanValue());
- } else if((x instanceof BlankEval)) {
- switch (getCode()) {
- case CmpOp.NE:
- // Excel counts blank values in range as not equal to
any value. See Bugzilla 51498
- return true;
- default:
- return false;
- }
- } else if((x instanceof NumberEval)) {
- switch (getCode()) {
- case CmpOp.NE:
- // not-equals comparison of a number to boolean always
returnes false
- return true;
- default:
- return false;
- }
+ } else if(x instanceof BlankEval) {
+ return getCode() == CmpOp.NE; // Excel counts blank values in
range as not equal to any value. See Bugzilla 51498
+ } else if(x instanceof NumberEval) {
+ return getCode() == CmpOp.NE;// not-equals comparison of a
number to boolean always returns false
} else {
return false;
}
@@ -295,8 +271,8 @@ public final class Countif extends Fixed2ArgFunction {
@Override
public boolean matches(ValueEval x) {
- if(x instanceof ErrorEval) {
- int testValue = ((ErrorEval)x).getErrorCode();
+ if(x instanceof ErrorEval errorEval) {
+ int testValue = errorEval.getErrorCode();
return evaluate(testValue - _value);
}
return false;
@@ -336,17 +312,16 @@ public final class Countif extends Fixed2ArgFunction {
@Override
public boolean matches(ValueEval x) {
if (x instanceof BlankEval) {
- switch(getCode()) {
- case CmpOp.NONE:
- case CmpOp.EQ:
- return _value.isEmpty();
- case CmpOp.NE:
+ return switch (getCode()) {
+ case CmpOp.NONE, CmpOp.EQ -> _value.isEmpty();
+ case CmpOp.NE ->
// pred '<>' matches empty string but not blank cell
// pred '<>ABC' matches blank and 'not ABC'
- return _value.length() != 0;
- }
- // no other criteria matches a blank cell
- return false;
+ !_value.isEmpty();
+ default ->
+ // no other criteria matches a blank cell
+ false;
+ };
}
if(!(x instanceof StringEval)) {
// must always be string
@@ -355,15 +330,15 @@ public final class Countif extends Fixed2ArgFunction {
return false;
}
String testedValue = ((StringEval) x).getStringValue();
- if (testedValue.length() < 1 && _value.length() < 1) {
+ if (testedValue.isEmpty() && _value.isEmpty()) {
// odd case: criteria '=' behaves differently to criteria ''
- switch(getCode()) {
- case CmpOp.NONE: return true;
- case CmpOp.EQ: return false;
- case CmpOp.NE: return true;
- }
- return false;
+ return switch (getCode()) {
+ case CmpOp.NONE -> true;
+ case CmpOp.EQ -> false;
+ case CmpOp.NE -> true;
+ default -> false;
+ };
}
if (_pattern != null) {
return evaluate(_pattern.matcher(testedValue).matches());
@@ -550,19 +525,19 @@ public final class Countif extends Fixed2ArgFunction {
* @return {@code null} to represent blank values
*/
/* package */ static Boolean parseBoolean(String strRep) {
- if (strRep.length() < 1) {
+ if (strRep.isEmpty()) {
return null;
}
switch(strRep.charAt(0)) {
case 't':
case 'T':
- if("TRUE".equalsIgnoreCase(strRep)) {
+ if(StringUtil.equalsIgnoreCase("TRUE", strRep)) {
return Boolean.TRUE;
}
break;
case 'f':
case 'F':
- if("FALSE".equalsIgnoreCase(strRep)) {
+ if(StringUtil.equalsIgnoreCase("FALSE", strRep)) {
return Boolean.FALSE;
}
break;
diff --git
a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java
b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java
index 70e1aa3927..c5669a801e 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java
@@ -34,6 +34,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.util.NumberComparer;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.StringUtil;
/**
* This class performs a D* calculation. It takes an {@link IDStarAlgorithm}
object and
@@ -121,9 +122,9 @@ public final class DStarRunner implements Function3Arg {
int fc = -1;
try {
filterColumn = OperandResolver.getSingleValue(filterColumn,
srcRowIndex, srcColumnIndex);
- if (filterColumn instanceof NumericValueEval) {
+ if (filterColumn instanceof NumericValueEval num) {
//fc is zero based while Excel uses 1 based column numbering
- fc =
Math.toIntExact(Math.round(((NumericValueEval)filterColumn).getNumberValue()))
- 1;
+ fc = Math.toIntExact(Math.round(num.getNumberValue())) - 1;
} else {
fc = getColumnForName(filterColumn, db);
}
@@ -222,7 +223,7 @@ public final class DStarRunner implements Function3Arg {
continue;
}
String columnName =
OperandResolver.coerceValueToString(columnNameValueEval);
- if(name.equalsIgnoreCase(columnName)) {
+ if(StringUtil.equalsIgnoreCase(name, columnName)) {
resultColumn = column;
break;
}
@@ -432,21 +433,19 @@ public final class DStarRunner implements Function3Arg {
ValueEval valueEval, operator op, String condition) {
String valueString = valueEval instanceof BlankEval ? "" :
OperandResolver.coerceValueToString(valueEval);
- switch(op) {
- case equal:
- return valueString.equalsIgnoreCase(condition);
- case notEqual:
- return !valueString.equalsIgnoreCase(condition);
- }
- return false; // Can not be reached.
+ return switch (op) {
+ case equal -> StringUtil.equalsIgnoreCase(valueString, condition);
+ case notEqual -> !StringUtil.equalsIgnoreCase(valueString,
condition);
+ default -> false;
+ };
}
private static Double getNumberFromValueEval(ValueEval value) {
- if(value instanceof NumericValueEval) {
- return ((NumericValueEval)value).getNumberValue();
+ if(value instanceof NumericValueEval num) {
+ return num.getNumberValue();
}
- else if(value instanceof StringValueEval) {
- String stringValue = ((StringValueEval)value).getStringValue();
+ else if(value instanceof StringValueEval sve) {
+ String stringValue = sve.getStringValue();
try {
return Double.parseDouble(stringValue);
} catch (NumberFormatException e2) {
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 3b3be9e1ff..e0f828abc0 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
@@ -22,6 +22,7 @@ import static
org.apache.poi.ss.formula.eval.ErrorEval.VALUE_INVALID;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.MathUtil;
+import org.apache.poi.util.StringUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -107,7 +108,7 @@ public abstract class NumericFunction implements Function {
DecimalFormat nf = (DecimalFormat)
NumberFormat.getCurrencyInstance(LocaleUtil.getUserLocale());
int decimalPlaces = Math.max(nPlaces, 0);
- if
(LocaleUtil.getUserLocale().getCountry().equalsIgnoreCase("US")) {
+ if
(StringUtil.equalsIgnoreCase(LocaleUtil.getUserLocale().getCountry(), "US")) {
// Java 23 removed "COMPAT" locale provider and thus
// we need to ensure that the dollar-sign is used and not
"USD" as Java 23 and newer
// would do
diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
b/poi/src/main/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
index 76c5adbbeb..f4ea414162 100644
--- a/poi/src/main/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
+++ b/poi/src/main/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
@@ -16,6 +16,8 @@
==================================================================== */
package org.apache.poi.ss.usermodel;
+import org.apache.poi.util.StringUtil;
+
/**
* Utility to identify built-in formats. The following is a list of the
formats as
* returned by this class.
@@ -151,7 +153,7 @@ public final class BuiltinFormats {
* @return index of format or -1 if undefined.
*/
public static int getBuiltinFormat(String pFmt) {
- String fmt = "TEXT".equalsIgnoreCase(pFmt) ? "@" : pFmt;
+ String fmt = StringUtil.equalsIgnoreCase("TEXT", pFmt) ? "@" : pFmt;
int i = -1;
for (String f : _formats) {
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 21c99484c5..8a7628b5a7 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
@@ -424,7 +424,7 @@ public class DataFormatter {
}
// Is it one of the special built in types, General or @?
- if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
+ if (StringUtil.equalsIgnoreCase("General", formatStr) ||
"@".equals(formatStr)) {
return generalNumberFormat;
}
@@ -493,7 +493,7 @@ public class DataFormatter {
return getDefaultFormat(cellValue);
}
- if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
+ if (StringUtil.equalsIgnoreCase("General", formatStr) ||
"@".equals(formatStr)) {
return generalNumberFormat;
}
diff --git a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
index fcc028be10..95260e8876 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
@@ -41,6 +41,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
+import org.apache.poi.util.StringUtil;
/**
* Helper methods for when working with Usermodel sheets
@@ -578,7 +579,7 @@ public class SheetUtil {
private static boolean initIgnoreMissingFontSystemFlag() {
final String flag =
System.getProperty("org.apache.poi.ss.ignoreMissingFontSystem");
if (flag != null) {
- return !flag.trim().equalsIgnoreCase("false");
+ return !StringUtil.equalsIgnoreCase(flag.trim(), "false");
}
return true;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]