Skip to content

Commit

Permalink
add markers
Browse files Browse the repository at this point in the history
  • Loading branch information
a13519 committed May 23, 2024
1 parent 07cdb57 commit 22121cb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
31 changes: 25 additions & 6 deletions src/main/java/net/zousys/compressedtable/ComparisonResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class ComparisonResult {
private List<RowResult> mismatches = new ArrayList<>();
private List<String> unitedHeaders;

/**
*
* @param before
* @param after
*/
public ComparisonResult(@NonNull CompressedTable before, @NonNull CompressedTable after) {
this.before = before;
this.after = after;
Expand All @@ -39,18 +44,32 @@ public static class RowResult {
@Getter
List<ResultField> fields = new ArrayList<>();
@Getter
@Setter
boolean unifiedMismatch;
/**
* the mismatched row discrepancy counts
*/
private int missMatchNumber;

/**
* add field result to row result
* @param resultField
*/
public void addFieldResult(ResultField resultField) {
fields.add(resultField);
if (resultField.isMissmatched()&&!resultField.isIgnored()){
missMatchNumber++;
}
}
}

@Builder
@Getter
@Setter
public static class ResultField {
String beforeField;
String afterField;
boolean missmatched;
boolean ignored;
private String name;
private String beforeField;
private String afterField;
private boolean missmatched;
private boolean ignored;
}

}
48 changes: 44 additions & 4 deletions src/main/java/net/zousys/compressedtable/CompressedComparator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.zousys.compressedtable;

import lombok.Builder;
import lombok.Getter;
import net.zousys.compressedtable.impl.CompressedTable;

import java.io.IOException;
Expand Down Expand Up @@ -33,15 +34,44 @@ public class CompressedComparator {

private Map<String, Integer> unitedHeaderMapping;

@Getter
@Builder.Default
private Map<String, Integer> markers = new HashMap<>();

private boolean trim;

/**
* This is to set the ignored colume of table in comparison, those columns won't be compared
*
* @param fields the columns
* @return
*/
public CompressedComparator setIgnoredFields(String[] fields) {
ignoredFields = new HashSet<>();
ignoredFields.addAll(Arrays.stream(fields).collect(Collectors.toSet()));
return this;
}

/**
* This is to count mismatched column time, later after the result spread sheet / csv generated, the header will mark the times of discrenpancies
* @param mismatch
*/
private void addMarker(ComparisonResult.RowResult mismatch) {
for (ComparisonResult.ResultField arf : mismatch.getFields()) {
if (arf.isMissmatched()) {
Integer ai = markers.get(arf.getName());
if (ai == null) {
markers.put(arf.getName(), 1);
} else {
markers.put(arf.getName(), ai.intValue() + 1);
}
}
}
}
/**
* This is to compare two tables
*
*/
public void compare() {
// missed in before
contains(after.getKeyedMapping().keySet(), before.getKeyedMapping().keySet(), beforeMissed, null);
Expand Down Expand Up @@ -92,7 +122,10 @@ public void compare() {
after,
trim,
unitedHeaders);
comparatorListener.handleMisMatched(mismatch);
if (mismatch.getMissMatchNumber() > 0) {
addMarker(mismatch);
comparatorListener.handleMisMatched(mismatch);
}
// remove from before and after
after.removeRowByKey(key);
before.removeRowByKey(key);
Expand Down Expand Up @@ -140,21 +173,25 @@ private static final ComparisonResult.RowResult compareRow(
Integer afterInd = after.getHeaderMapping().get(headerA);
String fvbefore = beforeInd == null ? null : fieldsA.get(beforeInd);
String fvafter = afterInd == null ? null : fieldsB.get(afterInd);

ComparisonResult.ResultField rf = ComparisonResult.ResultField.builder()
.name(headerA)
.beforeField(trim && fvbefore != null ? fvbefore.trim() : fvbefore)
.afterField(trim && fvafter != null ? fvafter.trim() : fvafter)
.build();
rowResult.getFields().add(rf);


if (ignoredFields != null && ignoredFields.contains(headerA)) {
rf.setIgnored(true);
rf.setMissmatched(false);
} else {
if ((rf.getBeforeField() == null || rf.getAfterField() == null)
|| !rf.getBeforeField().equals(rf.getAfterField())) {
rf.missmatched = true;
rowResult.unifiedMismatch = true;
rf.setMissmatched(true);
}
}

rowResult.getFields().add(rf);
}

return rowResult;
Expand Down Expand Up @@ -207,6 +244,9 @@ private void pickMissed() {

}

/**
* Union of before and after table
*/
public void uniteHeaders() {
unitedHeaders = new ArrayList<>();
unitedHeaderMapping = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class CompressedTableFactory {
public static enum Type {
CSV, EXCEL
}

private int ignoredLines = 0;
private String[] keyHeaders = new String[]{};
private char delimeter = ',';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CompressedTable implements GeneralTable {
private String[] headerkeys;
private boolean onHeader = true;
private int headerRowNumber = -1;
private int physicalLineNumber = 0;
/**
*
* @param no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ private void populate() {
});
}

/**
*
* @param rowResult
* @param comparisonResult
* @throws Exception
*/
private void append(ComparisonResult.RowResult rowResult, ComparisonResult comparisonResult) throws Exception {
XSSFRow row = spreadsheet.createRow(rowid++);
cellid = 0;
Expand Down

0 comments on commit 22121cb

Please sign in to comment.