I have a simple, functional interface:
public interface Callback<T> {
void invoke(T param);
}
I do a lot of asynchronous operations like:
public void getSubfolders(Folder folder, Callback<FolderList> result){
asyncExecutor.submit(() -> {
FolderList list = folder.get_SubFolders();
result.invoke(list);
});
}
Results should be processed on main thread. For this, I have a method JavaFX:
Platform.runLater(Runnable task);
What makes my code messy, like this one (and this pattern is repeated in 50 other methods):
public void getSubfolders(Folder folder, Callback<FolderList> result){
asyncExecutor.submit(() -> {
FolderList list = folder.get_SubFolders();
Platform.runLater(() -> result.invoke(list));
});
}
I would like to combine each callback call with Platform.runLater(...). The only thing I came up with is default method:
public interface Callback<T> {
void invoke(T param);
default void invokeOnMain(T param){
Platform.runLater(() -> invoke(param));
}
}
And then I just call result.invokeOnMain(list).
Is there a better approach for patterns like this?
user6367252
source
share