This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.atopecode.optionals; | |
| import static org.junit.jupiter.api.Assertions.assertEquals; | |
| import static org.junit.jupiter.api.Assertions.assertFalse; | |
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | |
| import static org.junit.jupiter.api.Assertions.assertNotNull; | |
| import static org.junit.jupiter.api.Assertions.assertNull; | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | |
| import static org.junit.jupiter.api.Assertions.assertTrue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.atopecode.optionals.model; | |
| public class State { | |
| private String name; | |
| private Integer code; | |
| public State(String name, Integer code) { | |
| this.name = name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.atopecode.optionals.model; | |
| public class Address { | |
| private String street; | |
| private Integer number; | |
| private State state; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.atopecode.optionals.model; | |
| public class Person { | |
| private String name; | |
| private String email; | |
| private Address address; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person { | |
| private String name; | |
| public Person(){ | |
| name = null; | |
| } | |
| String getName(){ | |
| return name; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person { | |
| private String name; | |
| //Mala práctica. | |
| public Person(Optional<String> name){ | |
| this.name = name.orElse(""); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person { | |
| private Optional<String> name; //Mala práctica. | |
| public Person(){ | |
| this.name = Optional.empty(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PersonService { | |
| private PersonRepository personRepository; | |
| @Autowired | |
| public(PersonRepository personRepository){ | |
| this.personRepository = personRepository; | |
| } | |
| //Se devuelve un 'Optional' indicando que puede que no exista |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| public void allMatch_anyMatch_noneMatch() { | |
| List<Integer> intList = Arrays.asList(2, 4, 5, 6, 8); | |
| boolean allEven = intList.stream().allMatch(i -> i % 2 == 0); | |
| boolean oneEven = intList.stream().anyMatch(i -> i % 2 == 0); | |
| boolean noneMultipleOfThree = intList.stream().noneMatch(i -> i % 3 == 0); | |
| assertEquals(allEven, false); | |
| assertEquals(oneEven, true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| public void distinct() { | |
| bookList.add(new Book(bookList.get(0).getTitle(), "", 0d)); | |
| List<String> distinctTitles = bookList.stream() | |
| .map(book -> book.getTitle()) | |
| .distinct() | |
| .peek(System.out::println) | |
| .collect(Collectors.toList()); | |
| assertEquals(distinctTitles.size(), 3); |