View Javadoc
1   /*
2   UnnecessaryParentheses
3   tokens = LAMBDA
4   
5   
6   */
7   
8   package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
9   
10  import java.io.Serializable;
11  import java.util.HashSet;
12  import java.util.Objects;
13  import java.util.function.Function;
14  
15  public class InputUnnecessaryParenthesesLambdas {
16      int foo(int y) {
17          MathOperation case1 = (x) -> x + x; // violation 'Unnecessary paren.* around lambda value'
18          MathOperation case2 = (x) -> { return x + x; }; // violation 'paren.* around lambda value'
19          MathOperation case3 = (int x) -> x + x;
20          MathOperation case4 = x -> x + x;
21          MathOperation2 case5 = (a, b) -> a + b;
22          MathOperation2 case6 = (int a, int b) -> a + b;
23          MathOperation2 case7 = (int a, int b) -> { return a + b; };
24          Objects.requireNonNull(null, () -> "message");
25          call((x) -> x + x); // violation 'Unnecessary parentheses around lambda value'
26          new HashSet<Integer>()
27              .stream()
28              .filter((f) -> f > 0); // violation 'Unnecessary parentheses around lambda value'
29          return y;
30      }
31  
32      static <T> CheckedFunction1<T, T> identity() {
33          return t -> t;
34      }
35  
36      public interface CheckedFunction2<T1, T2, R> extends Lambda<R> {
37          R apply(T1 t1, T2 t2) throws Throwable;
38  
39          default CheckedFunction1<T2, R> apply(T1 t1) {
40              return (T2 t2) -> apply(t1, t2);
41          }
42          @Override
43          default Function1<T1, CheckedFunction1<T2, R>> curried() {
44                      return t1 -> t2 -> apply(t1, t2);
45          }
46          default Function1<T1, CheckedFunction1<T2, R>> curried2() {
47              return (t1) -> (t2) -> apply(t1, t2); // 2 violations
48          }
49          default Function1<T1, CheckedFunction1<T2, R>> curried3() {
50              return (t1) -> t2 -> apply(t1, t2); // violation 'parentheses around lambda value'
51          }
52          default Function1<T1, CheckedFunction1<T2, R>> curried4() {
53              return t1 -> (t2) -> apply(t1, t2); // violation 'parentheses around lambda value'
54          }
55      }
56  
57      private void call(MathOperation o) {
58          o.operation(1);
59      }
60  
61      interface MathOperation {
62          int operation(int a);
63      }
64  
65      interface MathOperation2 {
66          int operation(int a, int b);
67      }
68  
69      interface Lambda<R> extends Serializable {
70          Lambda<?> curried();
71      }
72  
73      public interface Function1<T1, R> extends Lambda<R>, Function<T1, R> {
74          @Override
75          default Function1<T1, R> curried() {
76              return this;
77          }
78      }
79  
80      public interface CheckedFunction1<T1, R> extends Lambda<R> {
81          R apply(T1 t1) throws Throwable;
82  
83          @Override
84          default CheckedFunction1<T1, R> curried() {
85              return this;
86          }
87      }
88  }