From: Dominik Stadler Date: Tue, 6 Apr 2021 06:10:19 +0000 (+0000) Subject: Apply some IDE suggestions, improve exception message, add some JavaDoc X-Git-Tag: REL_5_1_0~281 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=51b04fc690cbeb8859aca8ad049bb8aea94db0dd;p=poi.git Apply some IDE suggestions, improve exception message, add some JavaDoc git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888415 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/main/src/main/java/org/apache/poi/ss/formula/eval/AreaEval.java b/main/src/main/java/org/apache/poi/ss/formula/eval/AreaEval.java index c2ce411685..c2a5240fcc 100644 --- a/main/src/main/java/org/apache/poi/ss/formula/eval/AreaEval.java +++ b/main/src/main/java/org/apache/poi/ss/formula/eval/AreaEval.java @@ -59,26 +59,29 @@ public interface AreaEval extends TwoDEval, ThreeDEval { * returns true if the cell at row and col specified * as absolute indexes in the sheet is contained in * this area. - * @param row - * @param col + * @param row 0-based row index + * @param col 0-based column index */ boolean contains(int row, int col); /** * returns true if the specified col is in range - * @param col + * @param col 0-based column index */ boolean containsColumn(int col); /** * returns true if the specified row is in range - * @param row + * @param row 0-based row index */ boolean containsRow(int row); int getWidth(); int getHeight(); /** + * @param relativeRowIndex 0-based row index relative to this area + * @param relativeColumnIndex 0-based column index relative to this area + * * @return the ValueEval from within this area at the specified relativeRowIndex and * relativeColumnIndex. Never null (possibly {@link BlankEval}). The * specified indexes should relative to the top left corner of this area. diff --git a/main/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java b/main/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java index 59a383c3e1..88fe456d5c 100644 --- a/main/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java +++ b/main/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java @@ -43,7 +43,7 @@ public final class OperandResolver { "(\\."+Digits+"("+Exp+")?))"+ "[\\x00-\\x20]*"); private static final Pattern fpPattern = Pattern.compile(fpRegex); - + private OperandResolver() { // no instances of this class } @@ -75,7 +75,7 @@ public final class OperandResolver { } return result; } - + /** * Retrieves a single value from an area evaluation utilizing the 2D indices of the cell * within its own area reference to index the value in the area evaluation. @@ -105,7 +105,7 @@ public final class OperandResolver { else if (ae.isRow() && relativeColIndex < ae.getWidth()) { return ae.getRelativeValue(0, relativeColIndex); } - + return ErrorEval.NA; } @@ -212,7 +212,7 @@ public final class OperandResolver { } return ae.getAbsoluteValue(ae.getFirstRow(), srcCellCol); } - + private static ValueEval chooseSingleElementFromRef(RefEval ref) { return ref.getInnerValueEval( ref.getFirstSheetIndex() ); } @@ -251,7 +251,6 @@ public final class OperandResolver { * {@link StringEval}, {@link BoolEval} or {@link BlankEval} */ public static double coerceValueToDouble(ValueEval ev) throws EvaluationException { - if (ev == BlankEval.instance) { return 0.0; } @@ -266,7 +265,7 @@ public final class OperandResolver { if (dd == null) { throw EvaluationException.invalidValue(); } - return dd.doubleValue(); + return dd; } throw new RuntimeException("Unexpected arg eval type (" + ev.getClass().getName() + ")"); } @@ -274,8 +273,8 @@ public final class OperandResolver { /** * Converts a string to a double using standard rules that Excel would use.
* Tolerates leading and trailing spaces,

- * - * Doesn't support currency prefixes, commas, percentage signs or arithmetic operations strings. + * + * Doesn't support currency prefixes, commas, percentage signs or arithmetic operations strings. * * Some examples:
* " 123 " -> 123.0
@@ -301,7 +300,7 @@ public final class OperandResolver { else { return null; } - + } public static Double parseDateTime(String pText) { @@ -340,7 +339,7 @@ public final class OperandResolver { return null; } if (ve instanceof BoolEval) { - return Boolean.valueOf(((BoolEval) ve).getBooleanValue()); + return ((BoolEval) ve).getBooleanValue(); } if (ve instanceof StringEval) { @@ -364,7 +363,7 @@ public final class OperandResolver { if (Double.isNaN(d)) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } - return Boolean.valueOf(d != 0); + return d != 0; } if (ve instanceof ErrorEval) { throw new EvaluationException((ErrorEval) ve); diff --git a/main/src/main/java/org/apache/poi/ss/formula/functions/Baseifs.java b/main/src/main/java/org/apache/poi/ss/formula/functions/Baseifs.java index 88ce9e3f4c..3886e60fb1 100644 --- a/main/src/main/java/org/apache/poi/ss/formula/functions/Baseifs.java +++ b/main/src/main/java/org/apache/poi/ss/formula/functions/Baseifs.java @@ -30,7 +30,7 @@ import org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate; import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; /** - * Base class for SUMIFS() and COUNTIFS() functions, as they share much of the same logic, + * Base class for SUMIFS() and COUNTIFS() functions, as they share much of the same logic, * the difference being the source of the totals. */ /*package*/ abstract class Baseifs implements FreeRefFunction { @@ -40,11 +40,11 @@ import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; * @return true if there should be a range argument before the criteria pairs */ protected abstract boolean hasInitialRange(); - + public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { final boolean hasInitialRange = hasInitialRange(); final int firstCriteria = hasInitialRange ? 1 : 0; - + if( args.length < (2+firstCriteria) || args.length % 2 != firstCriteria ) { return ErrorEval.VALUE_INVALID; } @@ -54,13 +54,13 @@ import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; if (hasInitialRange) { sumRange = convertRangeArg(args[0]); } - + // collect pairs of ranges and criteria AreaEval[] ae = new AreaEval[(args.length - firstCriteria)/2]; I_MatchPredicate[] mp = new I_MatchPredicate[ae.length]; for(int i = firstCriteria, k=0; i < args.length; i += 2, k++){ ae[k] = convertRangeArg(args[i]); - + mp[k] = Countif.createCriteriaPredicate(args[i+1], ec.getRowIndex(), ec.getColumnIndex()); } @@ -84,13 +84,13 @@ import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; private static void validateCriteriaRanges(AreaEval sumRange, AreaEval[] criteriaRanges) throws EvaluationException { int h = criteriaRanges[0].getHeight(); int w = criteriaRanges[0].getWidth(); - - if (sumRange != null - && (sumRange.getHeight() != h + + if (sumRange != null + && (sumRange.getHeight() != h || sumRange.getWidth() != w) ) { throw EvaluationException.invalidValue(); } - + for(AreaEval r : criteriaRanges){ if(r.getHeight() != h || r.getWidth() != w ) { @@ -107,7 +107,7 @@ import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; */ private static void validateCriteria(I_MatchPredicate[] criteria) throws EvaluationException { for(I_MatchPredicate predicate : criteria) { - + // check for errors in predicate and return immediately using this error code if(predicate instanceof ErrorMatcher) { throw new EvaluationException(ErrorEval.valueOf(((ErrorMatcher)predicate).getValue())); @@ -154,14 +154,16 @@ import org.apache.poi.ss.formula.functions.Countif.ErrorMatcher; /** * For counts, this would return 1, for sums it returns a cell value or zero. * This is only called after all the criteria are confirmed true for the coordinates. + * * @param sumRange if used - * @param relRowIndex - * @param relColIndex + * @param relRowIndex 0-based row index relative to the sumRange area + * @param relColIndex 0-based column index relative to the sumRange area + * * @return the aggregate input value corresponding to the given range coordinates */ private static double accumulate(AreaEval sumRange, int relRowIndex, int relColIndex) { if (sumRange == null) return 1.0; // count - + ValueEval addend = sumRange.getRelativeValue(relRowIndex, relColIndex); if (addend instanceof NumberEval) { return ((NumberEval)addend).getNumberValue(); diff --git a/main/src/main/java/org/apache/poi/ss/formula/functions/Countif.java b/main/src/main/java/org/apache/poi/ss/formula/functions/Countif.java index 1f37d2e433..94e5594fa0 100644 --- a/main/src/main/java/org/apache/poi/ss/formula/functions/Countif.java +++ b/main/src/main/java/org/apache/poi/ss/formula/functions/Countif.java @@ -138,11 +138,9 @@ public final class Countif extends Fixed2ArgFunction { } @Override public String toString() { - StringBuilder sb = new StringBuilder(64); - sb.append(getClass().getName()); - sb.append(" [").append(_representation).append("]"); - return sb.toString(); + return getClass().getName() + " [" + _representation + "]"; } + public String getRepresentation() { return _representation; } @@ -208,7 +206,7 @@ public final class Countif extends Fixed2ArgFunction { // x is text that is not a number return false; } - return _value == val.doubleValue(); + return _value == val; } else if((x instanceof NumberEval)) { NumberEval ne = (NumberEval) x; testValue = ne.getNumberValue(); @@ -304,7 +302,7 @@ public final class Countif extends Fixed2ArgFunction { } return false; } - + public int getValue() { return _value; } @@ -505,12 +503,12 @@ public final class Countif extends Fixed2ArgFunction { Boolean booleanVal = parseBoolean(value); if(booleanVal != null) { - return new BooleanMatcher(booleanVal.booleanValue(), operator); + return new BooleanMatcher(booleanVal, operator); } Double doubleVal = OperandResolver.parseDouble(value); if(doubleVal != null) { - return new NumberMatcher(doubleVal.doubleValue(), operator); + return new NumberMatcher(doubleVal, operator); } ErrorEval ee = parseError(value); if (ee != null) { diff --git a/main/src/main/java/org/apache/poi/ss/formula/functions/Sumproduct.java b/main/src/main/java/org/apache/poi/ss/formula/functions/Sumproduct.java index aea1fdb8ad..f52b10fa8b 100644 --- a/main/src/main/java/org/apache/poi/ss/formula/functions/Sumproduct.java +++ b/main/src/main/java/org/apache/poi/ss/formula/functions/Sumproduct.java @@ -81,17 +81,13 @@ public final class Sumproduct implements Function { } catch (EvaluationException e) { return e.getErrorEval(); } - throw new RuntimeException("Invalid arg type for SUMPRODUCT: (" - + firstArg.getClass().getName() + ")"); + throw new RuntimeException("Invalid arg type for SUMPRODUCT: " + firstArg); } private static ValueEval evaluateSingleProduct(ValueEval[] evalArgs) throws EvaluationException { - int maxN = evalArgs.length; - double term = 1D; - for(int n=0; n */ - private TreeMap defaultContentType; + private final TreeMap defaultContentType; /** * Override content type tree. @@ -286,7 +286,7 @@ public abstract class ContentTypeManager { throw new IllegalArgumentException("contentType"); } - return (this.defaultContentType.containsValue(contentType) || + return (this.defaultContentType.containsValue(contentType) || (this.overrideContentType != null && this.overrideContentType.containsValue(contentType))); } @@ -353,9 +353,9 @@ public abstract class ContentTypeManager { */ if (this.container != null && this.container.getPart(partName) != null) { throw new OpenXML4JRuntimeException( - "Rule M2.4 exception : Part \'" + "Rule M2.4 exception : Part '" + partName - + "\' not found - this error should NEVER happen!\n" + + "' not found - this error should NEVER happen!\n" + "Check that your code is closing the open resources in the correct order prior to filing a bug report.\n" + "If you can provide the triggering file, then please raise a bug at https://bz.apache.org/bugzilla/enter_bug.cgi?product=POI and attach the file that triggers it, thanks!"); }