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