Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
142   543   51   8.35
60   278   0.36   4.25
17     3  
4    
 
  StrictDuplicateCodeCheck       Line # 46 87.9% 0.8786127
44.01 115 41 41 0.36
  StrictDuplicateCodeCheck.ChecksumGenerator       Line # 63 - -1.0
0 0 0 0 -
  StrictDuplicateCodeCheck.TextfileChecksumGenerator       Line # 81 95% 0.95
8.01 24 8 8 0.33
  StrictDuplicateCodeCheck.JavaChecksumGenerator       Line # 132 100% 1.0
2 3 2 2 0.67
 
  (4)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 Oliver Burn
4    //
5    // This library is free software; you can redistribute it and/or
6    // modify it under the terms of the GNU Lesser General Public
7    // License as published by the Free Software Foundation; either
8    // version 2.1 of the License, or (at your option) any later version.
9    //
10    // This library is distributed in the hope that it will be useful,
11    // but WITHOUT ANY WARRANTY; without even the implied warranty of
12    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    // Lesser General Public License for more details.
14    //
15    // You should have received a copy of the GNU Lesser General Public
16    // License along with this library; if not, write to the Free Software
17    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18    ////////////////////////////////////////////////////////////////////////////////
19    package com.puppycrawl.tools.checkstyle.checks.duplicates;
20   
21    import java.io.File;
22    import java.io.IOException;
23    import java.util.Collection;
24    import java.util.Map;
25   
26    import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
27    import com.puppycrawl.tools.checkstyle.api.Utils;
28    import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
29   
30    import org.apache.commons.collections.MultiHashMap;
31    import org.apache.commons.collections.MultiMap;
32    import org.apache.commons.collections.ReferenceMap;
33    import org.apache.commons.logging.Log;
34    import org.apache.commons.logging.LogFactory;
35   
36    /**
37    * Performs a line-by-line comparison of all code lines and reports
38    * duplicate code if a sequence of lines differs only in
39    * indentation. All import statements in Java code are ignored, any
40    * other line - including javadoc, whitespace lines between methods,
41    * etc. - is considered (which is why the check is called
42    * <em>strict</em>).
43    *
44    * @author Lars K&uuml;hne
45    */
 
46    public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
47    {
48    /**
49    * A prime that is used o calculate checksums of lines and blocks of lines.
50    * Important that it's larger than the length of most lines to avoid hash
51    * collisions.
52    *
53    * For a list of primes see
54    * http://www.utm.edu/research/primes/lists/small/1000.txt
55    */
56    private static final int BIG_PRIME = 317;
57   
58    /**
59    * Converts each consecutive block of {@link #mMin} lines of the
60    * original source lines to a checksum that is checked against
61    * to find duplicates.
62    */
 
63    private interface ChecksumGenerator
64    {
65    /**
66    * Convert each block of the original source lines
67    * to a checksum that is checked against to find duplicates
68    *
69    * @param aOriginalLines the original lines as they appear
70    * in the source file
71    *
72    * @return an array of (aOriginalLines.length - mMin + 1) checksums
73    */
74    int[] convertLines(String[] aOriginalLines);
75    }
76   
77   
78    /**
79    * Calculates checksums for text files.
80    */
 
81    private class TextfileChecksumGenerator implements ChecksumGenerator
82    {
83    /** {@inheritDoc} */
 
84  6 toggle public int[] convertLines(String[] aOriginalLines)
85    {
86  6 final int lineCount = aOriginalLines.length;
87  6 final long[] checkSums = new long[lineCount];
88  149 for (int i = 0; i < lineCount; i++) {
89  143 final String line = aOriginalLines[i];
90  143 checkSums[i] = calcChecksum(line);
91    }
92  6 final int retLen = Math.max(0, lineCount - mMin + 1);
93  6 final int[] ret = new int[retLen];
94   
95  117 for (int i = 0; i < retLen; i++) {
96  111 int blockChecksum = 0;
97  111 boolean onlyEmptyLines = true;
98  1029 for (int j = 0; j < mMin; j++) {
99  924 if (aOriginalLines[i + j].length() > 0) {
100  614 onlyEmptyLines = false;
101    }
102  924 final long checksum = checkSums[i + j];
103  924 if (checksum == IGNORE) {
104  6 blockChecksum = IGNORE;
105  6 break;
106    }
107  918 blockChecksum += (j + 1) * BIG_PRIME * checksum;
108    }
109  111 ret[i] = onlyEmptyLines ? IGNORE : blockChecksum;
110    }
111  6 return ret;
112    }
113   
114    /**
115    * Computes a checksum for a a single line of text.
116    * @param aLine the aLine
117    * @return checksum
118    */
 
119  137 toggle protected int calcChecksum(String aLine)
120    {