View Javadoc
1   /*
2   ReturnCount
3   max = 1
4   maxForVoid = (default)1
5   format = (default)^equals$
6   tokens = (default)CTOR_DEF, METHOD_DEF, LAMBDA
7   
8   
9   */
10  
11  package com.puppycrawl.tools.checkstyle.checks.coding.returncount;
12  
13  import java.lang.Integer;
14  import java.util.Optional;
15  import java.util.concurrent.Callable;
16  import java.util.function.Supplier;
17  
18  
19  public class InputReturnCountLambda {
20  
21      Runnable fieldWithOneReturnInLambda = () -> {
22          return;
23      };
24  
25      Callable<Integer> fieldWithTwoReturnInLambda = () -> { // violation 'Return count is 2'
26          if (hashCode() == 0) return 0;
27          else return 1;
28      };
29  
30      Optional<Integer> methodWithOneReturnInLambda() {
31          return Optional.of(hashCode()).filter(i -> {
32              return i > 0;
33          });
34      }
35  
36      Optional<Integer> methodWithTwoReturnInLambda() {
37          return Optional.of(hashCode()).filter(i -> { // violation 'Return count is 2'
38              if (i > 0) return true;
39              else return false;
40          });
41      }
42  
43      Optional<Object> methodWithThreeReturnInLambda(int number) {
44          return Optional.of(number).map(i -> { // violation 'Return count is 3'
45              if (i == 42) return true;
46              else if (i == 7) return true;
47              else return false;
48          });
49      }
50  
51      int methodWithTwoReturnWithLambdas(final int number) { // violation 'Return count is 2'
52          if (hashCode() > 0) {
53              new Thread(
54                  () -> {
55                  }
56              ).start();
57              return number;
58          } else {
59              return Optional.of(hashCode()).orElseGet(() -> { // violation 'Return count is 2'
60                  if (number > 0) return number;
61                  else return 0;
62              });
63          }
64      }
65  
66      Supplier<Supplier<Integer>> methodWithOneReturnPerLambda() {
67          return () -> {
68              return () -> {
69                  return 1;
70              };
71          };
72      }
73  }