Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgiga authored and jazzido committed Sep 14, 2017
1 parent af0ce5e commit dd25f7b
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 174 deletions.
10 changes: 5 additions & 5 deletions src/main/java/technology/tabula/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@SuppressWarnings("serial")
public class Table extends Rectangle {

public static final Table EMPTY = new Table("");
public static final Table empty() { return new Table(""); }

private Table(String extractionMethod) {
this.extractionMethod = extractionMethod;
Expand All @@ -34,8 +34,8 @@ public Table(ExtractionAlgorithm extractionAlgorithm) {
public void add(RectangularTextContainer chunk, int row, int col) {
this.merge(chunk);

rowCount = Math.max(rowCount, row);
colCount = Math.max(colCount, col);
rowCount = Math.max(rowCount, row + 1);
colCount = Math.max(colCount, col + 1);

CellPosition cp = new CellPosition(row, col);

Expand All @@ -55,10 +55,10 @@ public List<List<RectangularTextContainer>> getRows() {

private List<List<RectangularTextContainer>> computeRows() {
List<List<RectangularTextContainer>> rows = new ArrayList<>();
for (int i = 0; i <= rowCount; i++) {
for (int i = 0; i < rowCount; i++) {
List<RectangularTextContainer> lastRow = new ArrayList<>();
rows.add(lastRow);
for (int j = 0; j <= colCount; j++) {
for (int j = 0; j < colCount; j++) {
RectangularTextContainer cell = cells.get(new CellPosition(i,j)); // JAVA_8 use getOrDefault()
lastRow.add(cell != null ? cell : TextChunk.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<Table> extract(Page page) {
List<TextElement> textElements = page.getText();

if (textElements.size() == 0) {
return Arrays.asList(new Table[] { Table.EMPTY });
return Arrays.asList(new Table[] { Table.empty() });
}

List<TextChunk> textChunks = this.verticalRulings == null ? TextElement.mergeWords(page.getText()) : TextElement.mergeWords(page.getText(), this.verticalRulings);
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/technology/tabula/TableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package technology.tabula;

import static org.junit.Assert.*;

import org.junit.Test;

public class TableTest {

@Test public void testEmpty() {
Table empty = Table.empty();

assertEquals(TextChunk.EMPTY, empty.getCell(0, 0));
assertEquals(TextChunk.EMPTY, empty.getCell(1, 1));

assertEquals(0, empty.getRowCount());
assertEquals(0, empty.getColCount());

assertEquals("", empty.getExtractionMethod());

assertEquals(0, empty.getTop(), 0);
assertEquals(0, empty.getRight(), 0);
assertEquals(0, empty.getBottom(), 0);
assertEquals(0, empty.getLeft(), 0);

assertEquals(0, empty.getArea(), 0);
}

@Test public void testRowColCounts() {
Table table = Table.empty();

assertEquals(0, table.getRowCount());
assertEquals(0, table.getColCount());

table.add(TextChunk.EMPTY, 0, 0);

assertEquals(1, table.getRowCount());
assertEquals(1, table.getColCount());

table.add(TextChunk.EMPTY, 9, 9);

assertEquals(10, table.getRowCount());
assertEquals(10, table.getColCount());
}

}
Loading

0 comments on commit dd25f7b

Please sign in to comment.