Merge pull request #5813 from earth001/BAEL-2333a
BAEL-2333 Sample code
This commit is contained in:
commit
3bec6f7eff
|
@ -1,5 +1,5 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-streams</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
|
@ -74,6 +74,11 @@
|
|||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>pl.touk</groupId>
|
||||
<artifactId>throwing-function</artifactId>
|
||||
<version>${throwing-function.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -108,8 +113,9 @@
|
|||
<protonpack.version>1.15</protonpack.version>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
<joda.version>2.10</joda.version>
|
||||
<throwing-function.version>1.3</throwing-function.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<asspectj.version>1.8.9</asspectj.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.stream.filter;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class Customer {
|
||||
private String name;
|
||||
private int points;
|
||||
private String profilePhotoUrl;
|
||||
|
||||
public Customer(String name, int points) {
|
||||
this(name, points, "");
|
||||
}
|
||||
|
||||
public Customer(String name, int points, String profilePhotoUrl) {
|
||||
this.name = name;
|
||||
this.points = points;
|
||||
this.profilePhotoUrl = profilePhotoUrl;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPoints() {
|
||||
return points;
|
||||
}
|
||||
|
||||
public boolean hasOver(int points) {
|
||||
return this.points > points;
|
||||
}
|
||||
|
||||
public boolean hasOverThousandPoints() {
|
||||
return this.points > 100;
|
||||
}
|
||||
|
||||
public boolean hasValidProfilePhoto() throws IOException {
|
||||
URL url = new URL(this.profilePhotoUrl);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
}
|
||||
|
||||
public boolean hasValidProfilePhotoWithoutCheckedException() {
|
||||
try {
|
||||
URL url = new URL(this.profilePhotoUrl);
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
package com.baeldung.stream.filter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pl.touk.throwing.ThrowingPredicate;
|
||||
import pl.touk.throwing.exception.WrappedException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
||||
|
||||
public class StreamFilterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterByPoints_thenGetTwo() {
|
||||
Customer john = new Customer("John P.", 15);
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1);
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
List<Customer> customersWithMoreThan100Points = customers
|
||||
.stream()
|
||||
.filter(c -> c.getPoints() > 100)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(customersWithMoreThan100Points).hasSize(2);
|
||||
assertThat(customersWithMoreThan100Points).contains(sarah, charles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterByPointsAndName_thenGetOne() {
|
||||
Customer john = new Customer("John P.", 15);
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1);
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
List<Customer> charlesWithMoreThan100Points = customers
|
||||
.stream()
|
||||
.filter(c -> c.getPoints() > 100 && c
|
||||
.getName()
|
||||
.startsWith("Charles"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(charlesWithMoreThan100Points).hasSize(1);
|
||||
assertThat(charlesWithMoreThan100Points).contains(charles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterByMethodReference_thenGetTwo() {
|
||||
Customer john = new Customer("John P.", 15);
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1);
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
List<Customer> customersWithMoreThan100Points = customers
|
||||
.stream()
|
||||
.filter(Customer::hasOverThousandPoints)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(customersWithMoreThan100Points).hasSize(2);
|
||||
assertThat(customersWithMoreThan100Points).contains(sarah, charles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomersWithOptional_whenFilterBy100Points_thenGetTwo() {
|
||||
Optional<Customer> john = Optional.of(new Customer("John P.", 15));
|
||||
Optional<Customer> sarah = Optional.of(new Customer("Sarah M.", 200));
|
||||
Optional<Customer> mary = Optional.of(new Customer("Mary T.", 300));
|
||||
List<Optional<Customer>> customers = Arrays.asList(john, sarah, Optional.empty(), mary, Optional.empty());
|
||||
|
||||
List<Customer> customersWithMoreThan100Points = customers
|
||||
.stream()
|
||||
.flatMap(c -> c
|
||||
.map(Stream::of)
|
||||
.orElseGet(Stream::empty))
|
||||
.filter(Customer::hasOverThousandPoints)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(customersWithMoreThan100Points).hasSize(2);
|
||||
assertThat(customersWithMoreThan100Points).contains(sarah.get(), mary.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterWithCustomHandling_thenThrowException() {
|
||||
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
assertThatThrownBy(() -> customers
|
||||
.stream()
|
||||
.filter(Customer::hasValidProfilePhotoWithoutCheckedException)
|
||||
.count()).isInstanceOf(RuntimeException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterWithThrowingFunction_thenThrowException() {
|
||||
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
assertThatThrownBy(() -> customers
|
||||
.stream()
|
||||
.filter((ThrowingPredicate.unchecked(Customer::hasValidProfilePhoto)))
|
||||
.collect(Collectors.toList())).isInstanceOf(WrappedException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
|
||||
Customer john = new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e");
|
||||
Customer sarah = new Customer("Sarah M.", 200);
|
||||
Customer charles = new Customer("Charles B.", 150);
|
||||
Customer mary = new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e");
|
||||
List<Customer> customers = Arrays.asList(john, sarah, charles, mary);
|
||||
|
||||
List<Customer> customersWithValidProfilePhoto = customers
|
||||
.stream()
|
||||
.filter(c -> {
|
||||
try {
|
||||
return c.hasValidProfilePhoto();
|
||||
} catch (IOException e) {
|
||||
//handle exception
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(customersWithValidProfilePhoto).hasSize(2);
|
||||
assertThat(customersWithValidProfilePhoto).contains(john, mary);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowException() {
|
||||
List<Customer> customers = Arrays.asList(new Customer("John P.", 15, "https://images.unsplash.com/photo-1543320485-d0d5a49c2b2e"), new Customer("Sarah M.", 200), new Customer("Charles B.", 150),
|
||||
new Customer("Mary T.", 1, "https://images.unsplash.com/photo-1543297057-25167dfc180e"));
|
||||
|
||||
assertThatThrownBy(() -> customers
|
||||
.stream()
|
||||
.filter(c -> {
|
||||
try {
|
||||
return c.hasValidProfilePhoto();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.collect(Collectors.toList())).isInstanceOf(RuntimeException.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue