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

Nikita Golubev - part 1 #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 25 additions & 21 deletions src/test/java/part1/exercise/CompletableFutureBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public void createNonEmpty() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);

// TODO Create non empty Optional
final Optional<Person> optPerson = null;
final Optional<Person> optPerson = Optional.of(person);

assertTrue(optPerson.isPresent());
assertEquals(person, optPerson.get());

// TODO Create stream with a single element
final Stream<Person> streamPerson = null;
final Stream<Person> streamPerson = Stream.of(person);

final List<Person> persons = streamPerson.collect(toList());
assertThat(persons.size(), is(1));
assertEquals(person, persons.get(0));

// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = null;
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);

assertTrue(futurePerson.isDone());
assertEquals(person, futurePerson.get());
Expand All @@ -83,18 +83,19 @@ public void createNonEmpty() throws ExecutionException, InterruptedException {
@Test
public void createEmpty() throws ExecutionException, InterruptedException {
// TODO Create empty Optional
final Optional<Person> optPerson = null;
final Optional<Person> optPerson = Optional.empty();

assertFalse(optPerson.isPresent());

// TODO Create empty stream
final Stream<Person> streamPerson = null;
final Stream<Person> streamPerson = Stream.empty();

final List<Person> persons = streamPerson.collect(toList());
assertThat(persons.size(), is(0));

// TODO Complete CompletableFuture with NoSuchElementException
final CompletableFuture<Person> futurePerson = null;
final CompletableFuture<Person> futurePerson = new CompletableFuture<>();
futurePerson.completeExceptionally(new NoSuchElementException());
// futurePerson.???

assertTrue(futurePerson.isCompletedExceptionally());
Expand All @@ -108,27 +109,30 @@ public void forEach() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);

// TODO Create non empty Optional
final Optional<Person> optPerson = null;
final Optional<Person> optPerson = Optional.of(person);

final CompletableFuture<Person> result1 = new CompletableFuture<>();

// TODO using optPerson.ifPresent complete result1
optPerson.ifPresent(result1::complete);
assertEquals(person, result1.get());

// TODO Create stream with a single element
final Stream<Person> streamPerson = null;
final Stream<Person> streamPerson = Stream.of(person);

final CompletableFuture<Person> result2 = new CompletableFuture<>();

// TODO Using streamPerson.forEach complete result2
streamPerson.forEach(result2::complete);
assertEquals(person, result2.get());

// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = null;
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);

final CompletableFuture<Person> result3 = new CompletableFuture<>();

// TODO Using futurePerson.thenAccept complete result3
futurePerson.thenAccept(result3::complete);
assertEquals(person, result3.get());
}

Expand All @@ -137,26 +141,26 @@ public void map() throws ExecutionException, InterruptedException {
final Person person = new Person("John", "Galt", 33);

// TODO Create non empty Optional
final Optional<Person> optPerson = null;
final Optional<Person> optPerson = Optional.of(person);

// TODO get Optional<first name> from optPerson
final Optional<String> optFirstName = null;
final Optional<String> optFirstName = optPerson.map(Person::getFirstName);

assertEquals(person.getFirstName(), optFirstName.get());

// TODO Create stream with a single element
final Stream<Person> streamPerson = null;
final Stream<Person> streamPerson = Stream.of(person);

// TODO Get Stream<first name> from streamPerson
final Stream<String> streamFirstName = null;
final Stream<String> streamFirstName = streamPerson.map(Person::getFirstName);

assertEquals(person.getFirstName(), streamFirstName.collect(toList()).get(0));

// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = null;
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);

// TODO Get CompletableFuture<first name> from futurePerson
final CompletableFuture<String> futureFirstName = null;
final CompletableFuture<String> futureFirstName = futurePerson.thenApply(Person::getFirstName);

assertEquals(person.getFirstName(), futureFirstName.get());
}
Expand All @@ -166,29 +170,29 @@ public void flatMap() throws ExecutionException, InterruptedException {
final Person person = employeeDb.get(keys.get(0)).thenApply(Employee::getPerson).get();

// TODO Create non empty Optional
final Optional<Person> optPerson = null;
final Optional<Person> optPerson = Optional.of(person);

// TODO Using flatMap and .getFirstName().codePoints().mapToObj(p -> p).findFirst()
// TODO get the first letter of first name if any
final Optional<Integer> optFirstCodePointOfFirstName =
null;
optPerson.flatMap(person1 -> person1.getFirstName().codePoints().mapToObj(value -> value).findFirst());

assertEquals(Integer.valueOf(65), optFirstCodePointOfFirstName.get());

// TODO Create stream with a single element
final Stream<Person> streamPerson = null;
final Stream<Person> streamPerson = Stream.of(person);

// TODO Using flatMapToInt and .getFirstName().codePoints() get codepoints stream from streamPerson
final IntStream codePoints = null;
final IntStream codePoints = streamPerson.flatMapToInt(person1 -> person1.getFirstName().codePoints());

final int[] codePointsArray = codePoints.toArray();
assertEquals(person.getFirstName(), new String(codePointsArray, 0, codePointsArray.length));

// TODO Create completed CompletableFuture
final CompletableFuture<Person> futurePerson = null;
final CompletableFuture<Person> futurePerson = CompletableFuture.completedFuture(person);

// TODO Get CompletableFuture<Employee> from futurePerson using getKeyByPerson and employeeDb
final CompletableFuture<Employee> futureEmployee = null;
final CompletableFuture<Employee> futureEmployee = futurePerson.thenCompose(person1 -> employeeDb.get(getKeyByPerson(person1)));

assertEquals(person, futureEmployee.thenApply(Employee::getPerson).get());
}
Expand Down