Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Calendar;
import java.util.Date;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.fesod.sheet.enums.NumericCellTypeEnum;
Expand All @@ -55,7 +54,6 @@
*/
@Getter
@Setter
@EqualsAndHashCode
public class CsvCell extends CellBase {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.Iterator;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -45,11 +44,11 @@
*/
@Getter
@Setter
@EqualsAndHashCode
public class CsvRow implements Row {

/**
* cell list
* cell list, kept sorted by column index by createCell; getCell binary-searches it and CsvSheet#flushData
* writes it in order
*/
private final List<CsvCell> cellList;

Expand Down Expand Up @@ -82,15 +81,25 @@ public CsvRow(CsvWorkbook csvWorkbook, CsvSheet csvSheet, Integer rowIndex) {

@Override
public Cell createCell(int column) {
CsvCell cell = new CsvCell(csvWorkbook, csvSheet, this, column, null);
cellList.add(cell);
return cell;
return createCell(column, null);
}

@Override
public Cell createCell(int column, CellType type) {
CsvCell cell = new CsvCell(csvWorkbook, csvSheet, this, column, type);
cellList.add(cell);
// Keep the list sorted by column index and replace the cell already in the column, since
// CsvSheet#flushData writes the cells in list order
int size = cellList.size();
if (size == 0 || cellList.get(size - 1).getColumnIndex() < column) {
cellList.add(cell);
return cell;
}
int position = indexOf(column);
if (position >= 0) {
cellList.set(position, cell);
} else {
cellList.add(-position - 1, cell);
}
return cell;
}

Expand All @@ -111,10 +120,8 @@ public int getRowNum() {

@Override
public Cell getCell(int cellnum) {
if (cellnum < 0 || cellnum >= cellList.size()) {
return null;
}
return cellList.get(cellnum);
int position = indexOf(cellnum);
return position >= 0 ? cellList.get(position) : null;
}

@Override
Expand All @@ -135,7 +142,29 @@ public short getLastCellNum() {
if (CollectionUtils.isEmpty(cellList)) {
return -1;
}
return (short) cellList.size();
return (short) (cellList.get(cellList.size() - 1).getColumnIndex() + 1);
}

/**
* Binary search for the cell in the given column.
*
* @return the position of the cell, or {@code -(insertion point) - 1} if the column has no cell
*/
private int indexOf(int column) {
int low = 0;
int high = cellList.size() - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midColumn = cellList.get(mid).getColumnIndex();
if (midColumn < column) {
low = mid + 1;
} else if (midColumn > column) {
high = mid - 1;
} else {
return mid;
}
}
return -(low + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.csv.CSVFormat;
Expand Down Expand Up @@ -75,7 +74,6 @@
*/
@Getter
@Setter
@EqualsAndHashCode
public class CsvSheet implements Sheet, Closeable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.compress.utils.Lists;
Expand All @@ -58,7 +57,6 @@
*/
@Getter
@Setter
@EqualsAndHashCode
public class CsvWorkbook implements Workbook {
/**
* output
Expand Down
Loading
Loading