1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.config.results;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.ConcurrentHashMap;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.rat.report.claim.ClaimStatistic;
27 import org.apache.rat.utils.DefaultLog;
28
29 import static java.lang.String.format;
30
31
32
33
34 public final class ClaimValidator {
35
36
37
38 private final ConcurrentHashMap<ClaimStatistic.Counter, Integer> max = new ConcurrentHashMap<>();
39
40
41
42 private final ConcurrentHashMap<ClaimStatistic.Counter, Integer> min = new ConcurrentHashMap<>();
43
44
45
46 private boolean hasErrors;
47
48
49
50
51 public ClaimValidator() {
52 for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
53 max.put(counter,
54 counter.getDefaultMaxValue() < 0 ? Integer.MAX_VALUE : counter.getDefaultMaxValue());
55 min.put(counter, counter.getDefaultMinValue());
56 }
57 }
58
59
60
61
62
63 public boolean hasErrors() {
64 return hasErrors;
65 }
66
67
68
69
70
71
72 public void setMax(final ClaimStatistic.Counter counter, final int value) {
73 if (value < 0) {
74 max.put(counter, Integer.MAX_VALUE);
75 } else {
76 max.put(counter, value);
77 }
78 min.compute(counter, (k, v) -> v != null && v > max.get(k) ? max.get(k) : v);
79 }
80
81
82
83
84
85
86 public void setMin(final ClaimStatistic.Counter counter, final int value) {
87 min.put(counter, value);
88 max.compute(counter, (k, v) -> v == null || v < value ? value : v);
89 }
90
91
92
93
94
95
96 public int getMax(final ClaimStatistic.Counter counter) {
97 Integer result = max.get(counter);
98 return result == null ? 0 : result;
99 }
100
101
102
103
104
105
106 public int getMin(final ClaimStatistic.Counter counter) {
107 Integer result = min.get(counter);
108 return result == null ? 0 : result;
109 }
110
111
112
113
114
115
116
117 public boolean isValid(final ClaimStatistic.Counter counter, final int count) {
118 boolean result = max.get(counter) >= count && min.get(counter) <= count;
119 hasErrors |= !result;
120 return result;
121 }
122
123
124
125
126
127 public void logIssues(final ClaimStatistic statistic) {
128 for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
129 if (!isValid(counter, statistic.getCounter(counter))) {
130 DefaultLog.getInstance().error(format("Unexpected count for %s, limit is [%s,%s]. Count: %s", counter,
131 min.get(counter), max.get(counter), statistic.getCounter(counter)));
132 DefaultLog.getInstance().info(format("%s (%s) is %s", counter, counter.displayName(),
133 StringUtils.uncapitalize(counter.getDescription())));
134 }
135 }
136 }
137
138
139
140
141
142
143 public List<String> listIssues(final ClaimStatistic statistic) {
144 List<String> result = new ArrayList<>();
145 for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
146 if (!isValid(counter, statistic.getCounter(counter))) {
147 result.add(counter.toString());
148 }
149 }
150 return result;
151 }
152 }