KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > recording > CoverageState


1 /*
2  * Created on 16.08.2004
3  *
4  * written by Matthias Kempka
5  */

6 package de.uka.ipd.coverage.recording;
7
8 import java.io.Serializable JavaDoc;
9
10 /**
11  * Created on 16.08.2004
12  * The class CoverageState represents a coverage state for a basic block, path,
13  * method or class. It is dependent on the coverage nature.
14  * There are three possible states: Full coverage, partial coverage or no coverage
15  * at all, predefined objects can and should be accessed with the static accessors
16  * <code>CoverageState.FULL_COVERAGE</code>, <code>CoverageState.PARTIAL_COVERAGE</code>
17  * and <code>CoverageState.NO_COVERAGE</code>.
18  * If there is need for a coverage percentile, additional CoverageState objects
19  * can be created via the constructor <code>CoverageState(double)</code>
20  * @author Matthias Kempka
21  */

22 /*
23  * It would have been nice to use booleans for coverage states, but
24  * there is an additional state, the partional coverage.
25  * class is final to make reference comparison safe.
26  */

27 public class CoverageState implements Comparable JavaDoc, Serializable JavaDoc {
28     
29     protected static final int FULL = 10;
30     protected static final int PARTIAL = 5;
31     protected static final int NONE = 0;
32     
33     public static final CoverageState FULL_COVERAGE = new CoverageState(FULL);
34     public static final CoverageState PARTIAL_COVERAGE = new CoverageState(PARTIAL);
35     public static final CoverageState NO_COVERAGE = new CoverageState(NONE);
36     
37     
38     protected int state;
39    
40      
41     private CoverageState(int state) {
42         this.state = state;
43     }
44     
45     public String JavaDoc toString() {
46         String JavaDoc stateString = ""; //$NON-NLS-1$
47
if (this.state == FULL) {
48             stateString = "FULL_COVERAGE"; //$NON-NLS-1$
49
} else if (this.state == PARTIAL) {
50             stateString = "PARTIAL_COVERAGE"; //$NON-NLS-1$
51
} else if (this.state == NONE) {
52             stateString = "NO_COVERAGE"; //$NON-NLS-1$
53
}
54         return stateString;
55     }
56
57     /* (non-Javadoc)
58      * @see java.lang.Comparable#compareTo(java.lang.Object)
59      */

60     public int compareTo(Object JavaDoc anotherCoverageState) {
61         if (!(anotherCoverageState instanceof CoverageState)) {
62             throw new IllegalArgumentException JavaDoc("Can only compare " + //$NON-NLS-1$
63
"CoverageState objects"); //$NON-NLS-1$
64
}
65         CoverageState obj = (CoverageState) anotherCoverageState;
66         return new Integer JavaDoc(this.state).compareTo(new Integer JavaDoc(obj.state));
67     }
68     
69     public boolean isGreaterThan(CoverageState comp) {
70         if (this.compareTo(comp) > 0) {
71             return true;
72         }
73         return false;
74     }
75
76     /**
77      * returns a new CoverageState combining two given CoverageStates.
78      * if both states are at NO_COVERAGE respective FULL_COVERAGE the result will
79      * be their state.
80      * All other combinations result in PARTIAL_COVERAGE.
81      * @param state1
82      * @param state2
83      */

84     public static CoverageState mergeCoverages(CoverageState state1, CoverageState state2) {
85         // calculate the state for the method
86
CoverageState result = null;
87         if (state1.equals(CoverageState.NO_COVERAGE)
88                 && state2.equals(CoverageState.NO_COVERAGE)) {
89             result = CoverageState.NO_COVERAGE;
90         } else if (state1.equals(CoverageState.FULL_COVERAGE)
91                 && state2.equals(CoverageState.FULL_COVERAGE)) {
92             result = CoverageState.FULL_COVERAGE;
93         } else {
94             result = CoverageState.PARTIAL_COVERAGE;
95         }
96         return result;
97     }
98
99 }
100
Popular Tags