1 package com.stateofflow.eclipse.metrics.calculators.cohesion;
2
3 class PairwiseFieldIrrelationEvaluator {
4 private double calculate(final String field1, final ClassComposition classComposition) {
5 double total = 0;
6 for (final String field2 : classComposition.getFields()) {
7 total += calculate(field1, field2, classComposition);
8 }
9 return total;
10 }
11
12 private double calculate(final String field1, final String field2, final ClassComposition classComposition) {
13 return field1.compareTo(field2) <= 0 ? 0 : 1.0 - ((double) getNumberOfMethodsContainingBoth(field1, field2, classComposition)) / getNumberOfMethodsContainingEither(field1, field2, classComposition);
14 }
15
16 public double evaluate(final ClassComposition classComposition) {
17 if (classComposition.getNumberOfFields() <= 1) {
18 return 0;
19 }
20
21 double total = 0;
22
23 for (final String field1 : classComposition.getFields()) {
24 total += calculate(field1, classComposition);
25 }
26
27 return 2d * total / (classComposition.getNumberOfFields() * (classComposition.getNumberOfFields() - 1));
28 }
29
30 private int getNumberOfMethodsContainingBoth(final String field1, final String field2, final ClassComposition classComposition) {
31 return classComposition.countMatchingMethods(new FieldSet.Predicate() {
32 public boolean execute(final FieldSet method) {
33 return method.contains(field1) && method.contains(field2);
34 }
35 });
36 }
37
38 private int getNumberOfMethodsContainingEither(final String field1, final String field2, final ClassComposition classComposition) {
39 return classComposition.countMatchingMethods(new FieldSet.Predicate() {
40 public boolean execute(final FieldSet method) {
41 return method.contains(field1) || method.contains(field2);
42 }
43 });
44 }
45 }
46