Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework.api.exercise1 (Mikhaylov) #384

Open
wants to merge 2 commits into
base: mikhaylov
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/main/java/lambda/data/Person.java
Original file line number Diff line number Diff line change
@@ -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 + "}";
}
}
113 changes: 113 additions & 0 deletions src/test/java/api/exercise/Exercise1.java
Original file line number Diff line number Diff line change
@@ -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<Person, Status> candidates = new HashMap<>();
candidates.put(alex, Status.PENDING);
candidates.put(ivan, Status.PENDING);
candidates.put(helen, Status.PENDING);

BiFunction<? super Person, ? super Status, ? extends Status> 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<Person, Status> 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<Person, Status> 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<Person, Status> oldValues = new HashMap<>();
oldValues.put(alex, Status.PENDING);
oldValues.put(dmitry, Status.DECLINED);
oldValues.put(ivan, Status.ACCEPTED);

Map<Person, Status> 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));
}
}