Skip to content

Instantly share code, notes, and snippets.

View SilverioMG's full-sized avatar

Silverio Martinez Garcia SilverioMG

View GitHub Profile
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;
package net.atopecode.optionals.model;
public class State {
private String name;
private Integer code;
public State(String name, Integer code) {
this.name = name;
package net.atopecode.optionals.model;
public class Address {
private String street;
private Integer number;
private State state;
package net.atopecode.optionals.model;
public class Person {
private String name;
private String email;
private Address address;
public class Person {
private String name;
public Person(){
name = null;
}
String getName(){
return name;
}
public class Person {
private String name;
//Mala práctica.
public Person(Optional<String> name){
this.name = name.orElse("");
}
}
public class Person {
private Optional<String> name; //Mala práctica.
public Person(){
this.name = Optional.empty();
}
}
public class PersonService {
private PersonRepository personRepository;
@Autowired
public(PersonRepository personRepository){
this.personRepository = personRepository;
}
//Se devuelve un 'Optional' indicando que puede que no exista
@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);
@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);