-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
std-bits/table/src/main/java/org/enso/table/parsing/problems/NoOpProblemAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.enso.table.parsing.problems; | ||
|
||
import java.util.List; | ||
|
||
/** A problem aggregator which ignores problems. */ | ||
public class NoOpProblemAggregator implements ProblemAggregator { | ||
|
||
@Override | ||
public void reportInvalidFormat(String cell) {} | ||
|
||
@Override | ||
public void reportLeadingZeroes(String cell) {} | ||
|
||
@Override | ||
public void reportMismatchedQuote() {} | ||
|
||
@Override | ||
public boolean hasProblems() { | ||
throw new IllegalStateException("This implementation does not provide problem information."); | ||
} | ||
|
||
@Override | ||
public List<ParsingProblem> getAggregatedProblems() { | ||
throw new IllegalStateException("This implementation does not provide problem information."); | ||
} | ||
} |
59 changes: 10 additions & 49 deletions
59
std-bits/table/src/main/java/org/enso/table/parsing/problems/ProblemAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,32 @@ | ||
package org.enso.table.parsing.problems; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* An aggregator for parsing problems. | ||
* | ||
* <p>Each strategy exposes a method that returns a summary of the problems. The particular methods | ||
* for reporting each problem are defined in particular subclasses. | ||
*/ | ||
public class ProblemAggregator { | ||
|
||
private final List<String> invalidFormatCells = new ArrayList<>(); | ||
private final List<String> leadingZerosCells = new ArrayList<>(); | ||
private int mismatchedQuotes = 0; | ||
public final String relatedColumnName; | ||
|
||
public ProblemAggregator(String relatedColumnName) { | ||
this.relatedColumnName = relatedColumnName; | ||
} | ||
/** An aggregator for parsing problems. */ | ||
public interface ProblemAggregator { | ||
|
||
/** | ||
* Reports a cell with an invalid format. | ||
* | ||
* <p>The reports are aggregated and finally a single problem containing all invalid cell for the | ||
* <p>The reports are aggregated and finally a single problem containing all invalid cells for the | ||
* given column is reported. | ||
*/ | ||
public void reportInvalidFormat(String cell) { | ||
invalidFormatCells.add(cell); | ||
} | ||
void reportInvalidFormat(String cell); | ||
|
||
public void reportLeadingZeroes(String cell) { | ||
leadingZerosCells.add(cell); | ||
} | ||
/** Reports a cell containing unexpected leading zeros. */ | ||
void reportLeadingZeroes(String cell); | ||
|
||
public void reportMismatchedQuote() { | ||
mismatchedQuotes++; | ||
} | ||
/** Reports that a mismatched quote has been encountered. */ | ||
void reportMismatchedQuote(); | ||
|
||
/** | ||
* Checks if there are any problems already reported. | ||
* | ||
* <p>This method returns true if and only if {@code getAggregatedProblems} would return a | ||
* non-empty list. | ||
*/ | ||
public boolean hasProblems() { | ||
return !invalidFormatCells.isEmpty() || !leadingZerosCells.isEmpty() || mismatchedQuotes > 0; | ||
} | ||
boolean hasProblems(); | ||
|
||
/** Return an aggregated summary of problems that have been reported. */ | ||
public List<ParsingProblem> getAggregatedProblems() { | ||
List<ParsingProblem> problems = new ArrayList<>(); | ||
|
||
if (!invalidFormatCells.isEmpty()) { | ||
problems.add(new InvalidFormat(relatedColumnName, invalidFormatCells)); | ||
} | ||
|
||
if (!leadingZerosCells.isEmpty()) { | ||
problems.add(new LeadingZeros(relatedColumnName, leadingZerosCells)); | ||
} | ||
|
||
for (int i = 0; i < mismatchedQuotes; ++i) { | ||
problems.add(new MismatchedQuote()); | ||
} | ||
|
||
assert problems.isEmpty() == !hasProblems(); | ||
|
||
return problems; | ||
} | ||
List<ParsingProblem> getAggregatedProblems(); | ||
} |
56 changes: 56 additions & 0 deletions
56
std-bits/table/src/main/java/org/enso/table/parsing/problems/ProblemAggregatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.enso.table.parsing.problems; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ProblemAggregatorImpl implements ProblemAggregator { | ||
public final String relatedColumnName; | ||
private final List<String> invalidFormatCells = new ArrayList<>(); | ||
private final List<String> leadingZerosCells = new ArrayList<>(); | ||
private int mismatchedQuotes = 0; | ||
|
||
public ProblemAggregatorImpl(String relatedColumnName) { | ||
this.relatedColumnName = relatedColumnName; | ||
} | ||
|
||
@Override | ||
public void reportInvalidFormat(String cell) { | ||
invalidFormatCells.add(cell); | ||
} | ||
|
||
@Override | ||
public void reportLeadingZeroes(String cell) { | ||
leadingZerosCells.add(cell); | ||
} | ||
|
||
@Override | ||
public void reportMismatchedQuote() { | ||
mismatchedQuotes++; | ||
} | ||
|
||
@Override | ||
public boolean hasProblems() { | ||
return !invalidFormatCells.isEmpty() || !leadingZerosCells.isEmpty() || mismatchedQuotes > 0; | ||
} | ||
|
||
@Override | ||
public List<ParsingProblem> getAggregatedProblems() { | ||
List<ParsingProblem> problems = new ArrayList<>(); | ||
|
||
if (!invalidFormatCells.isEmpty()) { | ||
problems.add(new InvalidFormat(relatedColumnName, invalidFormatCells)); | ||
} | ||
|
||
if (!leadingZerosCells.isEmpty()) { | ||
problems.add(new LeadingZeros(relatedColumnName, leadingZerosCells)); | ||
} | ||
|
||
for (int i = 0; i < mismatchedQuotes; ++i) { | ||
problems.add(new MismatchedQuote()); | ||
} | ||
|
||
assert problems.isEmpty() == !hasProblems(); | ||
|
||
return problems; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...bits/table/src/main/java/org/enso/table/parsing/problems/SimplifiedProblemAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.enso.table.parsing.problems; | ||
|
||
import java.util.List; | ||
|
||
public class SimplifiedProblemAggregator implements ProblemAggregator { | ||
|
||
private boolean hasProblems = false; | ||
|
||
@Override | ||
public void reportInvalidFormat(String cell) { | ||
hasProblems = true; | ||
} | ||
|
||
@Override | ||
public void reportLeadingZeroes(String cell) { | ||
hasProblems = true; | ||
} | ||
|
||
@Override | ||
public void reportMismatchedQuote() { | ||
hasProblems = true; | ||
} | ||
|
||
@Override | ||
public boolean hasProblems() { | ||
return hasProblems; | ||
} | ||
|
||
public void reset() { | ||
hasProblems = false; | ||
} | ||
|
||
@Override | ||
public List<ParsingProblem> getAggregatedProblems() { | ||
throw new IllegalStateException("Problem aggregation is not available in this implementation."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters