KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > testcases > fixes > CyclomaticComplexityRuleFixTestCase


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22 */

23 package org.hammurapi.inspectors.testcases.fixes;
24
25 /**
26  * CyclomaticComplexityRule
27  * @author Pavel Vlasov
28  * @version $Revision: 1.1 $
29  */

30 public class CyclomaticComplexityRuleFixTestCase {
31
32     private static org.apache.log4j.Logger logger =
33         org.apache.log4j.Logger.getRootLogger();
34
35     private static final int INT_ZERO = 0;
36     private static final int INT_1 = 1;
37     private static final int NEG_INT_1 = -INT_1;
38     private static final int INT_2 = 2;
39     private static final int NEG_INT_2 = -INT_2;
40     private static final int INT_3 = 3;
41     private static final int NEG_INT_3 = -INT_3;
42
43     private static final String JavaDoc CONST_SPACE = " ";
44     private static final char CONST_A = 'A';
45     private static final char CHAR_B = 'B';
46
47     // --- FIX ---
48

49     /* Split methods with complex logic if possible! */
50     /** Java doc automaticaly generated by Hammurapi */
51     public int complexMethod(final String JavaDoc strToProc, final boolean firstType) {
52         if (firstType) {
53             return complexMethodFT(strToProc);
54         } else {
55             return complexMethodNFT(strToProc);
56         }
57     }
58
59     private int complexMethodFT(final String JavaDoc strToProc) {
60         if (strToProc == null) {
61             return INT_1;
62         } else if (strToProc.length() == INT_ZERO) {
63             return INT_2;
64         } else if (
65             strToProc.length() == INT_1
66                 && strToProc.equalsIgnoreCase(CONST_SPACE)) {
67             return INT_3;
68         } else {
69             int retVal = INT_ZERO;
70             for (int i = INT_ZERO; i < strToProc.length(); i++) {
71                 if (strToProc.charAt(i) == CONST_A || i % INT_3 == INT_2) {
72                     retVal++;
73                 } else {
74                     retVal += INT_2;
75                 }
76             }
77             return retVal;
78         }
79     }
80
81     private int complexMethodNFT(final String JavaDoc strToProc) {
82         if (strToProc == null) {
83             return NEG_INT_1;
84         } else if (strToProc.length() == INT_ZERO) {
85             return NEG_INT_2;
86         } else if (
87             strToProc.length() == INT_1
88                 && strToProc.equalsIgnoreCase(CONST_SPACE)) {
89             return NEG_INT_3;
90         } else {
91             int retVal = INT_ZERO;
92             for (int i = INT_ZERO; i < strToProc.length(); i++) {
93                 if (strToProc.charAt(i) == CONST_A || i % INT_3 == INT_2) {
94                     retVal--;
95                 } else if (
96                     strToProc.charAt(i) == CHAR_B || i % INT_3 == INT_2) {
97                     retVal -= INT_2;
98                 } else {
99                     retVal -= INT_3;
100                 }
101             }
102             return retVal;
103         }
104     }
105
106     /* Take complex logic into util classes if possible! */
107     /** Java doc automaticaly generated by Hammurapi */
108     public static int tooComplexClassMethod(final int valToProc) {
109         return CyclomaticComplexityRule2FixTestCase.
110             tooComplexClassMethod(valToProc);
111     }
112     //--- END FIX ---
113
}
114
115
Popular Tags