View Javadoc
1   package com.github.davidmoten.rx;
2   
3   import com.github.davidmoten.rx.Checked.A1;
4   
5   import rx.functions.Action0;
6   import rx.functions.Action1;
7   import rx.functions.Action2;
8   import rx.functions.Func0;
9   import rx.functions.Func1;
10  import rx.functions.Func2;
11  
12  /**
13   * Utility functions that are useful for brevity when using checked exceptions
14   * in lambdas with RxJava.
15   * 
16   * <p>
17   * Instead of
18   * </p>
19   * 
20   * <pre>
21   * OutputStream os =  ...;
22   * Observable&lt;String&gt; source = ...;
23   * source.doOnNext(s -&gt; {
24   *         try {
25   *             os.write(s.getBytes());
26   *         } catch (IOException e) {
27   *             throw new RuntimeException(e);
28   *         }
29   *     })
30   *     .subscribe();
31   * </pre>
32   * 
33   * <p>
34   * you can write:
35   * </p>
36   * 
37   * <pre>
38   * source.doOnNext(Checked.a1(s -&gt; os.write(s.getBytes()))).subscribe();
39   * </pre>
40   */
41  public final class Checked {
42  
43      public static interface F0<T> {
44          T call() throws Exception;
45      }
46  
47      public static interface F1<T, R> {
48          R call(T t) throws Exception;
49      }
50  
51      public static interface F2<T, R, S> {
52          S call(T t, R r) throws Exception;
53      }
54  
55      public static interface A0 {
56          void call() throws Exception;
57      }
58  
59      public static interface A1<T> {
60          void call(T t) throws Exception;
61      }
62      
63      public static interface A2<T,R> {
64          void call(T t, R r) throws Exception;
65      }
66  
67      /**
68       * Returns a {@link Func0} that reports any exception thrown by f wrapped by
69       * a {@link RuntimeException}.
70       * 
71       * @param f
72       *            has same signature as Func0 but can throw an exception
73       * @param <T>
74       *            type parameter
75       * @return a {@link Func0} that reports any exception thrown by f wrapped by
76       *         a {@link RuntimeException}.
77       * 
78       */
79      public static <T> Func0<T> f0(final F0<T> f) {
80          return new Func0<T>() {
81              @Override
82              public T call() {
83                  try {
84                      return f.call();
85                  } catch (Exception e) {
86                      throw new RuntimeException(e);
87                  }
88              }
89  
90          };
91      }
92  
93      public static <T, R> Func1<T, R> f1(final F1<T, R> f) {
94          return new Func1<T, R>() {
95              @Override
96              public R call(T t) {
97                  try {
98                      return f.call(t);
99                  } catch (Exception e) {
100                     throw new RuntimeException(e);
101                 }
102             }
103 
104         };
105     }
106 
107     public static <T, R, S> Func2<T, R, S> f2(final F2<T, R, S> f) {
108         return new Func2<T, R, S>() {
109             @Override
110             public S call(T t, R r) {
111                 try {
112                     return f.call(t, r);
113                 } catch (Exception e) {
114                     throw new RuntimeException(e);
115                 }
116             }
117 
118         };
119     }
120 
121     public static Action0 a0(final A0 a) {
122         return new Action0() {
123             @Override
124             public void call() {
125                 try {
126                     a.call();
127                 } catch (Exception e) {
128                     throw new RuntimeException(e);
129                 }
130             }
131         };
132     }
133 
134     public static <T> Action1<T> a1(final A1<T> a) {
135         return new Action1<T>() {
136             @Override
137             public void call(T t) {
138                 try {
139                     a.call(t);
140                 } catch (Exception e) {
141                     throw new RuntimeException(e);
142                 }
143             }
144         };
145     }
146     
147     public static <T,R> Action2<T, R> a2(final A2<T, R> a) {
148         return new Action2<T, R>() {
149             @Override
150             public void call(T t, R r) {
151                 try {
152                     a.call(t, r);
153                 } catch (Exception e) {
154                     throw new RuntimeException(e);
155                 }
156             }
157         };
158     }
159 
160 }