From 5d376c8696399b58ec1a4625a84110c39c818f64 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Tue, 21 May 2019 00:13:56 +0000 Subject: [PATCH] fix boxed variable is never null git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1859593 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFRow.java | 106 ++++++++++-------- .../apache/poi/ss/formula/functions/Rank.java | 97 +++++++++------- src/java/org/apache/poi/util/StringUtil.java | 28 +++-- .../apache/poi/hslf/dev/SlideIdListing.java | 14 +-- 4 files changed, 140 insertions(+), 105 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index c9b3016dfa..d06278e1dc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -85,12 +85,12 @@ public final class HSSFRow implements Row, Comparable { this.sheet = sheet; row = record; setRowNum(record.getRowNumber()); - + // Size the initial cell list such that a read only case won't waste // lots of memory, and a create/read followed by adding new cells can // add a bit without needing a resize cells = new HSSFCell[record.getLastCol()+INITIAL_CAPACITY]; - + // Don't trust colIx boundaries as read by other apps // set the RowRecord empty for the moment record.setEmpty(); @@ -119,7 +119,7 @@ public final class HSSFRow implements Row, Comparable { * Use this to create new cells within the row and return it. *

* The cell that is returned will be of the requested type. - * The type can be changed either through calling setCellValue + * The type can be changed either through calling setCellValue * or setCellType, but there is a small overhead to doing this, * so it is best to create the required type up front. * @@ -429,7 +429,9 @@ public final class HSSFRow implements Row, Comparable { { int count = 0; for (HSSFCell cell : cells) { - if (cell != null) count++; + if (cell != null) { + count++; + } } return count; } @@ -499,8 +501,11 @@ public final class HSSFRow implements Row, Comparable { //The low-order 15 bits contain the row height. //The 0x8000 bit indicates that the row is standard height (optional) - if ((height & 0x8000) != 0) height = sheet.getSheet().getDefaultRowHeight(); - else height &= 0x7FFF; + if ((height & 0x8000) != 0) { + height = sheet.getSheet().getDefaultRowHeight(); + } else { + height &= 0x7FFF; + } return height; } @@ -627,45 +632,46 @@ public final class HSSFRow implements Row, Comparable { int thisId=-1; int nextId=-1; - public CellIterator() - { - findNext(); + public CellIterator() { + findNext(); } @Override - public boolean hasNext() { - return nextIdHSSFRow objects. Two rows are equal if they belong to the same worksheet and * their row indexes are equal. @@ -688,22 +694,19 @@ public final class HSSFRow implements Row, Comparable { * @throws IllegalArgumentException if the argument row belongs to a different worksheet */ @Override - public int compareTo(HSSFRow other) - { + public int compareTo(HSSFRow other) { if (this.getSheet() != other.getSheet()) { throw new IllegalArgumentException("The compared rows must belong to the same sheet"); } - Integer thisRow = this.getRowNum(); - Integer otherRow = other.getRowNum(); - return thisRow.compareTo(otherRow); + int thisRow = this.getRowNum(); + int otherRow = other.getRowNum(); + return Integer.compare(thisRow, otherRow); } @Override - public boolean equals(Object obj) - { - if (!(obj instanceof HSSFRow)) - { + public boolean equals(Object obj) { + if (!(obj instanceof HSSFRow)) { return false; } HSSFRow other = (HSSFRow) obj; @@ -716,7 +719,7 @@ public final class HSSFRow implements Row, Comparable { public int hashCode() { return row.hashCode(); } - + /** * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the right. * @param firstShiftColumnIndex the column to start shifting @@ -727,20 +730,25 @@ public final class HSSFRow implements Row, Comparable { public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { RowShifter.validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); - if(lastShiftColumnIndex + step + 1> cells.length) + if (lastShiftColumnIndex + step + 1 > cells.length) { extend(lastShiftColumnIndex + step + 1); - for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting + } + + for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting HSSFCell cell = getCell(columnIndex); cells[columnIndex+step] = null; - if(cell != null) + if (cell != null) { moveCell(cell, (short)(columnIndex+step)); + } } - for (int columnIndex = firstShiftColumnIndex; columnIndex <= firstShiftColumnIndex+step-1; columnIndex++) + for (int columnIndex = firstShiftColumnIndex; columnIndex <= firstShiftColumnIndex+step-1; columnIndex++) { cells[columnIndex] = null; + } } - private void extend(int newLenght){ + + private void extend(int newLength) { HSSFCell[] temp = cells.clone(); - cells = new HSSFCell[newLenght]; + cells = new HSSFCell[newLength]; System.arraycopy(temp, 0, cells, 0, temp.length); } @@ -754,15 +762,17 @@ public final class HSSFRow implements Row, Comparable { public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { RowShifter.validateShiftLeftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); - for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ + for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ HSSFCell cell = getCell(columnIndex); if(cell != null){ cells[columnIndex-step] = null; moveCell(cell, (short)(columnIndex-step)); + } else { + cells[columnIndex-step] = null; } - else cells[columnIndex-step] = null; } - for (int columnIndex = lastShiftColumnIndex-step+1; columnIndex <= lastShiftColumnIndex; columnIndex++) + for (int columnIndex = lastShiftColumnIndex-step+1; columnIndex <= lastShiftColumnIndex; columnIndex++) { cells[columnIndex] = null; + } } } diff --git a/src/java/org/apache/poi/ss/formula/functions/Rank.java b/src/java/org/apache/poi/ss/formula/functions/Rank.java index c5e07506be..225250eccc 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Rank.java +++ b/src/java/org/apache/poi/ss/formula/functions/Rank.java @@ -19,25 +19,33 @@ package org.apache.poi.ss.formula.functions; -import org.apache.poi.ss.formula.eval.*; +import org.apache.poi.ss.formula.eval.AreaEval; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.EvaluationException; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.RefEval; +import org.apache.poi.ss.formula.eval.RefListEval; +import org.apache.poi.ss.formula.eval.ValueEval; /** * Returns the rank of a number in a list of numbers. The rank of a number is its size relative to other values in a list. * Syntax: - * RANK(number,ref,order) - * Number is the number whose rank you want to find. - * Ref is an array of, or a reference to, a list of numbers. Nonnumeric values in ref are ignored. - * Order is a number specifying how to rank number. + * RANK(number,ref,order) + * Number is the number whose rank you want to find. + * Ref is an array of, or a reference to, a list of numbers. Nonnumeric values in ref are ignored. + * Order is a number specifying how to rank number. * If order is 0 (zero) or omitted, Microsoft Excel ranks number as if ref were a list sorted in descending order. * If order is any nonzero value, Microsoft Excel ranks number as if ref were a list sorted in ascending order. - * + * * @author Rubin Wang */ public class Rank extends Var2or3ArgFunction { + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { try { ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); @@ -46,9 +54,9 @@ public class Rank extends Var2or3ArgFunction { throw new EvaluationException(ErrorEval.NUM_ERROR); } - if(arg1 instanceof RefListEval) { - return eval(result, ((RefListEval)arg1), true); - } + if (arg1 instanceof RefListEval) { + return eval(result, ((RefListEval)arg1), true); + } final AreaEval aeRange = convertRangeArg(arg1); @@ -58,6 +66,7 @@ public class Rank extends Var2or3ArgFunction { } } + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) { try { ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); @@ -66,22 +75,22 @@ public class Rank extends Var2or3ArgFunction { throw new EvaluationException(ErrorEval.NUM_ERROR); } - ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex); - int order_value = OperandResolver.coerceValueToInt(ve); - final boolean order; - if(order_value==0) { - order = true; - } else if(order_value==1) { - order = false; - } else { - throw new EvaluationException(ErrorEval.NUM_ERROR); - } - - if(arg1 instanceof RefListEval) { - return eval(result, ((RefListEval)arg1), order); - } - - final AreaEval aeRange = convertRangeArg(arg1); + ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex); + int order_value = OperandResolver.coerceValueToInt(ve); + final boolean order; + if (order_value==0) { + order = true; + } else if(order_value==1) { + order = false; + } else { + throw new EvaluationException(ErrorEval.NUM_ERROR); + } + + if (arg1 instanceof RefListEval) { + return eval(result, ((RefListEval)arg1), order); + } + + final AreaEval aeRange = convertRangeArg(arg1); return eval(result, aeRange, order); } catch (EvaluationException e) { return e.getErrorEval(); @@ -94,10 +103,12 @@ public class Rank extends Var2or3ArgFunction { int width= aeRange.getWidth(); for (int r=0; rarg0 || !descending_order && valuearg0 || !descending_order && valuearg0 || !descending_order && valuearg0 || !descending_order && value 0xFF) { return true; @@ -331,10 +332,12 @@ public class StringUtil { } } + @Override public boolean hasNext() { return position < strings.length; } + @Override public String next() { int ourPos = position++; if (ourPos >= strings.length) { @@ -343,6 +346,7 @@ public class StringUtil { return strings[ourPos]; } + @Override public void remove() { } } @@ -374,13 +378,15 @@ public class StringUtil { * @see Symbol font - Unicode alternatives for Greek and special characters in HTML */ public static String mapMsCodepointString(String string) { - if (string == null || string.isEmpty()) return string; + if (string == null || string.isEmpty()) { + return string; + } initMsCodepointMap(); StringBuilder sb = new StringBuilder(); final int length = string.length(); for (int offset = 0; offset < length; ) { - Integer msCodepoint = string.codePointAt(offset); + int msCodepoint = string.codePointAt(offset); Integer uniCodepoint = msCodepointToUnicode.get(msCodepoint); sb.appendCodePoint(uniCodepoint == null ? msCodepoint : uniCodepoint); offset += Character.charCount(msCodepoint); @@ -395,7 +401,9 @@ public class StringUtil { } private static synchronized void initMsCodepointMap() { - if (msCodepointToUnicode != null) return; + if (msCodepointToUnicode != null) { + return; + } msCodepointToUnicode = new HashMap<>(); int i = 0xF020; for (int ch : symbolMap_f020) { @@ -609,7 +617,9 @@ public class StringUtil { // Could be replaced with org.apache.commons.lang3.StringUtils#join @Internal public static String join(Object[] array, String separator) { - if (array == null || array.length == 0) return ""; + if (array == null || array.length == 0) { + return ""; + } StringBuilder sb = new StringBuilder(); sb.append(array[0]); for (int i = 1; i < array.length; i++) { @@ -620,7 +630,9 @@ public class StringUtil { @Internal public static String join(Object[] array) { - if (array == null) return ""; + if (array == null) { + return ""; + } StringBuilder sb = new StringBuilder(); for (Object o : array) { sb.append(o); @@ -642,7 +654,9 @@ public class StringUtil { * @return the number of occurrences, 0 if the CharSequence is null */ public static int countMatches(CharSequence haystack, char needle) { - if (haystack == null) return 0; + if (haystack == null) { + return 0; + } int count = 0; final int length = haystack.length(); for (int i = 0; i < length; i++) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java index 10578f8a69..30a02151c6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java @@ -44,7 +44,7 @@ public final class SlideIdListing { private static byte[] fileContents; public static void main(String[] args) throws IOException { - if(args.length < 1) { + if (args.length < 1) { System.err.println("Need to give a filename"); System.exit(1); } @@ -88,8 +88,8 @@ public final class SlideIdListing { System.out.println(); // Look for latest core records that are slides or notes - for(int i=0; i