Added new code snippets to OptionalTest to further demo filter API (#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
This commit is contained in:
Egima profile 2016-12-06 23:55:33 +03:00 committed by Grzegorz Piwowarek
parent f621b66a51
commit 8f9f3bdfee
4 changed files with 47 additions and 4 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.optional;
public class Modem {
private Double price;
public Modem(Double price) {
this.price = price;
}
public Double getPrice() {
return price;
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.java_8_features;
package com.baeldung.optional;
import java.util.Optional;

View File

@ -33,4 +33,4 @@ public class AsyncEchoTest {
client.stop();
}
}
}

View File

@ -11,7 +11,8 @@ import java.util.Optional;
import org.junit.Test;
import com.baeldung.java_8_features.Person;
import com.baeldung.optional.Person;
import com.baeldung.optional.Modem;
public class OptionalTest {
// creating Optional
@ -95,7 +96,36 @@ public class OptionalTest {
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
assertFalse(is2017);
}
@Test
public void whenFiltersWithoutOptional_thenCorrect() {
assertTrue(priceIsInRange1(new Modem(10.0)));
assertFalse(priceIsInRange1(new Modem(9.9)));
assertFalse(priceIsInRange1(new Modem(null)));
assertFalse(priceIsInRange1(new Modem(15.5)));
}
@Test
public void whenFiltersWithOptional_thenCorrect() {
assertTrue(priceIsInRange2(new Modem(10.0)));
assertFalse(priceIsInRange2(new Modem(9.9)));
assertFalse(priceIsInRange2(new Modem(null)));
assertFalse(priceIsInRange2(new Modem(15.5)));
}
public boolean priceIsInRange1(Modem modem) {
boolean isInRange = false;
if (modem != null && modem.getPrice() != null && (modem.getPrice() >= 10 && modem.getPrice() <= 15)) {
isInRange = true;
}
return isInRange;
}
public boolean priceIsInRange2(Modem modem2) {
Optional<Modem> modemOptional = Optional.ofNullable(modem2);
boolean isInRange = modemOptional.filter(modem -> Optional.ofNullable(modem.getPrice()).orElse(-1.0) >= 10 && modem.getPrice() <= 15).isPresent();
return isInRange;
}
// Transforming Value With map()
@Test
public void givenOptional_whenMapWorks_thenCorrect() {
@ -203,4 +233,4 @@ public class OptionalTest {
System.out.println("Getting default value...");
return "Default Value";
}
}
}