-
Notifications
You must be signed in to change notification settings - Fork 25
CompletableFutureBasic + CachingDataStorageImpl #34
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
mmgeorg
wants to merge
1
commit into
java8-course:master
Choose a base branch
from
mmgeorg:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,42 +60,36 @@ public static void after() { | |
| 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()); | ||
| } | ||
|
|
||
| @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; | ||
| // futurePerson.??? | ||
| final CompletableFuture<Person> futurePerson = new CompletableFuture<>(); | ||
| futurePerson.completeExceptionally(new NoSuchElementException()); | ||
|
|
||
| assertTrue(futurePerson.isCompletedExceptionally()); | ||
| assertTrue(futurePerson | ||
|
|
@@ -107,56 +101,48 @@ public void createEmpty() throws ExecutionException, InterruptedException { | |
| 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()); | ||
| } | ||
|
|
||
| @Test | ||
| 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 = Optional.of(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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // TODO Get CompletableFuture from futurePerson |
||
|
|
||
| assertEquals(person.getFirstName(), futureFirstName.get()); | ||
| } | ||
|
|
@@ -165,31 +151,30 @@ public void map() throws ExecutionException, InterruptedException { | |
| 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; | ||
| final Optional<Integer> optFirstCodePointOfFirstName = optPerson | ||
| .flatMap(person1 -> person1.getFirstName() | ||
| .codePoints() | ||
| .boxed() | ||
| .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 = employeeDb.get(getKeyByPerson(futurePerson.get())); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use blocking method |
||
|
|
||
| assertEquals(person, futureEmployee.thenApply(Employee::getPerson).get()); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO get Optional from optPerso