Skip to content

Commit 8f9f3bd

Browse files
Egima profilepivovarit
authored andcommitted
Added new code snippets to OptionalTest to further demo filter API (eugenp#881)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 * standardized exception based tests * fixed exception based tests * removed redundant files * added network interface project * used UUID other than timestamps * fixed network interface tests * removed filetest change * made changes to NIO2 FileTest names * added project for asyncronous channel apis * added project for NIO2 advanced filesystems APIS * merge conflicts * merged changes to asyncfiletest with future get API * removed while loops from async client and server * added project for java8 optional * fixed merge conflicts in spring-core * fixed optional * fixed optional * fixed asyncechotest * shifted optional to own package * made additional tests to demo filter API
1 parent f621b66 commit 8f9f3bd

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.optional;
2+
3+
public class Modem {
4+
private Double price;
5+
6+
public Modem(Double price) {
7+
this.price = price;
8+
}
9+
10+
public Double getPrice() {
11+
return price;
12+
}
13+
}

core-java/src/main/java/com/baeldung/java_8_features/Person.java renamed to core-java/src/main/java/com/baeldung/optional/Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.java_8_features;
1+
package com.baeldung.optional;
22

33
import java.util.Optional;
44

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ public void teardown() throws IOException {
3333
client.stop();
3434
}
3535

36-
}
36+
}

‎core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import org.junit.Test;
1313

14-
import com.baeldung.java_8_features.Person;
14+
import com.baeldung.optional.Person;
15+
import com.baeldung.optional.Modem;
1516

1617
public class OptionalTest {
1718
// creating Optional
@@ -95,7 +96,36 @@ public void whenOptionalFilterWorks_thenCorrect() {
9596
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
9697
assertFalse(is2017);
9798
}
99+
@Test
100+
public void whenFiltersWithoutOptional_thenCorrect() {
101+
assertTrue(priceIsInRange1(new Modem(10.0)));
102+
assertFalse(priceIsInRange1(new Modem(9.9)));
103+
assertFalse(priceIsInRange1(new Modem(null)));
104+
assertFalse(priceIsInRange1(new Modem(15.5)));
105+
106+
}
98107

108+
@Test
109+
public void whenFiltersWithOptional_thenCorrect() {
110+
assertTrue(priceIsInRange2(new Modem(10.0)));
111+
assertFalse(priceIsInRange2(new Modem(9.9)));
112+
assertFalse(priceIsInRange2(new Modem(null)));
113+
assertFalse(priceIsInRange2(new Modem(15.5)));
114+
}
115+
116+
public boolean priceIsInRange1(Modem modem) {
117+
boolean isInRange = false;
118+
if (modem != null && modem.getPrice() != null && (modem.getPrice() >= 10 && modem.getPrice() <= 15)) {
119+
isInRange = true;
120+
}
121+
return isInRange;
122+
}
123+
124+
public boolean priceIsInRange2(Modem modem2) {
125+
Optional<Modem> modemOptional = Optional.ofNullable(modem2);
126+
boolean isInRange = modemOptional.filter(modem -> Optional.ofNullable(modem.getPrice()).orElse(-1.0) >= 10 && modem.getPrice() <= 15).isPresent();
127+
return isInRange;
128+
}
99129
// Transforming Value With map()
100130
@Test
101131
public void givenOptional_whenMapWorks_thenCorrect() {
@@ -203,4 +233,4 @@ public String getMyDefault() {
203233
System.out.println("Getting default value...");
204234
return "Default Value";
205235
}
206-
}
236+
}

0 commit comments

Comments
 (0)