Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 127 |  | Methods: | 7
| NCLOC: | 93 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
StatisticalRule.java | 95.8% | 100% | 100% | 98.8% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.stat;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.RuleContext;
8 |  |
9 |  | import java.util.Iterator;
10 |  | import java.util.List;
11 |  | import java.util.Set;
12 |  | import java.util.SortedSet;
13 |  | import java.util.TreeSet;
14 |  |
15 |  | /**
16 |  | * @author David Dixon-Peugh
17 |  | * Aug 8, 2002 StatisticalRule.java
18 |  | */
19 |  | public abstract class StatisticalRule extends AbstractRule {
20 |  |
21 |  | public static double DELTA = 0.000005; // Within this range. . .
22 |  |
23 |  | private SortedSet dataPoints = new TreeSet();
24 |  |
25 |  | private int count = 0;
26 |  | private double total = 0.0;
27 |  |
28 | 23719 | public void addDataPoint(DataPoint point) {
29 | 23719 | count++;
30 | 23719 | total += point.getScore();
31 | 23719 | dataPoints.add(point);
32 |  | }
33 |  |
34 | 175 | public void apply(List acus, RuleContext ctx) {
35 | 175 | visitAll(acus, ctx);
36 |  |
37 | 175 | double deviation;
38 | 175 | double minimum = 0.0;
39 |  |
40 | 175 | if (hasProperty("sigma")) {
41 | 108 | deviation = getStdDev();
42 | 108 | double sigma = getDoubleProperty("sigma");
43 | 108 | minimum = getMean() + (sigma * deviation);
44 |  | }
45 |  |
46 | 175 | if (hasProperty("minimum")) {
47 | 126 | double mMin = getDoubleProperty("minimum");
48 | 126 | if (mMin > minimum) {
49 | 102 | minimum = mMin;
50 |  | }
51 |  | }
52 |  |
53 | 175 | SortedSet newPoints = applyMinimumValue(dataPoints, minimum);
54 |  |
55 | 175 | if (hasProperty("topscore")) {
56 | 108 | int topScore = getIntProperty("topscore");
57 | 108 | if (newPoints.size() >= topScore) {
58 | 48 | newPoints = applyTopScore(newPoints, topScore);
59 |  | }
60 |  | }
61 |  |
62 | 175 | makeViolations(ctx, newPoints);
63 |  |
64 | 175 | double low = 0.0d;
65 | 175 | double high = 0.0d;
66 | 175 | if (!dataPoints.isEmpty()) {
67 | 175 | low = ((DataPoint) dataPoints.first()).getScore();
68 | 175 | high = ((DataPoint) dataPoints.last()).getScore();
69 |  | }
70 |  |
71 | 175 | ctx.getReport().addMetric(new Metric(this.getName(), count, total, low, high, getMean(), getStdDev()));
72 |  |
73 | 175 | dataPoints.clear();
74 |  | }
75 |  |
76 | 566 | protected double getMean() {
77 | 566 | return total / count;
78 |  | }
79 |  |
80 | 283 | protected double getStdDev() {
81 | 283 | Iterator points = dataPoints.iterator();
82 | 283 | double mean = getMean();
83 | 283 | double deltaSq = 0.0;
84 |  |
85 | 283 | if (dataPoints.size() < 2) {
86 | 17 | return Double.NaN;
87 |  | }
88 |  |
89 | 266 | while (points.hasNext()) {
90 | 26502 | DataPoint point = (DataPoint) points.next();
91 | 26502 | deltaSq += ((point.getScore() - mean) * (point.getScore() - mean));
92 |  | }
93 |  |
94 | 266 | return Math.sqrt(deltaSq / (dataPoints.size() - 1));
95 |  | }
96 |  |
97 | 175 | protected SortedSet applyMinimumValue(SortedSet pointSet, double minValue) {
98 | 175 | Iterator points = pointSet.iterator();
99 | 175 | SortedSet RC = new TreeSet();
100 |  |
101 | 175 | while (points.hasNext()) {
102 | 15719 | DataPoint point = (DataPoint) points.next();
103 |  |
104 | 15719 | if (point.getScore() > (minValue - DELTA)) {
105 | 3990 | RC.add(point);
106 |  | }
107 |  | }
108 | 175 | return RC;
109 |  | }
110 |  |
111 | 48 | protected SortedSet applyTopScore(SortedSet points, int topScore) {
112 | 48 | SortedSet s = new TreeSet();
113 | 48 | Object[] arr = points.toArray();
114 | 48 | for (int i = arr.length - 1; i >= (arr.length - topScore); i--) {
115 | 616 | s.add(arr[i]);
116 |  | }
117 | 48 | return s;
118 |  | }
119 |  |
120 | 175 | protected void makeViolations(RuleContext ctx, Set p) {
121 | 175 | Iterator points = p.iterator();
122 | 175 | while (points.hasNext()) {
123 | 2146 | DataPoint point = (DataPoint) points.next();
124 | 2146 | addViolationWithMessage(ctx, point.getNode(), point.getMessage());
125 |  | }
126 |  | }
127 |  | }