1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * Validates the ClaimStatistic results meet the specified requirements.
33 */
34 public final class ClaimValidator {
35 /**
36 * The map of max counter limits.
37 */
38 private final ConcurrentHashMap<ClaimStatistic.Counter, Integer> max = new ConcurrentHashMap<>();
39 /**
40 * The map of min counter limits.
41 */
42 private final ConcurrentHashMap<ClaimStatistic.Counter, Integer> min = new ConcurrentHashMap<>();
43 /**
44 * {@code true} if errors were detected in the claim.
45 */
46 private boolean hasErrors;
47
48 /**
49 * Constructor.
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 * Returns {@code true} if any validation failed.
61 * @return {@code true} if any validation failed.
62 */
63 public boolean hasErrors() {
64 return hasErrors;
65 }
66
67 /**
68 * Sets the max value for the specified counter.
69 * @param counter the counter to set the limit for.
70 * @param value the value to set. A negative value specifies no maximum value.
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 * Sets the max value for the specified counter.
83 * @param counter the counter to set the limit for.
84 * @param value the value to set. A negative value specifies no maximum value.
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 * Gets the limit for the specific counter.
93 * @param counter the counter to get the limit for.
94 * @return the limit for the counter or 0 if not set.
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 * Gets the limit for the specific counter.
103 * @param counter the counter to get the limit for.
104 * @return the limit for the counter or 0 if not set.
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 * Determines if the specified count is within the limits for the counter.
113 * @param counter The counter to check.
114 * @param count the limit to check.
115 * @return {@code true} if the count is within the limits, {@code false} otherwise.
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 * Logs all the invalid values as errors.
125 * @param statistic The statistics that contain the run values.
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 * Creates a list of items that have issues.
140 * @param statistic The statistics that contain the run values.
141 * @return a collection of counter names that are invalid.
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 }