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

Nazarov_Anton/part-3 #59

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
28 changes: 24 additions & 4 deletions src/main/java/part3/exercise/ComposeCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@

import part2.cache.CachingDataStorage;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class ComposeCachingDataStorage<K1, T1, K2, T2> implements CachingDataStorage<K1, T2> {
private final CachingDataStorage<K1, T1> storage1;
private final CachingDataStorage<K2, T2> storage2;
private final Function<T1, K2> mapping;

public ComposeCachingDataStorage(CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<T1, K2> mapping) {
// TODO
throw new UnsupportedOperationException();
this.storage1 = storage1;
this.storage2 = storage2;
this.mapping = mapping;
}


@Override
public OutdatableResult<T2> getOutdatable(K1 key) {
// TODO
throw new UnsupportedOperationException();
final OutdatableResult<T1> outdatable = storage1.getOutdatable(key);
final CompletableFuture<OutdatableResult<T2>> outdated
= outdatable.getResult().thenApply(v -> storage2.getOutdatable(mapping.apply(v)));

final OutdatableResult<T2> result
= new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());

outdatable.getOutdated().thenAccept(v -> result.getOutdated().complete(v));

outdated.thenAccept(v -> {
v.getResult().thenAccept(t -> {
result.getResult().complete(t);
result.getOutdated().thenAccept(o -> result.getOutdated().complete(o));
});
});
return result;
}
}
35 changes: 31 additions & 4 deletions src/main/java/part3/exercise/ListCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,44 @@
import part2.cache.CachingDataStorage;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

public class ListCachingDataStorage<K, T> implements CachingDataStorage<List<K>, List<T>> {
private final CachingDataStorage<K, T> storage;

public ListCachingDataStorage(CachingDataStorage<K, T> storage) {
// TODO
throw new UnsupportedOperationException();
this.storage = storage;
}

private <T> T getOrNull(Future<T> f) {
try {
return f.get();
} catch (InterruptedException | ExecutionException e1) {
e1.printStackTrace();
return null;
}
}

@Override
public OutdatableResult<List<T>> getOutdatable(List<K> key) {
// TODO
throw new UnsupportedOperationException();
final List<OutdatableResult<T>> collect = key.stream()
.map(storage::getOutdatable)
.collect(Collectors.toList());

final CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(collect.stream()
.map(OutdatableResult::getResult)
.toArray(CompletableFuture[]::new));

return new OutdatableResult<>(voidCompletableFuture.thenApply(v -> collect.stream()
.map(OutdatableResult::getResult)
.map(this::getOrNull)
.collect(Collectors.toList())),
CompletableFuture.anyOf(collect.stream()
.map(OutdatableResult::getOutdated)
.toArray(CompletableFuture[]::new))
.thenApply(v -> null));
}
}
14 changes: 10 additions & 4 deletions src/main/java/part3/exercise/MappingCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
import java.util.function.Function;

public class MappingCachingDataStorage<K, K1, T1, T> implements CachingDataStorage<K, T> {
private final CachingDataStorage<K1, T1> storage;
private final Function<K, K1> mapKey;
private final BiFunction<K, T1, T> mapValue;

public MappingCachingDataStorage(CachingDataStorage<K1, T1> storage,
Function<K, K1> mapKey,
BiFunction<K, T1, T> mapValue) {
// TODO
throw new UnsupportedOperationException();
this.storage = storage;
this.mapKey = mapKey;
this.mapValue = mapValue;
}

@Override
public OutdatableResult<T> getOutdatable(K key) {
// TODO
throw new UnsupportedOperationException();
final OutdatableResult<T1> outdatable = storage.getOutdatable(mapKey.apply(key));

return new OutdatableResult<>(outdatable.getResult().thenApply(v -> mapValue.apply(key, v)),
outdatable.getOutdated());
}
}
20 changes: 16 additions & 4 deletions src/main/java/part3/exercise/PairCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@

import part2.cache.CachingDataStorage;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;

public class PairCachingDataStorage<K, T, K1, T1, K2, T2> implements CachingDataStorage<K, T> {

private final CachingDataStorage<K1, T1> storage1;
private final CachingDataStorage<K2, T2> storage2;
private final Function<K, K1> getKey1;
private final Function<K, K2> getKey2;
private final Function<K, BiFunction<T1, T2, T>> resultMapper;

public PairCachingDataStorage(CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<K, K1> getKey1,
Function<K, K2> getKey2,
Function<K, BiFunction<T1, T2, T>> resultMapper) {
// TODO
throw new UnsupportedOperationException();
this.storage1 = storage1;
this.storage2 = storage2;
this.getKey1 = getKey1;
this.getKey2 = getKey2;
this.resultMapper = resultMapper;
}

@Override
public OutdatableResult<T> getOutdatable(K key) {
// TODO
throw new UnsupportedOperationException();
final OutdatableResult<T1> outdatable = storage1.getOutdatable(getKey1.apply(key));
final OutdatableResult<T2> outdatable1 = storage2.getOutdatable(getKey2.apply(key));
return new OutdatableResult<>(outdatable.getResult().thenCombine(outdatable1.getResult(), resultMapper.apply(key)),
CompletableFuture.anyOf(outdatable.getOutdated(), outdatable1.getOutdated()).thenApply(x -> null));
}
}