Skip to content

Commit df3653b

Browse files
W3D3jazzido
authored andcommitted
Extracted rounded comparator
1 parent 6923895 commit df3653b

1 file changed

Lines changed: 18 additions & 47 deletions

File tree

src/main/java/technology/tabula/extractors/SpreadsheetExtractionAlgorithm.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,28 @@ public class SpreadsheetExtractionAlgorithm implements ExtractionAlgorithm {
2727

2828
private static final float MAGIC_HEURISTIC_NUMBER = 0.65f;
2929

30-
private static final Comparator<Point2D> POINT_COMPARATOR = new Comparator<Point2D>() {
31-
@Override
32-
public int compare(Point2D arg0, Point2D arg1) {
33-
int rv = 0;
34-
float arg0X = Utils.round(arg0.getX(), 2);
35-
float arg0Y = Utils.round(arg0.getY(), 2);
36-
float arg1X = Utils.round(arg1.getX(), 2);
37-
float arg1Y = Utils.round(arg1.getY(), 2);
38-
39-
40-
if (arg0Y > arg1Y) {
41-
rv = 1;
42-
}
43-
else if (arg0Y < arg1Y) {
44-
rv = -1;
45-
}
46-
else if (arg0X > arg1X) {
47-
rv = 1;
48-
}
49-
else if (arg0X < arg1X) {
50-
rv = -1;
51-
}
52-
return rv;
30+
private static final Comparator<Point2D> Y_FIRST_POINT_COMPARATOR = (point1, point2) -> {
31+
int compareY = compareRounded(point1.getY(), point2.getY());
32+
if (compareY == 0) {
33+
return compareRounded(point1.getX(), point2.getX());
5334
}
35+
return compareY;
5436
};
5537

56-
private static final Comparator<Point2D> X_FIRST_POINT_COMPARATOR = new Comparator<Point2D>() {
57-
@Override
58-
public int compare(Point2D arg0, Point2D arg1) {
59-
int rv = 0;
60-
float arg0X = Utils.round(arg0.getX(), 2);
61-
float arg0Y = Utils.round(arg0.getY(), 2);
62-
float arg1X = Utils.round(arg1.getX(), 2);
63-
float arg1Y = Utils.round(arg1.getY(), 2);
64-
65-
if (arg0X > arg1X) {
66-
rv = 1;
67-
}
68-
else if (arg0X < arg1X) {
69-
rv = -1;
70-
}
71-
else if (arg0Y > arg1Y) {
72-
rv = 1;
73-
}
74-
else if (arg0Y < arg1Y) {
75-
rv = -1;
76-
}
77-
return rv;
38+
private static final Comparator<Point2D> X_FIRST_POINT_COMPARATOR = (point1, point2) -> {
39+
int compareX = compareRounded(point1.getX(), point2.getX());
40+
if (compareX == 0) {
41+
return compareRounded(point1.getY(), point2.getY());
7842
}
43+
return compareX;
7944
};
8045

46+
private static int compareRounded(double d1, double d2) {
47+
float d1Rounded = Utils.round(d1, 2);
48+
float d2Rounded = Utils.round(d2, 2);
49+
50+
return Float.compare(d1Rounded, d2Rounded);
51+
}
8152

8253
@Override
8354
public List<Table> extract(Page page) {
@@ -175,7 +146,7 @@ public static List<Cell> findCells(List<Ruling> horizontalRulingLines, List<Ruli
175146
List<Cell> cellsFound = new ArrayList<>();
176147
Map<Point2D, Ruling[]> intersectionPoints = Ruling.findIntersections(horizontalRulingLines, verticalRulingLines);
177148
List<Point2D> intersectionPointsList = new ArrayList<>(intersectionPoints.keySet());
178-
Collections.sort(intersectionPointsList, POINT_COMPARATOR);
149+
intersectionPointsList.sort(Y_FIRST_POINT_COMPARATOR);
179150
boolean doBreak = false;
180151

181152
for (int i = 0; i < intersectionPointsList.size(); i++) {
@@ -256,7 +227,7 @@ public static List<Rectangle> findSpreadsheetsFromCells(List<? extends Rectangle
256227
Collections.sort(pointsSortX, X_FIRST_POINT_COMPARATOR);
257228
// Y first sort
258229
List<Point2D> pointsSortY = new ArrayList<>(pointSet);
259-
Collections.sort(pointsSortY, POINT_COMPARATOR);
230+
Collections.sort(pointsSortY, Y_FIRST_POINT_COMPARATOR);
260231

261232
while (i < pointSet.size()) {
262233
float currY = (float) pointsSortY.get(i).getY();

0 commit comments

Comments
 (0)