Bug 65312: Make RecordType.byName work for type "formula"

Also avoid NullPointerException when parsing unexpected record types
and check for bounds in byId()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896553 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2021-12-30 23:03:40 +00:00
parent 1d630a94fc
commit fd6ccbdf14
2 changed files with 37 additions and 2 deletions

View File

@ -19,7 +19,6 @@
package org.apache.poi.ss.usermodel;
/**
* The Threshold / CFVO / Conditional Formatting Value Object.
* <p>This defines how to calculate the ranges for a conditional
@ -28,17 +27,27 @@ package org.apache.poi.ss.usermodel;
*/
public interface ConditionalFormattingThreshold {
enum RangeType {
// IDs should match the values documented
// in the spec for HSSF, e.g. at
// https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/3cc68999-c2fc-4a57-92a5-94c0720779e9
/** Number / Parameter */
NUMBER(1, "num"),
/** The minimum value from the range */
MIN(2, "min"),
/** The maximum value from the range */
MAX(3, "max"),
/** Percent of the way from the mi to the max value in the range */
PERCENT(4, "percent"),
/** The minimum value of the cell that is in X percentile of the range */
PERCENTILE(5, "percentile"),
UNALLOCATED(6, null),
/** Formula result */
FORMULA(7, "formula");
@ -52,12 +61,23 @@ public interface ConditionalFormattingThreshold {
}
public static RangeType byId(int id) {
// id is mapped to ordinal()+1
if (id <= 0 || id > values().length) {
return null;
}
return values()[id-1]; // 1-based IDs
}
public static RangeType byName(String name) {
for (RangeType t : values()) {
if (t.name.equals(name)) return t;
if (t.name == null && name == null) {
return t;
} else if (t.name != null && t.name.equals(name)) {
return t;
}
}
return null;
}

View File

@ -1385,4 +1385,19 @@ public abstract class BaseTestConditionalFormatting {
}
}
}
@Test
void testRangeType() {
for (RangeType rangeType : RangeType.values()) {
assertEquals(rangeType, RangeType.byName(rangeType.name));
assertEquals(rangeType, RangeType.byId(rangeType.id));
}
assertEquals(RangeType.UNALLOCATED, RangeType.byName(null));
assertNull(RangeType.byName("some other name"));
assertNull(RangeType.byId(-1));
assertNull(RangeType.byId(0));
assertNull(RangeType.byId(99));
}
}