View Javadoc
1   /*
2   SimplifyBooleanExpression
3   
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.coding.simplifybooleanexpression;
8   
9   /**
10     Contains boolean logic that can be simplified.
11  
12     @author lkuehne
13   */
14  public class InputSimplifyBooleanExpression
15  {
16  
17      public static boolean isOddMillis()
18      {
19          boolean even = System.currentTimeMillis() % 2 == 0;
20  
21          // can be simplified to "if (even)"
22          if (even == true) { // violation
23              return false;
24          }
25          else {
26              return true;
27          }
28          // return can be simplified to "return !even"
29      }
30  
31      public static boolean isOddMillis2()
32      {
33          boolean even = System.currentTimeMillis() % 2 == 0;
34          // can be simplified to "return !even"
35          if (!even)
36              return true;
37          else
38              return false;
39      }
40  
41      public static boolean giveMeTrue()
42      {
43          boolean tt = isOddMillis() || true; // violation
44          boolean ff = isOddMillis() && false; // violation
45          return !false || (true != false); // 2 violations
46      }
47  
48      public void tryToProvokeNPE()
49      {
50          if (true) {
51          }
52          else {
53          }
54  
55          if (true) {
56              return;
57          }
58          else {
59              return;
60          }
61      }
62  
63      public boolean ifNoElse()
64      {
65          if (isOddMillis()) {
66              return true;
67          }
68          return false;
69      }
70  
71      boolean a() {
72          boolean asd = false;
73          boolean dasa = true;
74  
75          if (asd) {
76              return true;
77          } else {
78              return dasa;
79          }
80      }
81  
82      boolean b() {
83          boolean asd = false;
84  
85          if(asd);
86          else;
87  
88          return true;
89      }
90  
91      void testTernaryExpressions() {
92          boolean a = false;
93          boolean b = true;
94          int c = 13;
95          boolean m = c > 1 ? true : false; // violation
96          boolean e = (a == true) // violation
97                  ? c > 1 : false;
98          boolean h = false ? c > 13 : c < 21; // violation
99          boolean f = a == b ? false : c > 1;
100         boolean q = c > 1 ? (c < 15
101                 ? false : b)
102                 : a != b;
103         boolean v = c > 0 ? true :
104                 c < 0 ? false : true; // violation
105         boolean g = (c > 0 ? true : c < 0)
106                 ? false : false; // violation
107         Boolean value = null;
108         boolean temp = value != null ? value : false;
109         temp = true ? a() : b(); // violation
110         int d = false ? 1 : 2; // violation
111         temp = a() ? true : true; // violation
112         temp = value != null ? value : (false);
113     }
114 }