fork download
  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3.  
  4. interface Callback<E extends Exception> {
  5. void call() throws E;
  6. }
  7.  
  8. class DuplexCallback<E extends Exception> implements Callback<E> {
  9. private final Callback<? extends E> first;
  10. private final Callback<? extends E> second;
  11.  
  12. public DuplexCallback(
  13. final Callback<? extends E> first,
  14. final Callback<? extends E> second)
  15. {
  16. this.first = first;
  17. this.second = second;
  18. }
  19.  
  20. public void call() throws E {
  21. first.call();
  22. second.call();
  23. }
  24. }
  25.  
  26. class RuntimeExceptionAdapterCallback<E extends Exception>
  27. implements Callback<E>
  28. {
  29. private final Callback<? extends RuntimeException> callback;
  30.  
  31. public RuntimeExceptionAdapterCallback(
  32. final Callback<? extends RuntimeException> callback)
  33. {
  34. this.callback = callback;
  35. }
  36.  
  37. public void call() {
  38. callback.call();
  39. }
  40. }
  41.  
  42. class LoggingCallback implements Callback<RuntimeException> {
  43. public void call() {
  44. System.out.println("call() called");
  45. }
  46. }
  47.  
  48. class BytesLoggingCallback implements Callback<UnsupportedEncodingException> {
  49. private final byte[] bytes;
  50.  
  51. public BytesLoggingCallback(final byte[] bytes) {
  52. this.bytes = bytes;
  53. }
  54.  
  55. public void call() throws UnsupportedEncodingException {
  56. System.out.println(new String(bytes, "utf-8"));
  57. }
  58. }
  59.  
  60. public class Main {
  61. public static void main(final String[] args) throws Exception {
  62. Callback<IOException> callback =
  63. new DuplexCallback<>(
  64. new RuntimeExceptionAdapterCallback<IOException>(
  65. new LoggingCallback()),
  66. new BytesLoggingCallback(new byte[]{65}));
  67. callback.call();
  68. }
  69. }
  70.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
call() called
A