Skip to content

Third homework (Aleksei Litkovetc) #63

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

Open
wants to merge 1 commit into
base: master
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
33 changes: 16 additions & 17 deletions src/test/java/part3/exercise/lambda/LambdaExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ public class LambdaExercise {
public void supply() {
final Person person = new Person("John", "Galt", 30);

final Supplier<Person> getPerson = null; // TODO return person from Supplier
final Supplier<Person> getPerson = () -> person;

assertEquals(person, getPerson.get());
}

@Test
public void function() {
final Function<Person, String> getPersonName1 = null; // TODO get the name of person using expression lambda
final Function<Person, String> getPersonName1 = p -> p.getFirstName();

final Function<Person, String> getPersonName2 = null; // TODO get the name of person using method reference
final Function<Person, String> getPersonName2 = Person::getFirstName;

// TODO get the name of person and log it to System.out using statement lambda: {}
final Function<Person, String> getPersonNameAndLogIt = null;
final Function<Person, String> getPersonNameAndLogIt = p -> {
String firstName = p.getFirstName();
System.out.println(firstName);
return firstName;
};

final Person person = new Person("John", "Galt", 30);

Expand All @@ -38,19 +41,17 @@ public void function() {

@Test
public void combineFunctions() {
final Function<Person, String> getPersonName = null; // TODO get the name of person
final Function<Person, String> getPersonName = Person::getFirstName;

assertEquals("John", getPersonName.apply(new Person("John", "Galt", 30)));

final Function<String, Integer> getStringLength = null; // TODO get string length
final Function<String, Integer> getStringLength = String::length;

assertEquals(Integer.valueOf(3), getStringLength.apply("ABC"));

// TODO get person name length using getPersonName and getStringLength without andThen
final Function<Person, Integer> getPersonNameLength1 = null;
final Function<Person, Integer> getPersonNameLength1 = p -> getStringLength.apply(getPersonName.apply(p));

// TODO get person name length using getPersonName and getStringLength with andThen
final Function<Person, Integer> getPersonNameLength2 = null;
final Function<Person, Integer> getPersonNameLength2 = getPersonName.andThen(getStringLength);

final Person person = new Person("John", "Galt", 30);

Expand All @@ -68,22 +69,20 @@ private Person createPerson(PersonFactory pf) {

// ((T -> R), (R -> boolean)) -> (T -> boolean)
private <T, R> Predicate<T> combine(Function<T, R> f, Predicate<R> p) {
// TODO
throw new UnsupportedOperationException();
return item -> p.test(f.apply(item));
}

@Test
public void methodReference() {
// TODO use only method reverences here.
final Person person = createPerson(null); // TODO
final Person person = createPerson(Person::new);

assertEquals(new Person("John", "Galt", 66), person);

final Function<Person, String> getPersonName = null; // TODO
final Function<Person, String> getPersonName = Person::getFirstName;

assertEquals("John", getPersonName.apply(person));

final Predicate<String> isJohnString = null; // TODO using method reference check that "John" equals string parameter
final Predicate<String> isJohnString = s -> s.equals("John");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use method reference this way: "John"::equals.


final Predicate<Person> isJohnPerson = combine(getPersonName, isJohnString);

Expand Down
115 changes: 93 additions & 22 deletions src/test/java/part3/exercise/stream/StreamsExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.assertEquals;

public class StreamsExercise {
Expand All @@ -19,7 +22,9 @@ public class StreamsExercise {
public void getAllJobHistoryEntries() {
final List<Employee> employees = getEmployees();

final List<JobHistoryEntry> jobHistoryEntries = null; // TODO
final List<JobHistoryEntry> jobHistoryEntries = employees.stream()
.flatMap(e -> e.getJobHistory().stream())
.collect(toList());

assertEquals(22, jobHistoryEntries.size());
}
Expand All @@ -29,12 +34,15 @@ public void getSumDuration() {
// sum all durations for all persons
final List<Employee> employees = getEmployees();

final int sumDurations = 0; // TODO
final int sumDurations = employees.stream()
.flatMap(e -> e.getJobHistory().stream())
.mapToInt(j -> j.getDuration())
.sum();

assertEquals(72, sumDurations);
}

private static class PersonEmployer{
private static class PersonEmployer {
private final Person person;
private final String employer;

Expand Down Expand Up @@ -64,7 +72,16 @@ public String toString() {
public void indexPersonsByEmployer1() {
final List<Employee> employees = getEmployees();

final Map<String, List<PersonEmployer>> index = null; // TODO
final Map<String, List<PersonEmployer>> index = employees.stream()
.flatMap(
e -> e.getJobHistory().stream()
.map(
j -> new PersonEmployer(e.getPerson(), j.getEmployer())
)
)
.collect(
groupingBy(PersonEmployer::getEmployer)
);

assertEquals(11, index.get("epam").size());
}
Expand All @@ -73,7 +90,16 @@ public void indexPersonsByEmployer1() {
public void indexPersonsByEmployer2() {
final List<Employee> employees = getEmployees();

final Map<String, List<Person>> index = null; // TODO
final Map<String, List<Person>> index = new HashMap<>();
employees.forEach(
e -> e.getJobHistory().forEach(
j -> {
String employer = j.getEmployer();
index.computeIfAbsent(employer, k -> new ArrayList<Person>());
index.get(employer).add(e.getPerson());
}
)
);

assertEquals(11, index.get("epam").size());
}
Expand Down Expand Up @@ -105,16 +131,18 @@ public String toString() {
}

private PersonDuration sumAllPersonDurations(Employee e) {
// TODO
throw new UnsupportedOperationException();
int sum = e.getJobHistory().stream().mapToInt(j -> j.getDuration()).sum();
return new PersonDuration(e.getPerson(), sum);
}

@Test
public void getSumPersonDuration() {
// sum all durations for each person
final List<Employee> employees = getEmployees();

final Map<Person, Integer> personDuration = null; // TODO use sumAllPersonDurations
final Map<Person, Integer> personDuration = employees.stream()
.map(this::sumAllPersonDurations)
.collect(toMap(PersonDuration::getPerson, PersonDuration::getDuration));

assertEquals(Integer.valueOf(8), personDuration.get(new Person("John", "Doe", 24)));
}
Expand All @@ -138,15 +166,23 @@ public Map<String, Integer> getDurationByPositionIndex() {
}

private static PersonPositionIndex getPersonPositionIndex(Employee e) {
// TODO
throw new UnsupportedOperationException();
Map<String, Integer> index = e.getJobHistory().stream()
.collect(
groupingBy(
JobHistoryEntry::getPosition,
summingInt(JobHistoryEntry::getDuration)
)
);
return new PersonPositionIndex(e.getPerson(), index);
}

@Test
public void getSumDurationsForPersonByPosition() {
final List<Employee> employees = getEmployees();

final List<PersonPositionIndex> personIndexes = null; // TODO use getPersonPositionIndex
final List<PersonPositionIndex> personIndexes = employees.stream()
.map(StreamsExercise::getPersonPositionIndex)
.collect(toList());

assertEquals(1, personIndexes.get(3).getDurationByPositionIndex().size());
}
Expand Down Expand Up @@ -179,8 +215,15 @@ public int getDuration() {
public void getDurationsForEachPersonByPosition() {
final List<Employee> employees = getEmployees();

final List<PersonPositionDuration> personPositionDurations = null; // TODO

final List<PersonPositionDuration> personPositionDurations = employees.stream()
.map(StreamsExercise::getPersonPositionIndex)
.flatMap(
e -> e.getDurationByPositionIndex()
.entrySet()
.stream()
.map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))
)
.collect(toList());

assertEquals(17, personPositionDurations.size());
}
Expand All @@ -190,8 +233,21 @@ public void getCoolestPersonByPosition1() {
// Get person with max duration on given position
final List<Employee> employees = getEmployees();

final Map<String, PersonPositionDuration> coolestPersonByPosition = null;// TODO

final Map<String, PersonPositionDuration> coolestPersonByPosition = employees.stream()
.map(StreamsExercise::getPersonPositionIndex)
.flatMap(
e -> e.getDurationByPositionIndex()
.entrySet()
.stream()
.map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))
)
.collect(
toMap(
PersonPositionDuration::getPosition,
Function.identity(),
BinaryOperator.maxBy(comparingInt(PersonPositionDuration::getDuration))
)
);

assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA").getPerson());
}
Expand All @@ -201,8 +257,23 @@ public void getCoolestPersonByPosition2() {
// Get person with max duration on given position
final List<Employee> employees = getEmployees();

final Map<String, Person> coolestPersonByPosition = null; // TODO

final Map<String, Person> coolestPersonByPosition = employees.stream()
.map(StreamsExercise::getPersonPositionIndex)
.flatMap(
e -> e.getDurationByPositionIndex()
.entrySet()
.stream()
.map(p -> new PersonPositionDuration(e.getPerson(), p.getKey(), p.getValue()))
)
.collect(
groupingBy(
PersonPositionDuration::getPosition,
collectingAndThen(
maxBy(comparing(PersonPositionDuration::getDuration)),
ppd -> ppd.get().getPerson()
)
)
);

assertEquals(new Person("John", "White", 22), coolestPersonByPosition.get("QA"));
}
Expand Down