Skip to content

Commit 7a716f2

Browse files
authored
Fix, document, and test ColumnType.compare and Table.compareRows (#1266)
1 parent a1c9e2d commit 7a716f2

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

core/src/main/java/tech/tablesaw/api/ColumnType.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,11 @@ static ColumnType valueOf(String name) {
8282
/** TODO: Research this method to provide a good comment */
8383
AbstractColumnParser<?> customParser(ReadOptions options);
8484

85-
/** TODO: Research this method to provide a good comment */
86-
default boolean compare(int rowNumber, Column<?> temp, Column<?> original) {
87-
Object o1 = original.get(rowNumber);
88-
Object o2 = temp.get(temp.size() - 1);
89-
return o1 == null ? o2 == null : o1.equals(o2);
90-
}
91-
9285
/**
9386
* Returns true if the value at the specified index in column1 is equal to the value at the
9487
* specified index in column 2
88+
*
89+
* @throws {@code IndexOutOfBoundsException} if either index exceeds the corresponding column size
9590
*/
9691
default boolean compare(int col1Row, Column<?> col1, int col2Row, Column<?> col2) {
9792
Object o1 = col1.get(col1Row);

core/src/main/java/tech/tablesaw/api/Table.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,17 +529,22 @@ public void copyRowsToTable(int[] rows, Table newTable) {
529529
}
530530

531531
/**
532-
* Returns {@code true} if the row @rowNumber in table1 holds the same data as the row at
533-
* rowNumber in table2
532+
* Returns {@code true} if the row {@code rowNumber} in {@code table1} holds the same values than
533+
* the row at {@code rowNumber} in {@code table2}. Returns {@code false} if the number of columns
534+
* is different in the two tables.
535+
*
536+
* @throws {@code IndexOutOfBoundsException} if {@code rowNumber} exceeds either table number of
537+
* rows
534538
*/
535539
public static boolean compareRows(int rowNumber, Table table1, Table table2) {
536-
int columnCount = table1.columnCount();
537-
boolean result;
540+
final int columnCount = table1.columnCount();
541+
if (columnCount != table2.columnCount()) {
542+
return false;
543+
}
538544
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
539545
ColumnType columnType = table1.column(columnIndex).type();
540-
result =
541-
columnType.compare(rowNumber, table2.column(columnIndex), table1.column(columnIndex));
542-
if (!result) {
546+
if (!columnType.compare(
547+
rowNumber, table2.column(columnIndex), rowNumber, table1.column(columnIndex))) {
543548
return false;
544549
}
545550
}

core/src/test/java/tech/tablesaw/api/TableTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class TableTest {
5050
private static final ColumnType[] BUSH_COLUMN_TYPES = {LOCAL_DATE, INTEGER, STRING};
5151
private static Table bush;
5252
private static Table bushMinimized;
53+
private static Table missingValues;
5354

5455
private Table table;
5556
private final DoubleColumn f1 = DoubleColumn.create("f1");
@@ -64,6 +65,8 @@ static void readTables() {
6465
.columnTypes(BUSH_COLUMN_TYPES));
6566
ColumnType[] types = {LOCAL_DATE, SHORT, STRING};
6667
bushMinimized = Table.read().csv(CsvReadOptions.builder("../data/bush.csv").columnTypes(types));
68+
missingValues = Table.read().csv(CsvReadOptions.builder("../data/missing_values.csv")
69+
.missingValueIndicator("-"));
6770
}
6871

6972
@BeforeEach
@@ -916,4 +919,36 @@ public void testToStringColumnsWithVaryingSizes() {
916919
fail("toString shouldn't throw " + e);
917920
}
918921
}
922+
923+
@Test
924+
void testCompareRowsIdentical() {
925+
for(int i = 0; i < missingValues.rowCount(); i++) {
926+
assertTrue(Table.compareRows(i, missingValues, missingValues), "Row " + i + " is not equal to itself");
927+
}
928+
}
929+
930+
@Test
931+
void testCompareRowsDifferent() {
932+
Table differentTable = missingValues.copy().sortDescendingOn("Sales");
933+
for(int i = 0; i < missingValues.rowCount(); i++) {
934+
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a different row");
935+
}
936+
}
937+
938+
@Test
939+
void testCompareRowsDifferentColumns() {
940+
Table differentTable = missingValues.copy().removeColumns("Sales");
941+
for(int i = 0; i < missingValues.rowCount(); i++) {
942+
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a row with less columns");
943+
}
944+
}
945+
946+
@Test
947+
void testCompareRowsOutOfBound() {
948+
Table differentTable = missingValues.copy().dropRows(0);
949+
int lastRowNumber = missingValues.rowCount() - 1;
950+
assertThrows(IndexOutOfBoundsException.class,
951+
() -> Table.compareRows(lastRowNumber, missingValues, differentTable),
952+
"Row outside range does not throw exception");
953+
}
919954
}

0 commit comments

Comments
 (0)