View Javadoc
1   //a comment //indent:0 exp:0
2   package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0
3   
4   import java.util.ArrayList; //indent:0 exp:0
5   import java.util.List; //indent:0 exp:0
6   import java.util.Map; //indent:0 exp:0
7   import java.util.StringJoiner; //indent:0 exp:0
8   import java.util.stream.Collector; //indent:0 exp:0
9   import java.util.stream.Collectors; //indent:0 exp:0
10  import java.util.stream.IntStream; //indent:0 exp:0
11  import java.util.stream.Stream; //indent:0 exp:0
12  
13  public class InputIndentationLambda1 { //indent:0 exp:0
14    private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(InputIndentationLambda1.class.getName()); //indent:2 exp:2
15    interface Printer //indent:2 exp:2
16    { //indent:2 exp:2
17      void print(String s); //indent:4 exp:4
18    } //indent:2 exp:2
19  
20    class LongTypeName { //indent:2 exp:2
21    } //indent:2 exp:2
22  
23    interface SomeInterface { //indent:2 exp:2
24      void someFunction(LongTypeName arg); //indent:4 exp:4
25    } //indent:2 exp:2
26  
27    void function1(Runnable x) { //indent:2 exp:2
28      Runnable r1 = () -> { //indent:4 exp:4
29        x.run(); //indent:6 exp:6
30      }; //indent:4 exp:4
31  
32      Runnable r2 = () -> { x.run(); }; //indent:4 exp:4
33  
34      Runnable r3 = () -> //indent:4 exp:4
35          x.run(); //indent:8 exp:8
36  
37      Runnable r4 = () -> x.run(); //indent:4 exp:4
38  
39      Printer r5 = s -> LOG.info(s); //indent:4 exp:4
40  
41      Printer r6 = s -> String.CASE_INSENSITIVE_ORDER //indent:4 exp:4
42          .equals(s); //indent:8 exp:8
43  
44      Runnable r7 = () //indent:4 exp:4
45          -> //indent:8 exp:8
46       { //indent:5 exp:4 warn
47       }; //indent:5 exp:4 warn
48  
49      Runnable r8 = //indent:4 exp:4
50           () //indent:9 exp:8 warn
51             -> //indent:11 exp:12 warn
52           {}; //indent:9 exp:8 warn
53  
54      Runnable r9 = //indent:4 exp:4
55          () //indent:8 exp:8
56              -> //indent:12 exp:12
57          {}; //indent:8 exp:8
58  
59      Object o = new Thread(() -> { //indent:4 exp:4
60        x.run(); //indent:6 exp:6
61      }); //indent:4 exp:4
62  
63      Runnable r01 = () -> { //indent:4 exp:4
64         x.run(); //indent:7 exp:6 warn
65       }; //indent:5 exp:4 warn
66  
67      Runnable r11 = //indent:4 exp:4
68          () -> { //indent:8 exp:8
69            x.run(); //indent:10 exp:10
70          }; //indent:8 exp:8
71  
72      Runnable r21 = //indent:4 exp:4
73          () -> { x.run(); }; //indent:8 exp:8
74  
75      Runnable r31 = //indent:4 exp:4
76          () -> x //indent:8 exp:8
77              .run(); //indent:12 exp:12
78  
79      Runnable r41 = //indent:4 exp:4
80          () -> x.run(); //indent:8 exp:8
81  
82      Printer r51 = //indent:4 exp:4
83          s -> LOG.info(s); //indent:8 exp:8
84  
85      Printer r61 = //indent:4 exp:4
86          s -> String.CASE_INSENSITIVE_ORDER //indent:8 exp:8
87              .equals(s); //indent:12 exp:12
88  
89      Object o1 = new Thread( //indent:4 exp:4
90          () -> { //indent:8 exp:8
91            x.run(); //indent:10 exp:10
92          }); //indent:8 exp:8
93  
94      Object o2 = new Thread(() -> { //indent:4 exp:4
95        x.run(); //indent:6 exp:6
96      }).toString(); //indent:4 exp:4
97  
98      SomeInterface i1 = (LongTypeName //indent:4 exp:4
99          arg) -> { //indent:8 exp:8
100       LOG.info(arg.toString()); //indent:6 exp:6
101     }; //indent:4 exp:4
102 
103     Printer[] manyRunnable = new Printer[]{ //indent:4 exp:4
104         s -> LOG.info(s), //indent:8 exp:6,8
105         s -> { LOG.info(s); }, //indent:8 exp:6,8
106         s -> LOG //indent:8 exp:6,8
107             .info(s), //indent:12 exp:12
108         s -> { //indent:8 exp:6,8
109           LOG.info(s); //indent:10 exp:10
110         }, //indent:8 exp:8
111     }; //indent:4 exp:4
112   } //indent:2 exp:2
113 
114   void function3(Runnable x) { //indent:2 exp:2
115     function1(() -> { //indent:4 exp:4
116       x.run(); //indent:6 exp:6
117     }); //indent:4 exp:4
118   } //indent:2 exp:2
119 
120   class Person { //indent:2 exp:2
121     String name; //indent:4 exp:4
122     int age; //indent:4 exp:4
123     Person(String name, int age) { //indent:4 exp:4
124     } //indent:4 exp:4
125   } //indent:2 exp:2
126 
127   class Foo { //indent:2 exp:2
128     String name; //indent:4 exp:4
129     List<Bar> bars = new ArrayList<>(); //indent:4 exp:4
130 
131     Foo(String name) { //indent:4 exp:4
132       this.name = name; //indent:6 exp:6
133     } //indent:4 exp:4
134   } //indent:2 exp:2
135 
136   class Bar { //indent:2 exp:2
137     String name; //indent:4 exp:4
138     Bar(String name) { //indent:4 exp:4
139       this.name = name; //indent:6 exp:6
140     } //indent:4 exp:4
141   } //indent:2 exp:2
142 
143   public void f() { //indent:2 exp:2
144     Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4
145         .map(s -> { //indent:8 exp:8
146           LOG.info("map: " + s); //indent:10 exp:10
147           return s.toUpperCase(java.util.Locale.getDefault()); //indent:10 exp:10
148         }) //indent:8 exp:8
149         .anyMatch(s -> { //indent:8 exp:8
150           LOG.info("anyMatch: " + s); //indent:10 exp:10
151           return s.startsWith("A"); //indent:10 exp:10
152         }); //indent:8 exp:8
153 
154     List<Person> persons = null; //indent:4 exp:4
155 
156     Map<Integer, List<Person>> personsByAge = persons //indent:4 exp:4
157         .stream() //indent:8 exp:8
158         .collect(Collectors.groupingBy(p -> p.age)); //indent:8 exp:8
159 
160     personsByAge //indent:4 exp:4
161         .forEach((age, p) -> LOG.info("age %s: %s\n")); //indent:8 exp:8
162 
163     Collector<Person, StringJoiner, String> personNameCollector = //indent:4 exp:4
164         Collector.of( //indent:8 exp:8
165             () -> new StringJoiner(" | "), //indent:12 exp:12
166             (j, p) -> j.add(p.name.toUpperCase(java.util.Locale.getDefault())), //indent:12 exp:12
167             (j1, j2) -> j1.merge(j2), //indent:12 exp:12
168             StringJoiner::toString); //indent:12 exp:12
169 
170     List<Foo> foos = new ArrayList<>(); //indent:4 exp:4
171 
172     foos.forEach(f -> //indent:4 exp:4
173         IntStream //indent:8 exp:8
174             .range(1, 4) //indent:12 exp:12
175             .forEach(i -> f.bars.add(new Bar("Bar" + i + " <- " + f.name)))); //indent:12 exp:12
176 
177     Stream.of("d2", "a2", "b1", "b3", "c") //indent:4 exp:4
178         .filter(s -> { //indent:8 exp:8
179          LOG.info(("filter: " + s)); //indent:9 exp:10 warn
180            return s.startsWith("a"); //indent:11 exp:10 warn
181         }) //indent:8 exp:8
182         .map(s -> { //indent:8 exp:8
183           LOG.info("map: " + s); //indent:10 exp:10
184           return s.toUpperCase(java.util.Locale.getDefault()); //indent:10 exp:10
185        }) //indent:7 exp:8 warn
186         .forEach(s -> //indent:8 exp:8
187             LOG.info("forEach: " + s)); //indent:12 exp:12
188 
189     IntStream.range(1, 4) //indent:4 exp:4
190         .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8
191         .peek(f -> IntStream.range(1, 4) //indent:8 exp:8
192             .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12
193             .forEach(f.bars::add)) //indent:12 exp:12
194         .flatMap(f -> f.bars.stream()) //indent:8 exp:8
195         .forEach(b -> LOG.info(b.name)); //indent:8 exp:8
196 
197     IntStream.range(1, 4) //indent:4 exp:4
198         .mapToObj(i -> new Foo("Foo" + i)) //indent:8 exp:8
199         .peek(f -> IntStream.range(1, 4) //indent:8 exp:8
200             .mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)) //indent:12 exp:12
201             .forEach(f.bars::add)) //indent:12 exp:12
202         .flatMap(f -> f.bars.stream()) //indent:8 exp:8
203         .forEach(b -> LOG.info(b.name)); //indent:8 exp:8
204   } //indent:2 exp:2
205 
206   Runnable r2r(Runnable x) { //indent:2 exp:2
207     return x; //indent:4 exp:4
208   } //indent:2 exp:2
209 
210   void function2(Runnable x) { //indent:2 exp:2
211     Runnable r0 = r2r(() -> { //indent:4 exp:4
212       int i = 1; //indent:6 exp:6
213     }); //indent:4 exp:4
214 
215     Runnable r1 = r2r(() -> { //indent:4 exp:4
216           int i = 1; //indent:10 exp:10
217         } //indent:8 exp:8
218     ); //indent:4 exp:4
219 
220     Runnable r2 = r2r(r2r(() -> { //indent:4 exp:4
221               int i = 1; //indent:14 exp:14
222             } //indent:12 exp:12
223         ) //indent:8 exp:8
224     ); //indent:4 exp:4
225   } //indent:2 exp:2
226 } //indent:0 exp:0