~ubuntu-branches/ubuntu/natty/cobertura/natty

« back to all changes in this revision

Viewing changes to src/net/sourceforge/cobertura/coveragedata/JumpData.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-05-11 19:21:46 UTC
  • mfrom: (0.1.4 sid) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100511192146-j742v5jsl89ztndu
Tags: 1.9.4.1+dfsg-2
* Now Build-Depends on libservlet2.5-java and add a missing Depends
  on the same package. (Closes: #580842). 
* Simplify list of JRE dependences for cobertura and drop JRE dependences for
  libcobertura-java as Java libraries are no longer required to depend on a
  JVM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
package net.sourceforge.cobertura.coveragedata;
23
23
 
 
24
import java.io.IOException;
 
25
import java.io.ObjectInputStream;
24
26
import java.io.Serializable;
 
27
import java.util.concurrent.locks.Lock;
 
28
import java.util.concurrent.locks.ReentrantLock;
25
29
 
26
30
/**
27
31
 * <p>
35
39
{
36
40
        private static final long serialVersionUID = 8;
37
41
 
 
42
        protected transient Lock lock;
 
43
 
38
44
        private int conditionNumber;
39
45
 
40
46
        private long trueHits;
47
53
                this.conditionNumber = conditionNumber;
48
54
                this.trueHits = 0L;
49
55
                this.falseHits = 0L;
 
56
                initLock();
 
57
        }
 
58
        
 
59
        private void initLock()
 
60
        {
 
61
                lock = new ReentrantLock();
50
62
        }
51
63
 
52
64
        public int compareTo(Object o)
56
68
                return this.conditionNumber - ((JumpData) o).conditionNumber;
57
69
        }
58
70
 
59
 
        void touchBranch(boolean branch)
 
71
        void touchBranch(boolean branch,int new_hits)
60
72
        {
61
 
                if (branch)
 
73
                lock.lock();
 
74
                try
62
75
                {
63
 
                        this.trueHits++;
 
76
                        if (branch)
 
77
                        {
 
78
                                this.trueHits+=new_hits;
 
79
                        }
 
80
                        else
 
81
                        {
 
82
                                this.falseHits+=new_hits;
 
83
                        }
64
84
                }
65
 
                else
 
85
                finally
66
86
                {
67
 
                        this.falseHits++;
 
87
                        lock.unlock();
68
88
                }
69
89
        }
70
90
 
75
95
 
76
96
        public long getTrueHits()
77
97
        {
78
 
                return this.trueHits;
 
98
                lock.lock();
 
99
                try
 
100
                {
 
101
                        return this.trueHits;
 
102
                }
 
103
                finally
 
104
                {
 
105
                        lock.unlock();
 
106
                }
79
107
        }
80
108
 
81
109
        public long getFalseHits()
82
110
        {
83
 
                return this.falseHits;
 
111
                lock.lock();
 
112
                try
 
113
                {
 
114
                        return this.falseHits;
 
115
                }
 
116
                finally
 
117
                {
 
118
                        lock.unlock();
 
119
                }
84
120
        }
85
121
 
86
122
        public double getBranchCoverageRate()
87
123
        {
88
 
                return ((double) getNumberOfCoveredBranches()) / getNumberOfValidBranches();
 
124
                lock.lock();
 
125
                try
 
126
                {
 
127
                        return ((double) getNumberOfCoveredBranches()) / getNumberOfValidBranches();
 
128
                }
 
129
                finally
 
130
                {
 
131
                        lock.unlock();
 
132
                }
89
133
        }
90
134
 
91
135
        public boolean equals(Object obj)
96
140
                        return false;
97
141
 
98
142
                JumpData branchData = (JumpData) obj;
99
 
                return (this.trueHits == branchData.trueHits)
100
 
                                && (this.falseHits == branchData.falseHits)
101
 
                                && (this.conditionNumber == branchData.conditionNumber);
 
143
                getBothLocks(branchData);
 
144
                try
 
145
                {
 
146
                        return (this.trueHits == branchData.trueHits)
 
147
                                        && (this.falseHits == branchData.falseHits)
 
148
                                        && (this.conditionNumber == branchData.conditionNumber);
 
149
                }
 
150
                finally
 
151
                {
 
152
                        lock.unlock();
 
153
                        branchData.lock.unlock();
 
154
                }
102
155
        }
103
156
 
104
157
        public int hashCode()
108
161
 
109
162
        public int getNumberOfCoveredBranches()
110
163
        {
111
 
                return ((trueHits > 0) ? 1 : 0) + ((falseHits > 0) ? 1: 0);
 
164
                lock.lock();
 
165
                try
 
166
                {
 
167
                        return ((trueHits > 0) ? 1 : 0) + ((falseHits > 0) ? 1: 0);
 
168
                }
 
169
                finally
 
170
                {
 
171
                        lock.unlock();
 
172
                }
112
173
        }
113
174
 
114
175
        public int getNumberOfValidBranches()
119
180
        public void merge(BranchCoverageData coverageData)
120
181
        {
121
182
                JumpData jumpData = (JumpData) coverageData;
122
 
                this.trueHits += jumpData.trueHits;
123
 
                this.falseHits += jumpData.falseHits;
 
183
                getBothLocks(jumpData);
 
184
                try
 
185
                {
 
186
                        this.trueHits += jumpData.trueHits;
 
187
                        this.falseHits += jumpData.falseHits;
 
188
                }
 
189
                finally
 
190
                {
 
191
                        lock.unlock();
 
192
                        jumpData.lock.unlock();
 
193
                }
 
194
        }
 
195
 
 
196
        private void getBothLocks(JumpData other) {
 
197
                /*
 
198
                 * To prevent deadlock, we need to get both locks or none at all.
 
199
                 * 
 
200
                 * When this method returns, the thread will have both locks.
 
201
                 * Make sure you unlock them!
 
202
                 */
 
203
                boolean myLock = false;
 
204
                boolean otherLock = false;
 
205
                while ((!myLock) || (!otherLock))
 
206
                {
 
207
                        try
 
208
                        {
 
209
                                myLock = lock.tryLock();
 
210
                                otherLock = other.lock.tryLock();
 
211
                        }
 
212
                        finally
 
213
                        {
 
214
                                if ((!myLock) || (!otherLock))
 
215
                                {
 
216
                                        //could not obtain both locks - so unlock the one we got.
 
217
                                        if (myLock)
 
218
                                        {
 
219
                                                lock.unlock();
 
220
                                        }
 
221
                                        if (otherLock)
 
222
                                        {
 
223
                                                other.lock.unlock();
 
224
                                        }
 
225
                                        //do a yield so the other threads will get to work.
 
226
                                        Thread.yield();
 
227
                                }
 
228
                        }
 
229
                }
 
230
        }
 
231
        
 
232
        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
 
233
        {
 
234
                in.defaultReadObject();
 
235
                initLock();
124
236
        }
125
237
 
126
238
}