Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Last active November 28, 2023 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kikimora/9dcfb8afb72ed0e87435705e08f9232c to your computer and use it in GitHub Desktop.
Save Kikimora/9dcfb8afb72ed0e87435705e08f9232c to your computer and use it in GitHub Desktop.
SyncAsync.java
//Given below definitions implement MySyncApiImpl.operation and MySyncApiImpl.cancelOperation methods.
//Send me a link to github repo or a gist with your implementation.
public interface Callback {
void onSuccess(int result);
}
public interface ErrorCallback {
void onError(MyApiException error);
}
public interface Cancellable {
void cancel();
}
public class MyApiException extends Exception {}
public interface MyAsyncApi {
Cancellable operation(int param, Callback onSuccess, ErrorCallback onError);
}
public class MySyncApiImpl {
public MySyncApiImpl(MyAsyncApi api) {
this.api = api;
}
/**
* Runs MyAsyncApi.operation and blocks until it finishes. Throws IllegalStateException if called while operation is running.
*/
public int operation(int param) throws MyApiException {
//Please implement this method using this.api
}
/**
* Cancel most recent operation started with 'operation' method.
* Return true of operation was cancelled and false otherwise.
*/
public boolean cancelOperation() {
//Cancel the running operation.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment