guavaCacheを使用するようにいくつかのコードをリファクタリングしています。
初期コード:
public Post getPost(Integer key) throws SQLException, IOException {
return PostsDB.findPostByID(key);
}
何かを壊さないために、スローされた例外をラップせずにそのまま保持する必要があります。
現在の解決策はやや醜いようです:
public Post getPost(final Integer key) throws SQLException, IOException {
try {
return cache.get(key, new Callable<Post>() {
@Override
public Post call() throws Exception {
return PostsDB.findPostByID(key);
}
});
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
throw new IllegalStateException(e);
}
}
}
それをより良くするための可能な方法はありますか?