java-streams-8-api - adding slf4j fixing indentation
This commit is contained in:
parent
f5ecf03890
commit
a5bde90d40
|
@ -41,6 +41,12 @@
|
||||||
<version>3.3.2</version>
|
<version>3.3.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -2,44 +2,35 @@ package com.baeldung.java8;
|
||||||
|
|
||||||
import com.baeldung.streamApi.Product;
|
import com.baeldung.streamApi.Product;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mock;
|
import org.slf4j.Logger;
|
||||||
import org.mockito.Mockito;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.Matchers.anyString;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Alex Vengr
|
|
||||||
*/
|
|
||||||
public class Java8StreamApiTest {
|
public class Java8StreamApiTest {
|
||||||
|
|
||||||
private long counter;
|
private long counter;
|
||||||
|
|
||||||
private static Logger log = Logger.getAnonymousLogger();
|
private static Logger log = LoggerFactory.getLogger(Java8StreamApiTest.class);
|
||||||
|
|
||||||
private List<Product> productList;
|
private List<Product> productList;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
productList = Arrays.asList(new Product(23, "potatoes"),
|
productList = Arrays.asList(
|
||||||
new Product(14, "orange"), new Product(13, "lemon"),
|
new Product(23, "potatoes"), new Product(14, "orange"),
|
||||||
new Product(23, "bread"), new Product(13, "sugar"));
|
new Product(13, "lemon"), new Product(23, "bread"),
|
||||||
|
new Product(13, "sugar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -47,7 +38,7 @@ public class Java8StreamApiTest {
|
||||||
|
|
||||||
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
||||||
long size = list.stream().skip(1)
|
long size = list.stream().skip(1)
|
||||||
.map(element -> element.substring(0, 3)).count();
|
.map(element -> element.substring(0, 3)).count();
|
||||||
assertEquals(list.size() - 1, size);
|
assertEquals(list.size() - 1, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,18 +49,18 @@ public class Java8StreamApiTest {
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
long sizeFirst = list.stream()
|
long sizeFirst = list.stream()
|
||||||
.skip(2).map(element -> {
|
.skip(2).map(element -> {
|
||||||
wasCalled();
|
wasCalled();
|
||||||
return element.substring(0, 3);
|
return element.substring(0, 3);
|
||||||
}).count();
|
}).count();
|
||||||
assertEquals( 1, counter);
|
assertEquals(1, counter);
|
||||||
|
|
||||||
counter = 0;
|
counter = 0;
|
||||||
long sizeSecond = list.stream().map(element -> {
|
long sizeSecond = list.stream().map(element -> {
|
||||||
wasCalled();
|
wasCalled();
|
||||||
return element.substring(0, 3);
|
return element.substring(0, 3);
|
||||||
}).skip(2).count();
|
}).skip(2).count();
|
||||||
assertEquals( 3, counter);
|
assertEquals(3, counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -117,7 +108,7 @@ public class Java8StreamApiTest {
|
||||||
try {
|
try {
|
||||||
streamOfStrings = Files.lines(path, Charset.forName("UTF-8"));
|
streamOfStrings = Files.lines(path, Charset.forName("UTF-8"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error("Error creating streams from paths {}", path, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
assertEquals("a", streamOfStrings.findFirst().get());
|
assertEquals("a", streamOfStrings.findFirst().get());
|
||||||
|
|
||||||
|
@ -136,13 +127,13 @@ public class Java8StreamApiTest {
|
||||||
|
|
||||||
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
List<String> list = Arrays.asList("abc1", "abc2", "abc3");
|
||||||
Optional<String> stream = list.stream()
|
Optional<String> stream = list.stream()
|
||||||
.filter(element -> {
|
.filter(element -> {
|
||||||
log.info("filter() was called");
|
log.info("filter() was called");
|
||||||
return element.contains("2");
|
return element.contains("2");
|
||||||
}).map(element -> {
|
}).map(element -> {
|
||||||
log.info("map() was called");
|
log.info("map() was called");
|
||||||
return element.toUpperCase();
|
return element.toUpperCase();
|
||||||
}).findFirst();
|
}).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -155,17 +146,17 @@ public class Java8StreamApiTest {
|
||||||
assertEquals(16, reducedTwoParams);
|
assertEquals(16, reducedTwoParams);
|
||||||
|
|
||||||
int reducedThreeParams = Stream.of(1, 2, 3)
|
int reducedThreeParams = Stream.of(1, 2, 3)
|
||||||
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
||||||
log.info("combiner was called");
|
log.info("combiner was called");
|
||||||
return a + b;
|
return a + b;
|
||||||
});
|
});
|
||||||
assertEquals(16, reducedThreeParams);
|
assertEquals(16, reducedThreeParams);
|
||||||
|
|
||||||
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream()
|
int reducedThreeParamsParallel = Arrays.asList(1, 2, 3).parallelStream()
|
||||||
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
.reduce(10, (a, b) -> a + b, (a, b) -> {
|
||||||
log.info("combiner was called");
|
log.info("combiner was called");
|
||||||
return a + b;
|
return a + b;
|
||||||
});
|
});
|
||||||
assertEquals(36, reducedThreeParamsParallel);
|
assertEquals(36, reducedThreeParamsParallel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,13 +164,13 @@ public class Java8StreamApiTest {
|
||||||
public void collecting_whenAsExpected_thenCorrect() {
|
public void collecting_whenAsExpected_thenCorrect() {
|
||||||
|
|
||||||
List<String> collectorCollection = productList.stream()
|
List<String> collectorCollection = productList.stream()
|
||||||
.map(Product::getName).collect(Collectors.toList());
|
.map(Product::getName).collect(Collectors.toList());
|
||||||
|
|
||||||
assertTrue(collectorCollection instanceof List);
|
assertTrue(collectorCollection instanceof List);
|
||||||
assertEquals(5, collectorCollection.size());
|
assertEquals(5, collectorCollection.size());
|
||||||
|
|
||||||
String listToString = productList.stream().map(Product::getName)
|
String listToString = productList.stream().map(Product::getName)
|
||||||
.collect(Collectors.joining(", ", "[", "]"));
|
.collect(Collectors.joining(", ", "[", "]"));
|
||||||
|
|
||||||
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
|
assertTrue(listToString.contains(",") && listToString.contains("[") && listToString.contains("]"));
|
||||||
|
|
||||||
|
@ -190,15 +181,15 @@ public class Java8StreamApiTest {
|
||||||
assertEquals(86, summingPrice);
|
assertEquals(86, summingPrice);
|
||||||
|
|
||||||
IntSummaryStatistics statistics = productList.stream()
|
IntSummaryStatistics statistics = productList.stream()
|
||||||
.collect(Collectors.summarizingInt(Product::getPrice));
|
.collect(Collectors.summarizingInt(Product::getPrice));
|
||||||
assertEquals(23, statistics.getMax());
|
assertEquals(23, statistics.getMax());
|
||||||
|
|
||||||
Map<Integer, List<Product>> collectorMapOfLists = productList.stream()
|
Map<Integer, List<Product>> collectorMapOfLists = productList.stream()
|
||||||
.collect(Collectors.groupingBy(Product::getPrice));
|
.collect(Collectors.groupingBy(Product::getPrice));
|
||||||
assertEquals(3, collectorMapOfLists.keySet().size());
|
assertEquals(3, collectorMapOfLists.keySet().size());
|
||||||
|
|
||||||
Map<Boolean, List<Product>> mapPartioned = productList.stream()
|
Map<Boolean, List<Product>> mapPartioned = productList.stream()
|
||||||
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
|
.collect(Collectors.partitioningBy(element -> element.getPrice() > 15));
|
||||||
assertEquals(2, mapPartioned.keySet().size());
|
assertEquals(2, mapPartioned.keySet().size());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -206,16 +197,19 @@ public class Java8StreamApiTest {
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
public void collect_whenThrows_thenCorrect() {
|
public void collect_whenThrows_thenCorrect() {
|
||||||
Set<Product> unmodifiableSet = productList.stream()
|
Set<Product> unmodifiableSet = productList.stream()
|
||||||
.collect(Collectors.collectingAndThen(Collectors.toSet(),
|
.collect(Collectors.collectingAndThen(Collectors.toSet(),
|
||||||
Collections::unmodifiableSet));
|
Collections::unmodifiableSet));
|
||||||
unmodifiableSet.add(new Product(4, "tea"));
|
unmodifiableSet.add(new Product(4, "tea"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
|
public void customCollector_whenResultContainsAllElementsFrSource_thenCorrect() {
|
||||||
Collector<Product, ?, LinkedList<Product>> toLinkedList =
|
Collector<Product, ?, LinkedList<Product>> toLinkedList =
|
||||||
Collector.of(LinkedList::new, LinkedList::add,
|
Collector.of(LinkedList::new, LinkedList::add,
|
||||||
(first, second) -> { first.addAll(second); return first; });
|
(first, second) -> {
|
||||||
|
first.addAll(second);
|
||||||
|
return first;
|
||||||
|
});
|
||||||
|
|
||||||
LinkedList<Product> linkedListOfPersons = productList.stream().collect(toLinkedList);
|
LinkedList<Product> linkedListOfPersons = productList.stream().collect(toLinkedList);
|
||||||
assertTrue(linkedListOfPersons.containsAll(productList));
|
assertTrue(linkedListOfPersons.containsAll(productList));
|
||||||
|
@ -226,14 +220,14 @@ public class Java8StreamApiTest {
|
||||||
Stream<Product> streamOfCollection = productList.parallelStream();
|
Stream<Product> streamOfCollection = productList.parallelStream();
|
||||||
boolean isParallel = streamOfCollection.isParallel();
|
boolean isParallel = streamOfCollection.isParallel();
|
||||||
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12)
|
boolean haveBigPrice = streamOfCollection.map(product -> product.getPrice() * 12)
|
||||||
.anyMatch(price -> price > 200);
|
.anyMatch(price -> price > 200);
|
||||||
assertTrue(isParallel && haveBigPrice);
|
assertTrue(isParallel && haveBigPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parallel_whenIsParallel_thenCorrect() {
|
public void parallel_whenIsParallel_thenCorrect() {
|
||||||
IntStream intStreamParallel =
|
IntStream intStreamParallel =
|
||||||
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
||||||
boolean isParallel = intStreamParallel.isParallel();
|
boolean isParallel = intStreamParallel.isParallel();
|
||||||
assertTrue(isParallel);
|
assertTrue(isParallel);
|
||||||
}
|
}
|
||||||
|
@ -241,7 +235,7 @@ public class Java8StreamApiTest {
|
||||||
@Test
|
@Test
|
||||||
public void parallel_whenIsSequential_thenCorrect() {
|
public void parallel_whenIsSequential_thenCorrect() {
|
||||||
IntStream intStreamParallel =
|
IntStream intStreamParallel =
|
||||||
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
IntStream.range(1, 150).parallel().map(element -> element * 34);
|
||||||
IntStream intStreamSequential = intStreamParallel.sequential();
|
IntStream intStreamSequential = intStreamParallel.sequential();
|
||||||
boolean isParallel = intStreamParallel.isParallel();
|
boolean isParallel = intStreamParallel.isParallel();
|
||||||
assertFalse(isParallel);
|
assertFalse(isParallel);
|
||||||
|
@ -252,13 +246,13 @@ public class Java8StreamApiTest {
|
||||||
try {
|
try {
|
||||||
path = Files.createTempFile(null, ".txt");
|
path = Files.createTempFile(null, ".txt");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
|
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
|
||||||
writer.write("a\nb\nc");
|
writer.write("a\nb\nc");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue