View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule451wheretobreak;
2   
3   import java.util.Arrays;
4   import java.util.List;
5   import java.util.function.BiFunction;
6   import java.util.function.Function;
7   import java.util.function.Supplier;
8   
9   /** Some javdoc. */
10  public class InputLambdaBodyWrap {
11  
12    private List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
13  
14    private BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
15  
16    private Supplier<String> getMessage =
17        () ->
18  
19            "Hello, world!";
20  
21    private void printNames() {
22      names.forEach(name -> System.out.println(name));
23    }
24  
25    private void runTask() {
26      Runnable runTask = ()
27          -> {
28            System.out.println("Starting task");
29            System.out.println("Task completed");
30          };
31    }
32  
33    private void executeNestedLambda() {
34      Function<Function<Integer, Integer>, Integer> applyFunc = f -> f.apply(5);
35      System.out.println(
36          applyFunc.apply(
37              x ->
38                  x * x));
39    }
40  
41    private void sumOfSquares() {
42      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
43      int sum =
44          numbers.stream()
45              .filter(n -> n % 2 == 1)
46              .mapToInt(
47                  n ->
48                      n * n)
49              .sum();
50    }
51  
52    private void showComplexNestedLambda() {
53      // terminology: () - parentheses, [] - brackets, {} - braces
54      // Code below is definitely "unbraced expression" and it is single (but not a single-line),
55      // fact that is parentheses-ed is still definition of single.
56      // Multiple expressions will imply curly braces and `;`.
57      // so case below is ok
58      Function<Integer, Function<Integer, Integer>> createAdder =
59          x ->
60              (y ->
61                  x + y);
62    }
63  
64    private void foo() {
65      BiFunction<String, Long, Long> r =
66          (String label, Long value) -> {
67            return value;
68          };
69  
70      // violation 3 lines below ''{' at column 11 should be on the previous line.'
71      java.util.function.Predicate<String> predicate = str
72          ->
73            {
74              return str.isEmpty();
75            };
76  
77      // violation 3 lines below ''{' at column 11 should be on the previous line.'
78      Function<String, BiFunction<String, String, String>> s =
79          (String label) ->
80            {
81              return (a, b) ->
82                {
83                  return a + " " + b;
84                };
85            };
86      // violation 4 lines above ''{' at column 15 should be on the previous line.'
87    }
88  }