PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 7package kilim;
 8
 9public class Pausable extends Exception {
10    private static final long serialVersionUID = 1L;
11    private Pausable() {}
12    private Pausable(String msg) {}
13
14    
15    public interface Spawn<TT> {
16        TT execute() throws Pausable, Exception;
17    }
18    public interface Fork {
19        void execute() throws Pausable, Exception;
20    }
21    public interface Fork1<AA> {
22        void execute(AA arg1) throws Pausable, Exception;
23    }
24
25    public interface Pfun<XX,YY,EE extends Throwable> { YY apply(XX obj) throws Pausable, EE; }
26    public interface Psumer<XX,EE extends Throwable> { void apply(XX obj) throws Pausable, EE; }
27
28    public static <XX,YY,EE extends Throwable>
29        YY chain(XX obj,Pfun<XX,YY,EE> function) throws Pausable, EE {
30        return function.apply(obj);
31    }
32    public static <X1,X2,ZZ,E1 extends Throwable,E2 extends Throwable>
33        ZZ chain(X1 obj,
34            Pfun<X1,X2,E1> function1,
35            Pfun<X2,ZZ,E2> function2) throws Pausable, E1, E2 {
36        X2 obj2 = function1.apply(obj);
37        return function2.apply(obj2);
38    }
39    public static <X1,X2,X3,X4,E1 extends Throwable,E2 extends Throwable,E3 extends Throwable>
40        X4 chain(X1 obj,
41            Pfun<X1,X2,E1> function1,
42            Pfun<X2,X3,E2> function2,
43            Pfun<X3,X4,E3> function3) throws Pausable, E1, E2, E3 {
44        X2 obj2 = function1.apply(obj);
45        X3 obj3 = function2.apply(obj2);
46        return function3.apply(obj3);
47    }
48
49    
50    public static <XX,EE extends Throwable> XX apply(XX obj,Psumer<XX,EE> func) throws Pausable, EE {
51        func.apply(obj);
52        return obj;
53    }
54    public static <XX,E1 extends Throwable,E2 extends Throwable>
55        XX apply(XX obj,Psumer<XX,E1> func1,Psumer<XX,E2> func2) throws Pausable, E1, E2 {
56        func1.apply(obj);
57        func2.apply(obj);
58        return obj;
59    }
60    public static <XX,E1 extends Throwable,E2 extends Throwable,E3 extends Throwable>
61        XX apply(XX obj,
62                Psumer<XX,E1> func1,
63                Psumer<XX,E2> func2,
64                Psumer<XX,E3> func3)
65                throws Pausable, E1, E2, E3 {
66        func1.apply(obj);
67        func2.apply(obj);
68        return obj;
69    }
70    public static <XX,EE extends Throwable> XX applyAll(XX obj,Psumer<XX,EE> ... funcs) throws Pausable, EE {
71        for (Psumer<XX,EE> func : funcs)
72            func.apply(obj);
73        return obj;
74    }
75
76}
77