diff --git a/src/main/java/lambda/data/Person.java b/src/main/java/lambda/data/Person.java new file mode 100644 index 0000000..773efac --- /dev/null +++ b/src/main/java/lambda/data/Person.java @@ -0,0 +1,55 @@ +package lambda.data; + +import java.util.Objects; + +public class Person { + + private final String firstName; + private final String lastName; + private final int age; + + public Person(String firstName, String lastName, int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; + } + + public String getFirstName(Person this) { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public int getAge() { + return age; + } + + public String getFullName() { + return firstName + " " + lastName; + } + + @Override + public boolean equals(Object other) { + if (this == other) return true; + if (other == null || getClass() != other.getClass()) return false; + Person person = (Person) other; + return age == person.age + && Objects.equals(firstName, person.firstName) + && Objects.equals(lastName, person.lastName); + } + + @Override + public int hashCode() { + return Objects.hash(age, firstName, lastName); + } + + @Override + public String toString() { + return "Person@" + hashCode() + ": {" + + "firstName='" + firstName + "', " + + "lastName='" + lastName + "', " + + "age=" + age + "}"; + } +} diff --git a/src/test/java/api/exercise/Exercise1.java b/src/test/java/api/exercise/Exercise1.java new file mode 100644 index 0000000..29ee8d1 --- /dev/null +++ b/src/test/java/api/exercise/Exercise1.java @@ -0,0 +1,113 @@ +package api.exercise; + +import lambda.data.Person; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.stream.Collectors; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; + +@SuppressWarnings({"ConstantConditions", "unused", "MismatchedQueryAndUpdateOfCollection"}) +class Exercise1 { + + enum Status { + UNKNOWN, + PENDING, + COMMENTED, + ACCEPTED, + DECLINED + } + + @Test + void acceptGreaterThan21OthersDecline() { + Person alex = new Person("Алексей", "Мельников", 20); + Person ivan = new Person("Иван", "Стрельцов", 24); + Person helen = new Person("Елена", "Рощина", 22); + Map candidates = new HashMap<>(); + candidates.put(alex, Status.PENDING); + candidates.put(ivan, Status.PENDING); + candidates.put(helen, Status.PENDING); + + BiFunction greaterThan21 = (p, s) -> p.getAge() > 21 ? Status.ACCEPTED : Status.DECLINED; + candidates.replaceAll(greaterThan21); + + assertThat(candidates, Matchers.hasEntry(ivan, Status.ACCEPTED)); + assertThat(candidates, Matchers.hasEntry(helen, Status.ACCEPTED)); + assertThat(candidates, Matchers.hasEntry(alex, Status.DECLINED)); + } + + @Test + void acceptGreaterThan21OthersRemove() { + Person alex = new Person("Алексей", "Мельников", 20); + Person ivan = new Person("Иван", "Стрельцов", 24); + Person helen = new Person("Елена", "Рощина", 22); + Map candidates = new HashMap<>(); + candidates.put(alex, Status.PENDING); + candidates.put(ivan, Status.PENDING); + candidates.put(helen, Status.PENDING); + + candidates.put(new Person("a", "a", 19), Status.PENDING); + candidates.put(new Person("b", "c", 16), Status.PENDING); + candidates.put(new Person("b", "c", 5), Status.PENDING); + + candidates = candidates.entrySet().stream() + .filter(e -> e.getKey().getAge() > 21) + .peek(e -> e.setValue(Status.ACCEPTED)) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue)); + + assertThat(candidates, Matchers.hasEntry(ivan, Status.ACCEPTED)); + assertThat(candidates, Matchers.hasEntry(helen, Status.ACCEPTED)); + assertThat(candidates, not(hasKey(alex))); + } + + @Test + void getStatus() { + Person alex = new Person("Алексей", "Мельников", 20); + Person ivan = new Person("Иван", "Стрельцов", 24); + Person helen = new Person("Елена", "Рощина", 22); + Map candidates = new HashMap<>(); + candidates.put(alex, Status.PENDING); + candidates.put(ivan, Status.PENDING); + + Status alexStatus = candidates.getOrDefault(alex, Status.UNKNOWN); + Status ivanStatus = candidates.getOrDefault(ivan, Status.UNKNOWN); + Status helenStatus = candidates.getOrDefault(helen, Status.UNKNOWN); + + assertThat(alexStatus, is(Status.PENDING)); + assertThat(ivanStatus, is(Status.PENDING)); + assertThat(helenStatus, is(Status.UNKNOWN)); + } + + @Test + void putToNewValuesIfNotExists() { + Person alex = new Person("Алексей", "Мельников", 20); + Person ivan = new Person("Иван", "Стрельцов", 24); + Person helen = new Person("Елена", "Рощина", 22); + Person dmitry = new Person("Дмитрий", "Егоров", 30); + Map oldValues = new HashMap<>(); + oldValues.put(alex, Status.PENDING); + oldValues.put(dmitry, Status.DECLINED); + oldValues.put(ivan, Status.ACCEPTED); + + Map newValues = new HashMap<>(); + newValues.put(alex, Status.DECLINED); + newValues.put(helen, Status.PENDING); + + // TODO implementation + oldValues.forEach(newValues::putIfAbsent); + + assertThat(newValues, hasEntry(alex, Status.DECLINED)); + assertThat(newValues, hasEntry(ivan, Status.ACCEPTED)); + assertThat(newValues, hasEntry(helen, Status.PENDING)); + } +}