|
1 | 1 | package io.snyk.languageserver.protocolextension.messageObjects.scanResults; |
2 | 2 |
|
3 | | -import java.util.*; |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
4 | 7 | import io.snyk.eclipse.plugin.domain.ProductConstants; |
5 | 8 |
|
6 | 9 | public class IssueComparator implements Comparator<Issue> { |
7 | | - |
8 | | - private final Map<Issue, List<Issue>> issuesGrouped; |
9 | | - |
10 | | - public IssueComparator() { |
11 | | - this.issuesGrouped = new HashMap<>(); |
12 | | - } |
13 | | - |
14 | | - public IssueComparator(Collection<Issue> issues) { |
15 | | - this.issuesGrouped = new HashMap<>(); |
16 | | - for (Issue issue : issues) { |
17 | | - this.issuesGrouped.computeIfAbsent(issue, k -> new ArrayList<>()).add(issue); |
18 | | - } |
19 | | - } |
20 | | - |
21 | 10 | @Override |
22 | 11 | public int compare(Issue o1, Issue o2) { |
23 | 12 | Map<String, Integer> severityOrder = getSeverityOrderHashMap(); |
24 | 13 |
|
25 | | - // Get ranks for the severities of the two issues |
26 | 14 | int rank1 = severityOrder.getOrDefault(o1.severity().toLowerCase(), Integer.MAX_VALUE); |
27 | 15 | int rank2 = severityOrder.getOrDefault(o2.severity().toLowerCase(), Integer.MAX_VALUE); |
28 | 16 |
|
29 | | - // Compare based on severity rank (lower rank = higher severity) |
| 17 | + // Compare based on severity rank |
30 | 18 | int severityComparison = Integer.compare(rank1, rank2); |
31 | 19 | if (severityComparison != 0) { |
32 | 20 | return severityComparison; |
33 | 21 | } |
34 | 22 |
|
35 | | - // Fallback: Compare by issue counts grouped by severity (cascading) |
36 | | - int o1Criticals = getCount(o1, ProductConstants.SEVERITY_CRITICAL); |
37 | | - int o2Criticals = getCount(o2, ProductConstants.SEVERITY_CRITICAL); |
38 | | - |
39 | | - int o1Highs = getCount(o1, ProductConstants.SEVERITY_HIGH); |
40 | | - int o2Highs = getCount(o2, ProductConstants.SEVERITY_HIGH); |
41 | | - |
42 | | - int o1Mediums = getCount(o1, ProductConstants.SEVERITY_MEDIUM); |
43 | | - int o2Mediums = getCount(o2, ProductConstants.SEVERITY_MEDIUM); |
44 | | - |
45 | | - int o1Lows = getCount(o1, ProductConstants.SEVERITY_LOW); |
46 | | - int o2Lows = getCount(o2, ProductConstants.SEVERITY_LOW); |
47 | | - |
48 | | - if (o1Criticals != o2Criticals) { |
49 | | - return Integer.compare(o2Criticals, o1Criticals); |
50 | | - } else if (o1Highs != o2Highs) { |
51 | | - return Integer.compare(o2Highs, o1Highs); |
52 | | - } else if (o1Mediums != o2Mediums) { |
53 | | - return Integer.compare(o2Mediums, o1Mediums); |
54 | | - } else if (o1Lows != o2Lows) { |
55 | | - return Integer.compare(o2Lows, o1Lows); |
56 | | - } |
57 | | - |
58 | 23 | // Fallback to comparing by hash codes if everything else is equal |
59 | 24 | return Integer.compare(o1.hashCode(), o2.hashCode()); |
60 | 25 | } |
61 | 26 |
|
62 | | - private int getCount(Issue issue, String severity) { |
63 | | - List<Issue> issuesForType = issuesGrouped.getOrDefault(issue, Collections.emptyList()); |
64 | | - return (int) issuesForType.stream() |
65 | | - .filter(i -> severity.equalsIgnoreCase(i.severity())) |
66 | | - .count(); |
67 | | - } |
68 | | - |
69 | 27 | private static Map<String, Integer> getSeverityOrderHashMap() { |
70 | 28 | Map<String, Integer> severityOrder = new HashMap<>(); |
71 | 29 | severityOrder.put(ProductConstants.SEVERITY_CRITICAL.toLowerCase(), 1); |
|
0 commit comments