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 InputFormattedLambdaBodyWrap {
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 = () -> "Hello, world!";
17  
18    private void printNames() {
19      names.forEach(name -> System.out.println(name));
20    }
21  
22    private void runTask() {
23      Runnable runTask =
24          () -> {
25            System.out.println("Starting task");
26            System.out.println("Task completed");
27          };
28    }
29  
30    private void executeNestedLambda() {
31      Function<Function<Integer, Integer>, Integer> applyFunc = f -> f.apply(5);
32      System.out.println(applyFunc.apply(x -> x * x));
33    }
34  
35    private void sumOfSquares() {
36      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
37      int sum = numbers.stream().filter(n -> n % 2 == 1).mapToInt(n -> n * n).sum();
38    }
39  
40    private void showComplexNestedLambda() {
41      // terminology: () - parentheses, [] - brackets, {} - braces
42      // Code below is definitely "unbraced expression" and it is single (but not a single-line),
43      // fact that is parentheses-ed is still definition of single.
44      // Multiple expressions will imply curly braces and `;`.
45      // so case below is ok
46      Function<Integer, Function<Integer, Integer>> createAdder = x -> (y -> x + y);
47    }
48  
49    private void foo() {
50      BiFunction<String, Long, Long> r =
51          (String label, Long value) -> {
52            return value;
53          };
54  
55      java.util.function.Predicate<String> predicate =
56          str -> {
57            return str.isEmpty();
58          };
59  
60      Function<String, BiFunction<String, String, String>> s =
61          (String label) -> {
62            return (a, b) -> {
63              return a + " " + b;
64            };
65          };
66    }
67  }