Skip to content

Commit a5bde90

Browse files
java-streams-8-api - adding slf4j fixing indentation
1 parent f5ecf03 commit a5bde90

2 files changed

Lines changed: 53 additions & 53 deletions

File tree

‎core-java-8/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<version>3.3.2</version>
4242
</dependency>
4343

44+
<dependency>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-api</artifactId>
47+
<version>${org.slf4j.version}</version>
48+
</dependency>
49+
4450
<!-- test scoped -->
4551

4652
<dependency>

‎core-java-8/src/test/java/com/baeldung/java8/Java8StreamApiTest.java‎

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,43 @@
22

33
import com.baeldung.streamApi.Product;
44
import org.junit.Before;
5-
import org.junit.BeforeClass;
65
import org.junit.Test;
7-
import org.mockito.Mock;
8-
import org.mockito.Mockito;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
98

109
import java.io.BufferedWriter;
11-
import java.io.File;
1210
import java.io.IOException;
1311
import java.nio.charset.Charset;
1412
import java.nio.file.Files;
1513
import java.nio.file.Path;
16-
import java.nio.file.Paths;
1714
import java.util.*;
18-
import java.util.logging.Logger;
1915
import java.util.regex.Pattern;
2016
import java.util.stream.*;
2117

2218
import static org.junit.Assert.*;
23-
import static org.mockito.Matchers.anyString;
24-
import static org.mockito.Mockito.mock;
2519

26-
/**
27-
* Created by Alex Vengr
28-
*/
2920
public class Java8StreamApiTest {
3021

3122
private long counter;
3223

33-
private static Logger log = Logger.getAnonymousLogger();
24+
private static Logger log = LoggerFactory.getLogger(Java8StreamApiTest.class);
3425

3526
private List<Product> productList;
3627

37-
3828
@Before
3929
public void init() {
40-
productList = Arrays.asList(new Product(23, "potatoes"),
41-
new Product(14, "orange"), new Product(13, "lemon"),
42-
new Product(23, "bread"), new Product(13, "sugar"));
30+
productList = Arrays.asList(
31+
new Product(23, "potatoes"), new Product(14, "orange"),
32+
new Product(13, "lemon"), new Product(23, "bread"),
33+
new Product(13, "sugar"));
4334
}
4435

4536
@Test
4637
public void checkPipeline_whenStreamOneElementShorter_thenCorrect() {
4738

4839
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
4940
long size = list.stream().skip(1)
50-
.map(element -> element.substring(0, 3)).count();
41+
.map(element -> element.substring(0, 3)).count();
5142
assertEquals(list.size() - 1, size);
5243
}
5344

@@ -58,18 +49,18 @@ public void checkOrder_whenChangeQuantityOfMethodCalls_thenCorrect() {
5849

5950
counter = 0;
6051
long sizeFirst = list.stream()
61-
.skip(2).map(element -> {
62-
wasCalled();
63-
return element.substring(0, 3);
64-
}).count();
65-
assertEquals( 1, counter);
52+
.skip(2).map(element -> {
53+
wasCalled();
54+
return element.substring(0, 3);
55+
}).count();
56+
assertEquals(1, counter);
6657

6758
counter = 0;
6859
long sizeSecond = list.stream().map(element -> {
6960
wasCalled();
7061
return element.substring(0, 3);
7162
}).skip(2).count();
72-
assertEquals( 3, counter);
63+
assertEquals(3, counter);
7364
}
7465

7566
@Test
@@ -117,7 +108,7 @@ public void createStream_whenCreated_thenCorrect() {
117108
try {
118109
streamOfStrings = Files.lines(path, Charset.forName("UTF-8"));
119110
} catch (IOException e) {
120-
e.printStackTrace();
111+
log.error("Error creating streams from paths {}", path, e.getMessage(), e);
121112
}
122113
assertEquals("a", streamOfStrings.findFirst().get());
123114

@@ -136,13 +127,13 @@ public void runStreamPipeline_whenOrderIsRight_thenCorrect() {
136127

137128
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
138129
Optional<String> stream = list.stream()
139-
.filter(element -> {
140-
log.info("filter() was called");
141-
return element.contains("2");
142-
}).map(element -> {
143-
log.info("map() was called");
144-
return element.toUpperCase();
145-
}).findFirst();
130+
.filter(element -> {
131+
log.info("filter() was called");
132+
return element.contains("2");
133+
}).map(element -> {
134+
log.info("map() was called");
135+
return element.toUpperCase();
136+
}).findFirst();
146137
}
147138

148139
@Test
@@ -155,31 +146,31 @@ public void reduce_whenExpected_thenCorrect() {
155146
assertEquals(16, reducedTwoParams);
156147

157148
int reducedThreeParams = Stream.of(1, 2, 3)
158-
.reduce(10, (a, b) -> a + b, (a, b) -> {
159-
log.info("combiner was called");
160-
return a + b;
161-
});
149+
.reduce(10, (a, b) -> a + b, (a, b) -> {
150+
log.info("combiner was called");
151+
return a + b;
152+
});
162153
assertEquals(16, reducedThreeParams);
163154

164155
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream()
165-
.reduce(10, (a, b) -> a + b, (a, b) -> {
166-
log.info("combiner was called");
167-
return a + b;
168-
});
156+
.reduce(10, (a, b) -> a + b, (a, b) -> {
157+
log.info("combiner was called");
158+
return a + b;
159+
});
169160
assertEquals(36, reducedThreeParamsParallel);
170161
}
171162

172163
@Test
173164
public void collecting_whenAsExpected_thenCorrect() {
174165

175166
List<String> collectorCollection = productList.stream()
176-
.map(Product::getName).collect(Collectors.toList());
167+
.map(Product::getName).collect(Collectors.toList());
177168

178169
assertTrue(collectorCollection instanceof List);
179170
assertEquals(5, collectorCollection.size());
180171

181172
String listToString = productList.stream().map(Product::getName)
182-
.collect(Collectors.joining(", ", "[", "]"));
173+
.collect(Collectors.joining(", ", "[", "]"));
183174

184175
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
185176

@@ -190,32 +181,35 @@ public void collecting_whenAsExpected_thenCorrect() {
190181
assertEquals(86, summingPrice);
191182

192183
IntSummaryStatistics statistics = productList.stream()
193-
.collect(Collectors.summarizingInt(Product::getPrice));
184+
.collect(Collectors.summarizingInt(Product::getPrice));
194185
assertEquals(23, statistics.getMax());
195186

196187
Map<Integer, List<Product>> collectorMapOfLists = productList.stream()
197-
.collect(Collectors.groupingBy(Product::getPrice));
188+
.collect(Collectors.groupingBy(Product::getPrice));
198189
assertEquals(3, collectorMapOfLists.keySet().size());
199190

200191
Map<Boolean, List<Product>> mapPartioned = productList.stream()
201-
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
192+
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
202193
assertEquals(2, mapPartioned.keySet().size());
203194

204195
}
205196

206197
@Test(expected = UnsupportedOperationException.class)
207198
public void collect_whenThrows_thenCorrect() {
208199
Set<Product> unmodifiableSet = productList.stream()
209-
.collect(Collectors.collectingAndThen(Collectors.toSet(),
210-
Collections::unmodifiableSet));
200+
.collect(Collectors.collectingAndThen(Collectors.toSet(),
201+
Collections::unmodifiableSet));
211202
unmodifiableSet.add(new Product(4, "tea"));
212203
}
213204

214205
@Test
215206
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
216207
Collector<Product, ?, LinkedList<Product>> toLinkedList =
217-
Collector.of(LinkedList::new, LinkedList::add,
218-
(first, second) -> { first.addAll(second); return first; });
208+
Collector.of(LinkedList::new, LinkedList::add,
209+
(first, second) -> {
210+
first.addAll(second);
211+
return first;
212+
});
219213

220214
LinkedList<Product> linkedListOfPersons = productList.stream().collect(toLinkedList);
221215
assertTrue(linkedListOfPersons.containsAll(productList));
@@ -226,22 +220,22 @@ public void parallelStream_whenWorks_thenCorrect() {
226220
Stream<Product> streamOfCollection = productList.parallelStream();
227221
boolean isParallel = streamOfCollection.isParallel();
228222
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12)
229-
.anyMatch(price -> price > 200);
223+
.anyMatch(price -> price > 200);
230224
assertTrue(isParallel && haveBigPrice);
231225
}
232226

233227
@Test
234228
public void parallel_whenIsParallel_thenCorrect() {
235229
IntStream intStreamParallel =
236-
IntStream.range(1, 150).parallel().map(element -> element * 34);
230+
IntStream.range(1, 150).parallel().map(element -> element * 34);
237231
boolean isParallel = intStreamParallel.isParallel();
238232
assertTrue(isParallel);
239233
}
240234

241235
@Test
242236
public void parallel_whenIsSequential_thenCorrect() {
243237
IntStream intStreamParallel =
244-
IntStream.range(1, 150).parallel().map(element -> element * 34);
238+
IntStream.range(1, 150).parallel().map(element -> element * 34);
245239
IntStream intStreamSequential = intStreamParallel.sequential();
246240
boolean isParallel = intStreamParallel.isParallel();
247241
assertFalse(isParallel);
@@ -252,13 +246,13 @@ private Path getPath() {
252246
try {
253247
path = Files.createTempFile(null, ".txt");
254248
} catch (IOException e) {
255-
e.printStackTrace();
249+
log.error(e.getMessage());
256250
}
257251

258252
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
259253
writer.write("a\nb\nc");
260254
} catch (IOException e) {
261-
e.printStackTrace();
255+
log.error(e.getMessage());
262256
}
263257
return path;
264258
}

0 commit comments

Comments
 (0)