/src/kilim/Pausable.java

http://github.com/kilim/kilim · Java · 77 lines · 62 code · 10 blank · 5 comment · 1 complexity · bd137124ff6a9b2b4db5ecf4e1414c83 MD5 · raw file

  1. /* Copyright (c) 2006, Sriram Srinivasan
  2. *
  3. * You may distribute this software under the terms of the license
  4. * specified in the file "License"
  5. */
  6. package kilim;
  7. public class Pausable extends Exception {
  8. private static final long serialVersionUID = 1L;
  9. private Pausable() {}
  10. private Pausable(String msg) {}
  11. public interface Spawn<TT> {
  12. TT execute() throws Pausable, Exception;
  13. }
  14. public interface Fork {
  15. void execute() throws Pausable, Exception;
  16. }
  17. public interface Fork1<AA> {
  18. void execute(AA arg1) throws Pausable, Exception;
  19. }
  20. public interface Pfun<XX,YY,EE extends Throwable> { YY apply(XX obj) throws Pausable, EE; }
  21. public interface Psumer<XX,EE extends Throwable> { void apply(XX obj) throws Pausable, EE; }
  22. public static <XX,YY,EE extends Throwable>
  23. YY chain(XX obj,Pfun<XX,YY,EE> function) throws Pausable, EE {
  24. return function.apply(obj);
  25. }
  26. public static <X1,X2,ZZ,E1 extends Throwable,E2 extends Throwable>
  27. ZZ chain(X1 obj,
  28. Pfun<X1,X2,E1> function1,
  29. Pfun<X2,ZZ,E2> function2) throws Pausable, E1, E2 {
  30. X2 obj2 = function1.apply(obj);
  31. return function2.apply(obj2);
  32. }
  33. public static <X1,X2,X3,X4,E1 extends Throwable,E2 extends Throwable,E3 extends Throwable>
  34. X4 chain(X1 obj,
  35. Pfun<X1,X2,E1> function1,
  36. Pfun<X2,X3,E2> function2,
  37. Pfun<X3,X4,E3> function3) throws Pausable, E1, E2, E3 {
  38. X2 obj2 = function1.apply(obj);
  39. X3 obj3 = function2.apply(obj2);
  40. return function3.apply(obj3);
  41. }
  42. public static <XX,EE extends Throwable> XX apply(XX obj,Psumer<XX,EE> func) throws Pausable, EE {
  43. func.apply(obj);
  44. return obj;
  45. }
  46. public static <XX,E1 extends Throwable,E2 extends Throwable>
  47. XX apply(XX obj,Psumer<XX,E1> func1,Psumer<XX,E2> func2) throws Pausable, E1, E2 {
  48. func1.apply(obj);
  49. func2.apply(obj);
  50. return obj;
  51. }
  52. public static <XX,E1 extends Throwable,E2 extends Throwable,E3 extends Throwable>
  53. XX apply(XX obj,
  54. Psumer<XX,E1> func1,
  55. Psumer<XX,E2> func2,
  56. Psumer<XX,E3> func3)
  57. throws Pausable, E1, E2, E3 {
  58. func1.apply(obj);
  59. func2.apply(obj);
  60. return obj;
  61. }
  62. public static <XX,EE extends Throwable> XX applyAll(XX obj,Psumer<XX,EE> ... funcs) throws Pausable, EE {
  63. for (Psumer<XX,EE> func : funcs)
  64. func.apply(obj);
  65. return obj;
  66. }
  67. }