KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > analysis > Lines


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: Lines.java 174 2006-11-19 16:22:19Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.analysis;
9
10 import com.mountainminds.eclemma.core.analysis.ILineCoverage;
11
12 /**
13  * ILineCoverage implementation.
14  *
15  * @author Marc R. Hoffmann
16  * @version $Revision: 174 $
17  */

18 public class Lines extends Counter implements ILineCoverage {
19
20   /**
21    * We allways increase the size of the coverage array by multiples of this
22    * constant. Larger values will require more memory, a lower value will lead
23    * to repeated memory allocation and copy operations.
24    */

25   private static final int BLOCK_INCREMENT = 64;
26
27   /** Array of flags for each line */
28   private byte[] coverage = null;
29
30   private int firstline = 0;
31
32   private int lastline = 0;
33
34   private int offset = 0;
35   
36   protected Lines() {
37     super(0, 0);
38   }
39
40   private int getBlockAlignedOffset(int line) {
41     return line - (line % BLOCK_INCREMENT);
42   }
43
44   private int getBlockAlignedSize(int size) {
45     return size + BLOCK_INCREMENT - (size % BLOCK_INCREMENT);
46   }
47
48   /**
49    * Updates the firstline and lastline property. If the given line falls
50    * outside the coverage array the array is increased accordingly.
51    *
52    * @param line
53    * line number to check
54    */

55   private void ensureCapacity(int line) {
56     if (coverage == null) {
57       firstline = line;
58       lastline = line;
59       offset = getBlockAlignedOffset(line);
60       coverage = new byte[BLOCK_INCREMENT];
61       return;
62     }
63     if (firstline > line) {
64       firstline = line;
65       if (offset > line) {
66         int newoffset = getBlockAlignedOffset(line);
67         byte[] newcoverage = new byte[coverage.length + offset - newoffset];
68         System.arraycopy(coverage, 0, newcoverage, offset - newoffset,
69             coverage.length);
70         offset = newoffset;
71         coverage = newcoverage;
72       }
73       return;
74     }
75     if (lastline < line) {
76       lastline = line;
77       if (line - offset >= coverage.length) {
78         byte[] newcoverage = new byte[getBlockAlignedSize(line - offset)];
79         System.arraycopy(coverage, 0, newcoverage, 0, coverage.length);
80         coverage = newcoverage;
81       }
82     }
83   }
84
85   public void addLines(int lines[], boolean isCovered) {
86     for (int i = 0; i < lines.length; i++) {
87       int line = lines[i];
88       ensureCapacity(line);
89       int idx = line - offset;
90       switch (coverage[idx]) {
91       case NO_CODE:
92         total++;
93         if (isCovered)
94           covered++;
95         break;
96       case NOT_COVERED:
97         if (isCovered)
98           covered++;
99         break;
100       }
101       coverage[idx] |= isCovered ? FULLY_COVERED : NOT_COVERED;
102     }
103   }
104
105   public Counter increment(int total, int covered) {
106     throw new UnsupportedOperationException JavaDoc();
107   }
108   
109   // ILineCoverage interface:
110

111   public byte[] getCoverage() {
112     return coverage;
113   }
114
115   public int getFirstLine() {
116     return firstline;
117   }
118
119   public int getLastLine() {
120     return lastline;
121   }
122
123   public int getOffset() {
124     return offset;
125   }
126
127 }
128
Popular Tags