Skip to content

Commit

Permalink
Optimize the performance of the copyrightFilePatternGenerality check
Browse files Browse the repository at this point in the history
  • Loading branch information
tibetiroka committed Mar 16, 2024
1 parent 6a88445 commit 9978c44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/tibetiroka/deblint/Linters.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

import static com.tibetiroka.deblint.FieldSpec.RequirementStatus.MANDATORY;
Expand Down Expand Up @@ -1030,7 +1031,7 @@ public static class TypeCopyrightLinter implements FileLinter {
/**
* A cache for compiled patters, used in {@link #toRegex(String)}.
*/
private static final HashMap<String, Pattern> PARSED_PATTERNS = new HashMap<>();
private static final ConcurrentHashMap<String, Pattern> PARSED_PATTERNS = new ConcurrentHashMap<>();

@Override
public void accept(ControlFile file, Configuration config) {
Expand Down Expand Up @@ -1124,14 +1125,13 @@ public boolean isMoreGeneric(String first, String second) {
*/
public void lintFileStanzas(ControlFile file, Configuration config) {
if(config.copyrightFilePatternGenerality) {
HashSet<String> previousPatterns = new HashSet<>();
ArrayList<String> previousPatterns = new ArrayList<>();
for(int i = 0; i < file.getSpecs().size(); i++) {
StanzaSpec spec = file.getSpecs().get(i);
if(spec.name().equals("file stanza")) {
ArrayList<String> currentPatterns = new ArrayList<>();
Stanza s = file.getStanzas().get(i);
DataField field = s.getField("Files");
Arrays.stream(field.data().split("\\n")).map(String::trim).filter(d -> !d.isEmpty()).forEachOrdered(currentPatterns::add);
List<String> currentPatterns = Arrays.stream(field.data().split("\\n")).map(String::trim).filter(d -> !d.isEmpty()).toList();
if(config.redundantFilePattern || config.duplicateFilePattern) {
for(int i1 = 0; i1 < currentPatterns.size(); i1++) {
String pat1 = currentPatterns.get(i1);
Expand All @@ -1146,13 +1146,13 @@ public void lintFileStanzas(ControlFile file, Configuration config) {
}
}
}
for(String current : currentPatterns) {
for(String previous : previousPatterns) {
previousPatterns.parallelStream().forEach(previous -> {
for(String current : currentPatterns) {
if(isMoreGeneric(current, previous)) {
Main.error("More generic patterns should precede specific ones: " + previous + " and " + current, "copyrightFilePatternGenerality", "https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/#files-field");
}
}
}
});
previousPatterns.addAll(currentPatterns.stream().map(this::normalizePattern).toList());
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/tibetiroka/deblint/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class Main {
* @param error The error message
* @throws IllegalArgumentException If testing
*/
public static void error(String error) {
public static synchronized void error(String error) {
error(error, null, null);
}

Expand All @@ -59,7 +59,7 @@ public static void error(String error) {
* @param check The name of the check that generated the error
* @throws IllegalArgumentException If testing
*/
public static void error(String error, String check) {
public static synchronized void error(String error, String check) {
error(error, check, null);
}

Expand All @@ -71,7 +71,7 @@ public static void error(String error, String check) {
* @param reference The error's description in the standard
* @throws IllegalArgumentException If testing
*/
public static void error(String error, String check, String reference) {
public static synchronized void error(String error, String check, String reference) {
ERROR_COUNT++;
if(IS_TEST) {
if(check != null) {
Expand Down Expand Up @@ -107,7 +107,7 @@ public static int getErrorCount() {
*
* @param information The message to print
*/
public static void info(String information) {
public static synchronized void info(String information) {
if(!IS_TEST) {
System.out.println(information);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public static void main(String[] args) {
* @param warning The warning to display
* @throws IllegalStateException If testing
*/
public static void warn(String warning) {
public static synchronized void warn(String warning) {
if(IS_TEST) {
throw new IllegalStateException(warning);
} else {
Expand Down

0 comments on commit 9978c44

Please sign in to comment.