Skip to content

Commit 125ca54

Browse files
adamretterRadek Hubner
authored andcommitted
[ignore] Minor code cleanup
1 parent bac71c1 commit 125ca54

File tree

4 files changed

+24
-35
lines changed

4 files changed

+24
-35
lines changed

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/AllErrorsMetaDataValidator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ trait AllErrorsMetaDataValidator extends MetaDataValidator {
2929
val row = rows.next()
3030
val result = validateRow(row, schema, Some(rows.hasNext))
3131
/*
32-
** Only store the results if they contain a warning or a failure. This means the validator is not limited by the available memory
33-
** when processing large files.
34-
*/
35-
if (containsErrors(result) || containsWarnings(result) ) {
32+
Only store the results if they contain a warning or a failure. This means the validator is not limited by the
33+
available memory when processing large files.
34+
*/
35+
if (containsErrors(result) || containsWarnings(result)) {
3636
validateRows(result :: results)
3737
} else {
3838
validateRows(results)
@@ -69,4 +69,4 @@ trait AllErrorsMetaDataValidator extends MetaDataValidator {
6969
if(isWarningDirective) toWarnings(ruleResult, row.lineNumber, columnIndex) else toErrors(ruleResult, row.lineNumber, columnIndex)
7070
}}.sequence[MetaDataValidation, Any]
7171
}
72-
}
72+
}

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/FailFastMetaDataValidator.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ trait FailFastMetaDataValidator extends MetaDataValidator {
3434
val row = rows.next()
3535
val result = validateRow(row, schema, Some(rows.hasNext))
3636
/*
37-
** Only store the results if they contain a warning or a failure. This means the validator is not limited by the available memory
38-
** when processing large files.
37+
Only store the results if they contain a warning or a failure. This means the validator is not limited by the
38+
available memory when processing large files.
3939
*/
40-
if (containsErrors(result) || containsWarnings(result) ) {
40+
if (containsErrors(result) || containsWarnings(result)) {
4141
validateRows(result :: results)
4242
} else {
4343
validateRows(results)

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/Rule.scala

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,35 +98,29 @@ abstract class Rule(name: String, val argProviders: ArgProvider*) extends Positi
9898

9999
}
100100

101-
/**
102-
* This object is a place to store the precompilled regexs
103-
* @author Jess Flanagan
101+
/**
102+
* This object is a place to store the precompiled regexs
103+
* @author Jess Flanagan
104104
*/
105-
object RegexCache
106-
{
105+
object RegexCache {
107106
val cache = collection.mutable.Map[String, Pattern]()
108107

109-
/**
110-
* This function returns compiled regexs.
111-
* First we check to see if its already in the cache. Otherwise we compile it, add it to the cache and return the
112-
* compiled version. This results in a significant speed up for processing large files.
113-
* @param pattern A regex pattern string.
114-
* @returns A compiled representation of a regular expression.
115-
* @author Jess Flanagan
116-
*/
117-
def getCompiledRegex(pattern: String): Pattern ={
118-
if (!cache.contains(pattern)){
119-
cache += (pattern -> Pattern.compile(pattern))
120-
}
121-
cache(pattern);
122-
}
108+
/**
109+
* This function returns compiled regexs.
110+
* First we check to see if its already in the cache. Otherwise we compile it, add it to the cache and return the
111+
* compiled version. This results in a significant speed up for processing large files.
112+
* @param pattern A regex pattern string.
113+
*
114+
* @return A compiled representation of a regular expression.
115+
* @author Jess Flanagan
116+
*/
117+
def getCompiledRegex(pattern: String): Pattern = cache.getOrElseUpdate(pattern, Pattern.compile(pattern))
123118
}
124119

125120
abstract class PatternRule(name: String, pattern: String) extends Rule(name) {
126121
// Uses the cache to retrieve a compiled regex representation for the pattern string.
127-
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema, mayBeLast: Option[Boolean] = None): Boolean ={
122+
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema, mayBeLast: Option[Boolean] = None): Boolean = {
128123
RegexCache.getCompiledRegex(pattern).matcher(cellValue).matches()
129-
130124
}
131125
}
132126

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/v1_0/Rule.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,15 @@ case class IfRule(condition: Rule, rules: List[Rule], elseRules: Option[List[Rul
9494

9595
abstract class PatternRule(name: String, pattern: String) extends Rule(name) {
9696
// Uses the cache to retrieve a compiled regex representation for the pattern string.
97-
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema, mayBeLast: Option[Boolean] = None): Boolean ={
97+
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema, mayBeLast: Option[Boolean] = None): Boolean = {
9898
RegexCache.getCompiledRegex(pattern).matcher(cellValue).matches()
99-
10099
}
101-
102100
}
103101

104102
case class RegExpRule(regex: String) extends Rule("regex") {
105103
override def valid(cellValue: String, columnDefinition: ColumnDefinition, columnIndex: Int, row: Row, schema: Schema, mayBeLast: Option[Boolean] = None): Boolean = {
106-
107104
val regexp = if (columnDefinition.directives.contains(IgnoreCase())) "(?i)" + regex else regex
108-
109105
RegexCache.getCompiledRegex(regexp).matcher(cellValue).matches()
110-
111106
}
112107

113108
override def toError = {

0 commit comments

Comments
 (0)