fix boxed variable is never null

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1859593 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alain Béarez 2019-05-21 00:13:56 +00:00
parent a59ed12ecf
commit 5d376c8696
4 changed files with 136 additions and 101 deletions
src
java/org/apache/poi
hssf/usermodel
ss/formula/functions
util
scratchpad/src/org/apache/poi/hslf/dev

View File

@ -85,12 +85,12 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
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<HSSFRow> {
* Use this to create new cells within the row and return it.
* <p>
* 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<HSSFRow> {
{
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<HSSFRow> {
//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<HSSFRow> {
int thisId=-1;
int nextId=-1;
public CellIterator()
{
findNext();
public CellIterator() {
findNext();
}
@Override
public boolean hasNext() {
return nextId<cells.length;
public boolean hasNext() {
return nextId < cells.length;
}
@Override
public Cell next() {
if (!hasNext())
public Cell next() {
if (!hasNext()) {
throw new NoSuchElementException("At last element");
HSSFCell cell=cells[nextId];
thisId=nextId;
findNext();
return cell;
}
HSSFCell cell = cells[nextId];
thisId = nextId;
findNext();
return cell;
}
@Override
public void remove() {
if (thisId == -1)
public void remove() {
if (thisId == -1) {
throw new IllegalStateException("remove() called before next()");
cells[thisId]=null;
}
cells[thisId] = null;
}
private void findNext()
{
int i=nextId+1;
for(;i<cells.length;i++)
{
if(cells[i]!=null) break;
}
nextId=i;
private void findNext() {
int i = nextId + 1;
for (; i < cells.length; i++) {
if (cells[i] != null) {
break;
}
}
nextId = i;
}
}
/**
* Compares two <code>HSSFRow</code> 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<HSSFRow> {
* @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<HSSFRow> {
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<HSSFRow> {
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<HSSFRow> {
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;
}
}
}

View File

@ -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);
}
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);
}
if (arg1 instanceof RefListEval) {
return eval(result, ((RefListEval)arg1), order);
}
final AreaEval aeRange = convertRangeArg(arg1);
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; r<height; r++) {
for (int c=0; c<width; c++) {
Double value = getValue(aeRange, r, c);
if(value==null)continue;
if(descending_order && value>arg0 || !descending_order && value<arg0){
if (value==null) {
continue;
}
if (descending_order && value>arg0 || !descending_order && value<arg0){
rank++;
}
}
@ -108,21 +119,21 @@ public class Rank extends Var2or3ArgFunction {
private static ValueEval eval(double arg0, RefListEval aeRange, boolean descending_order) {
int rank = 1;
for(ValueEval ve : aeRange.getList()) {
if (ve instanceof RefEval) {
ve = ((RefEval) ve).getInnerValueEval(((RefEval) ve).getFirstSheetIndex());
}
if (ve instanceof RefEval) {
ve = ((RefEval) ve).getInnerValueEval(((RefEval) ve).getFirstSheetIndex());
}
final Double value;
if (ve instanceof NumberEval) {
value = ((NumberEval)ve).getNumberValue();
} else {
continue;
}
final double value;
if (ve instanceof NumberEval) {
value = ((NumberEval)ve).getNumberValue();
} else {
continue;
}
if(descending_order && value>arg0 || !descending_order && value<arg0){
rank++;
}
}
if (descending_order && value>arg0 || !descending_order && value<arg0){
rank++;
}
}
return new NumberEval(rank);
}

View File

@ -282,8 +282,9 @@ public class StringUtil {
* @return boolean result true:string has at least one multibyte character
*/
public static boolean hasMultibyte(String value) {
if (value == null)
if (value == null) {
return false;
}
for (char c : value.toCharArray()) {
if (c > 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 <a href="http://www.alanwood.net/demos/symbol.html">Symbol font - Unicode alternatives for Greek and special characters in HTML</a>
*/
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++) {

View File

@ -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<latestRecords.length; i++) {
if(latestRecords[i] instanceof Slide) {
for (int i=0; i<latestRecords.length; i++) {
if (latestRecords[i] instanceof Slide) {
Slide s = (Slide)latestRecords[i];
SlideAtom sa = s.getSlideAtom();
System.out.println("Found the latest version of a slide record:");
@ -101,8 +101,8 @@ public final class SlideIdListing {
}
}
System.out.println();
for(int i=0; i<latestRecords.length; i++) {
if(latestRecords[i] instanceof Notes) {
for (int i=0; i<latestRecords.length; i++) {
if (latestRecords[i] instanceof Notes) {
Notes n = (Notes)latestRecords[i];
NotesAtom na = n.getNotesAtom();
System.out.println("Found the latest version of a notes record:");
@ -153,7 +153,7 @@ public final class SlideIdListing {
}
ss.close();
System.out.println();
}
@ -163,6 +163,6 @@ public final class SlideIdListing {
long type = LittleEndian.getUShort(fileContents, pos+2);
long rlen = LittleEndian.getUInt(fileContents, pos+4);
return Record.createRecordForType(type,fileContents,pos,(int)rlen+8);
return Record.createRecordForType(type,fileContents,pos,(int)rlen+8);
}
}