diff --git a/src/main/java/part2/cache/TypedEmployeeCachedStorage.java b/src/main/java/part2/cache/TypedEmployeeCachedStorage.java index e9049a5..7915f07 100755 --- a/src/main/java/part2/cache/TypedEmployeeCachedStorage.java +++ b/src/main/java/part2/cache/TypedEmployeeCachedStorage.java @@ -4,6 +4,8 @@ import data.typed.Employer; import data.typed.Position; +import java.util.concurrent.CompletableFuture; + public class TypedEmployeeCachedStorage implements CachingDataStorage { private final CachingDataStorage employeeStorage; @@ -21,6 +23,9 @@ public TypedEmployeeCachedStorage(CachingDataStorage empl @Override public OutdatableResult getOutdatable(String key) { // TODO note that you don't know timeouts for different storage. And timeouts can be different. + + final CompletableFuture employeeCompletableFuture = employeeStorage.get(key); + throw new UnsupportedOperationException(); } } diff --git a/src/test/java/part1/exercise/CompletableFutureBasics.java b/src/test/java/part1/exercise/CompletableFutureBasics.java index cfd0861..859b2ac 100755 --- a/src/test/java/part1/exercise/CompletableFutureBasics.java +++ b/src/test/java/part1/exercise/CompletableFutureBasics.java @@ -9,10 +9,7 @@ import org.junit.Test; import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.function.Function; @@ -39,7 +36,7 @@ public static void before() { (a, b) -> a)); employeeDb = new SlowCompletableFutureDb<>(employeeMap); - keys = employeeMap.keySet().stream().collect(toList()); + keys = new ArrayList<>(employeeMap.keySet()); } private static String getKeyByPerson(Person person) { @@ -60,21 +57,18 @@ 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 optPerson = null; + final Optional optPerson = Optional.of(person); assertTrue(optPerson.isPresent()); assertEquals(person, optPerson.get()); - // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); final List persons = streamPerson.collect(toList()); assertThat(persons.size(), is(1)); assertEquals(person, persons.get(0)); - // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); assertTrue(futurePerson.isDone()); assertEquals(person, futurePerson.get()); @@ -82,20 +76,17 @@ public void createNonEmpty() throws ExecutionException, InterruptedException { @Test public void createEmpty() throws ExecutionException, InterruptedException { - // TODO Create empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.empty(); assertFalse(optPerson.isPresent()); - // TODO Create empty stream - final Stream streamPerson = null; + final Stream streamPerson = Stream.empty(); final List persons = streamPerson.collect(toList()); assertThat(persons.size(), is(0)); - // TODO Complete CompletableFuture with NoSuchElementException - final CompletableFuture futurePerson = null; - // futurePerson.??? + final CompletableFuture futurePerson = new CompletableFuture<>(); + futurePerson.completeExceptionally(new NoSuchElementException()); assertTrue(futurePerson.isCompletedExceptionally()); assertTrue(futurePerson @@ -107,28 +98,27 @@ 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 optPerson = null; + final Optional optPerson = Optional.of(person); final CompletableFuture 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 streamPerson = null; + final Stream streamPerson = Stream.of(person); final CompletableFuture result2 = new CompletableFuture<>(); - // TODO Using streamPerson.forEach complete result2 + streamPerson.forEach(result2::complete); + assertEquals(person, result2.get()); - // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); final CompletableFuture result3 = new CompletableFuture<>(); - // TODO Using futurePerson.thenAccept complete result3 + futurePerson.thenAccept(result3::complete); assertEquals(person, result3.get()); } @@ -136,27 +126,21 @@ public void forEach() throws ExecutionException, InterruptedException { public void map() throws ExecutionException, InterruptedException { final Person person = new Person("John", "Galt", 33); - // TODO Create non empty Optional - final Optional optPerson = null; + final Optional optPerson = Optional.of(person); - // TODO get Optional from optPerson - final Optional optFirstName = null; + final Optional optFirstName = optPerson.map(Person::getFirstName); assertEquals(person.getFirstName(), optFirstName.get()); - // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); - // TODO Get Stream from streamPerson - final Stream streamFirstName = null; + final Stream streamFirstName = streamPerson.map(Person::getFirstName); assertEquals(person.getFirstName(), streamFirstName.collect(toList()).get(0)); - // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); - // TODO Get CompletableFuture from futurePerson - final CompletableFuture futureFirstName = null; + final CompletableFuture futureFirstName = futurePerson.thenApply(Person::getFirstName); assertEquals(person.getFirstName(), futureFirstName.get()); } @@ -165,30 +149,22 @@ 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 optPerson = null; + final Optional 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 optFirstCodePointOfFirstName = - null; + final Optional optFirstCodePointOfFirstName = optPerson.flatMap(p -> p.getFirstName().codePoints().boxed().findFirst()); assertEquals(Integer.valueOf(65), optFirstCodePointOfFirstName.get()); - // TODO Create stream with a single element - final Stream streamPerson = null; + final Stream streamPerson = Stream.of(person); - // TODO Using flatMapToInt and .getFirstName().codePoints() get codepoints stream from streamPerson - final IntStream codePoints = null; + final IntStream codePoints = streamPerson.flatMapToInt(p -> p.getFirstName().codePoints()); final int[] codePointsArray = codePoints.toArray(); assertEquals(person.getFirstName(), new String(codePointsArray, 0, codePointsArray.length)); - // TODO Create completed CompletableFuture - final CompletableFuture futurePerson = null; + final CompletableFuture futurePerson = CompletableFuture.completedFuture(person); - // TODO Get CompletableFuture from futurePerson using getKeyByPerson and employeeDb - final CompletableFuture futureEmployee = null; + final CompletableFuture futureEmployee = futurePerson.thenCompose(p -> employeeDb.get(getKeyByPerson(p))); assertEquals(person, futureEmployee.thenApply(Employee::getPerson).get()); }