// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/Address.java
// public class Address {
// 
//     private int nr;
//     private String street;
//     private String country;
//     private String city;
//     private Optional<PostalCode> postalCode =  Optional.empty();
// 
//     public Address(int nr, String street, String country, String city) {
//         this.nr = nr;
//         this.street = street;
//         this.country = country;
//         this.city = city;
//     }
// 
//     public int getNr() {
//         return nr;
//     }
// 
//     public void setNr(int nr) {
//         this.nr = nr;
//     }
// 
//     public String getStreet() {
//         return street;
//     }
// 
//     public void setStreet(String street) {
//         this.street = street;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public void setCountry(String country) {
//         this.country = country;
//     }
// 
//     public String getCity() {
//         return city;
//     }
// 
//     public void setCity(String city) {
//         this.city = city;
//     }
// 
//     public Optional<PostalCode> getPostalCode() {
//         return postalCode;
//     }
// 
//     public void setPostalCode(PostalCode postalCode) {
//         this.postalCode = Optional.of(postalCode);
//     }
// }
// 
// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/Student.java
// public class Student {
// 
//     private String name;
//     private Address address;
//     private Optional<Address> temporaryAddress = Optional.empty();
// 
//     public Student(String name, Address address) {
//         this.address = address;
//         this.name = name;
//     }
// 
//     public Address getAddress() {
//         return address;
//     }
// 
//     public void setAddress(Address address) {
//         this.address = address;
//     }
// 
//     public Optional<Address> getTemporaryAddress() {
//         return temporaryAddress;
//     }
// 
//     public void setTemporaryAddress(Address temporaryAddress) {
//         this.temporaryAddress = Optional.ofNullable(temporaryAddress);
//     }
// 
//     public String getName() {
//         return name;
//     }
// }
// 
// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/PostalCode.java
// public class PostalCode {
// 
//     private int number;
//     private String prefix;
// 
//     public PostalCode(int number, String prefix) {
//         this.number = number;
//         this.prefix = prefix;
//     }
// 
//     public int getNumber() {
//         return number;
//     }
// 
//     public void setNumber(int number) {
//         this.number = number;
//     }
// 
//     public String getPrefix() {
//         return prefix;
//     }
// 
//     public void setPrefix(String prefix) {
//         this.prefix = prefix;
//     }
// }


import net.safedata.java8.training.optional.with.flatmap.model.Address;
import net.safedata.java8.training.optional.with.flatmap.model.Student;
import net.safedata.java8.training.optional.with.flatmap.model.PostalCode;
import java.util.*;
package net.safedata.java8.training.optional.with.flatmap;




public class FlatMapExample {

    public static void main(String[] args) {

        List<Student> students = setUp();

        //Do I have any student with a temporary address abroad ?

        //With postal code
        students.stream()
                .filter(student -> student.getName().equals("Mihai"))
                .findAny()
                .flatMap(Student::getTemporaryAddress)
                .flatMap(Address::getPostalCode)
                .filter(postalCode -> !postalCode.getPrefix().equals("RO"))
// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/Address.java
// public class Address {
// 
//     private int nr;
//     private String street;
//     private String country;
//     private String city;
//     private Optional<PostalCode> postalCode =  Optional.empty();
// 
//     public Address(int nr, String street, String country, String city) {
//         this.nr = nr;
//         this.street = street;
//         this.country = country;
//         this.city = city;
//     }
// 
//     public int getNr() {
//         return nr;
//     }
// 
//     public void setNr(int nr) {
//         this.nr = nr;
//     }
// 
//     public String getStreet() {
//         return street;
//     }
// 
//     public void setStreet(String street) {
//         this.street = street;
//     }
// 
//     public String getCountry() {
//         return country;
//     }
// 
//     public void setCountry(String country) {
//         this.country = country;
//     }
// 
//     public String getCity() {
//         return city;
//     }
// 
//     public void setCity(String city) {
//         this.city = city;
//     }
// 
//     public Optional<PostalCode> getPostalCode() {
//         return postalCode;
//     }
// 
//     public void setPostalCode(PostalCode postalCode) {
//         this.postalCode = Optional.of(postalCode);
//     }
// }
// 
// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/Student.java
// public class Student {
// 
//     private String name;
//     private Address address;
//     private Optional<Address> temporaryAddress = Optional.empty();
// 
//     public Student(String name, Address address) {
//         this.address = address;
//         this.name = name;
//     }
// 
//     public Address getAddress() {
//         return address;
//     }
// 
//     public void setAddress(Address address) {
//         this.address = address;
//     }
// 
//     public Optional<Address> getTemporaryAddress() {
//         return temporaryAddress;
//     }
// 
//     public void setTemporaryAddress(Address temporaryAddress) {
//         this.temporaryAddress = Optional.ofNullable(temporaryAddress);
//     }
// 
//     public String getName() {
//         return name;
//     }
// }
// 
// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/model/PostalCode.java
// public class PostalCode {
// 
//     private int number;
//     private String prefix;
// 
//     public PostalCode(int number, String prefix) {
//         this.number = number;
//         this.prefix = prefix;
//     }
// 
//     public int getNumber() {
//         return number;
//     }
// 
//     public void setNumber(int number) {
//         this.number = number;
//     }
// 
//     public String getPrefix() {
//         return prefix;
//     }
// 
//     public void setPrefix(String prefix) {
//         this.prefix = prefix;
//     }
// }


// Path: s02/s02e01-optional/src/main/java/net/safedata/java8/training/optional/with/flatmap/FlatMapExample.java
import net.safedata.java8.training.optional.with.flatmap.model.Address;
import net.safedata.java8.training.optional.with.flatmap.model.Student;
import net.safedata.java8.training.optional.with.flatmap.model.PostalCode;
import java.util.*;

package net.safedata.java8.training.optional.with.flatmap;




public class FlatMapExample {

    public static void main(String[] args) {

        List<Student> students = setUp();

        //Do I have any student with a temporary address abroad ?

        //With postal code
        students.stream()
                .filter(student -> student.getName().equals("Mihai"))
                .findAny()
                .flatMap(Student::getTemporaryAddress)
                .flatMap(Address::getPostalCode)
                .filter(postalCode -> !postalCode.getPrefix().equals("RO"))
                .ifPresent(code -> System.out.println("The postal code is : " +code.getPrefix() + "" + code.getNumber()));