parent
74b303f3eb
commit
72b6473aac
|
@ -41,47 +41,53 @@ public class ChildDataImpl implements ChildData {
|
|||
|
||||
@Override
|
||||
public CompletableFuture<List<ChildModel>> getChildList(ChildModelCallback callback) { // TODO: リファクタリング
|
||||
logger.debug("子供リスト取得開始");
|
||||
return cacheWrapper.getChildList().thenComposeAsync(childList -> {
|
||||
if (childList == null || childList.isEmpty()) {
|
||||
logger.debug("キャッシュ無: 子供リストを取得中...");
|
||||
return ksActions.syncChildList().thenApply(childListFromServer -> {
|
||||
if (childListFromServer == null || childListFromServer.isEmpty()) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
logger.debug("子供リスト取得開始");
|
||||
AtomicReference<List<ChildModel>> childListTmp = new AtomicReference<>();
|
||||
Thread thread = new Thread(() -> ksActions.syncChildList().thenAccept(childList -> {
|
||||
if (childListTmp.get() == null || childListTmp.get().isEmpty()) {
|
||||
logger.debug("子供リスト取得完了: キャッシュよりはやく取得完了 or キャッシュ無し");
|
||||
if (childList == null || childList.isEmpty()) {
|
||||
callback.onUnchanged();
|
||||
} else {
|
||||
callback.onUpdated(childListFromServer);
|
||||
callback.onUpdated(childList);
|
||||
}
|
||||
return childListFromServer;
|
||||
}).exceptionally(e -> {
|
||||
logger.error("子供リスト取得失敗: " + e.getMessage());
|
||||
callback.onFailed(e.getMessage());
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
logger.debug("キャッシュ有 (子供数: " + childList.size() + ")");
|
||||
ksActions.syncChildList().thenAccept(childListFromServer -> {
|
||||
if (childListFromServer != null) {
|
||||
boolean isChanged = childListFromServer.size() != childList.size() ||
|
||||
childListFromServer.stream().anyMatch(child -> childList.stream().noneMatch(childTmp -> child.getId().equals(childTmp.getId())));
|
||||
if (isChanged) {
|
||||
logger.debug("子供リスト取得完了: キャッシュと比較して変更あり");
|
||||
callback.onUpdated(childListFromServer);
|
||||
} else {
|
||||
logger.debug("子供リスト取得完了: キャッシュと比較して変更なし");
|
||||
callback.onUnchanged();
|
||||
}
|
||||
} else {
|
||||
boolean isChanged =
|
||||
childList.size() != childListTmp.get().size() ||
|
||||
childList.stream().anyMatch(child -> childListTmp.get().stream().noneMatch(childTmp -> child.getId().equals(childTmp.getId())));
|
||||
if (isChanged) {
|
||||
logger.debug("子供リスト取得完了: キャッシュと比較して変更あり");
|
||||
callback.onUpdated(childList);
|
||||
} else {
|
||||
logger.debug("子供リスト取得完了: キャッシュと比較して変更なし");
|
||||
callback.onUnchanged();
|
||||
}
|
||||
}).exceptionally(e -> {
|
||||
logger.error("子供リスト取得失敗: " + e.getMessage());
|
||||
callback.onFailed(e.getMessage());
|
||||
return null;
|
||||
});
|
||||
return CompletableFuture.completedFuture(childList);
|
||||
}
|
||||
}
|
||||
}).exceptionally(e -> {
|
||||
logger.error("子供リスト取得失敗: " + e.getMessage());
|
||||
callback.onFailed(e.getMessage());
|
||||
return null;
|
||||
}));
|
||||
thread.start();
|
||||
return cacheWrapper.getChildList().thenApply(childList -> {
|
||||
if (childList == null || childList.isEmpty()) {
|
||||
try {
|
||||
logger.debug("キャッシュ無: 子供リスト取得スレッド待機");
|
||||
thread.join();
|
||||
return cacheWrapper.getChildList().join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
logger.debug("キャッシュ有 (子供数: " + childList.size() + ")");
|
||||
childListTmp.set(childList);
|
||||
return childList;
|
||||
}
|
||||
}).join();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateChild(ChildModel child) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user