View Javadoc
1   package com.github.davidmoten.rx;
2   
3   import java.io.Closeable;
4   import java.io.IOException;
5   import java.util.List;
6   import java.util.concurrent.CountDownLatch;
7   import java.util.concurrent.atomic.AtomicBoolean;
8   import java.util.concurrent.atomic.AtomicInteger;
9   import java.util.concurrent.atomic.AtomicLong;
10  import java.util.concurrent.atomic.AtomicReference;
11  
12  import rx.Subscription;
13  import rx.functions.Action0;
14  import rx.functions.Action1;
15  import rx.functions.Action2;
16  import rx.functions.Action3;
17  
18  public final class Actions {
19  
20      private Actions() {
21          // prevent instantiation
22      }
23  
24      public static Action1<Integer> setAtomic(final AtomicInteger a) {
25          return new Action1<Integer>() {
26  
27              @Override
28              public void call(Integer t) {
29                  a.set(t);
30              }
31          };
32      }
33  
34      public static Action1<Long> setAtomic(final AtomicLong a) {
35          return new Action1<Long>() {
36  
37              @Override
38              public void call(Long t) {
39                  a.set(t);
40              }
41          };
42      }
43  
44      public static Action1<Boolean> setAtomic(final AtomicBoolean a) {
45          return new Action1<Boolean>() {
46  
47              @Override
48              public void call(Boolean t) {
49                  a.set(t);
50              }
51          };
52      }
53  
54      public static <T> Action1<T> setAtomic(final AtomicReference<T> a) {
55          return new Action1<T>() {
56  
57              @Override
58              public void call(T t) {
59                  a.set(t);
60              }
61          };
62      }
63  
64      private static class HolderDoNothing0 {
65          static final Action0 INSTANCE = new Action0() {
66  
67              @Override
68              public void call() {
69                  // do nothing
70              }
71          };
72  
73      }
74  
75      public static Action0 doNothing0() {
76          return HolderDoNothing0.INSTANCE;
77      }
78  
79      private static class HolderDoNothing1 {
80          static final Action1<Object> INSTANCE = new Action1<Object>() {
81  
82              @Override
83              public void call(Object t) {
84                  // do Nothing
85              }
86          };
87      }
88  
89      @SuppressWarnings("unchecked")
90      public static <T> Action1<T> doNothing1() {
91          return (Action1<T>) HolderDoNothing1.INSTANCE;
92      }
93  
94      private static class HolderDoNothing2 {
95          static final Action2<Object, Object> INSTANCE = new Action2<Object, Object>() {
96  
97              @Override
98              public void call(Object t, Object t2) {
99                  // do Nothing
100             }
101         };
102     }
103 
104     @SuppressWarnings("unchecked")
105     public static <T, R> Action2<T, R> doNothing2() {
106         return (Action2<T, R>) HolderDoNothing2.INSTANCE;
107     }
108 
109     private static class HolderDoNothing3 {
110         static final Action3<Object, Object, Object> INSTANCE = new Action3<Object, Object, Object>() {
111 
112             @Override
113             public void call(Object t, Object t2, Object t3) {
114                 // do Nothing
115             }
116         };
117     }
118 
119     @SuppressWarnings("unchecked")
120     public static <T, R, S> Action3<T, R, S> doNothing3() {
121         return (Action3<T, R, S>) HolderDoNothing3.INSTANCE;
122     }
123 
124     public static Action0 unsubscribe(final Subscription subscription) {
125         return new Action0() {
126             @Override
127             public void call() {
128                 subscription.unsubscribe();
129             }
130         };
131     }
132 
133     public static <T> Action1<T> increment1(final AtomicInteger count) {
134         return new Action1<T>() {
135             @Override
136             public void call(T t) {
137                 count.incrementAndGet();
138             }
139         };
140     }
141 
142     public static Action0 increment0(final AtomicInteger count) {
143         return new Action0() {
144             @Override
145             public void call() {
146                 count.incrementAndGet();
147             }
148         };
149     }
150 
151     public static <T> Action1<T> decrement1(final AtomicInteger count) {
152         return new Action1<T>() {
153             @Override
154             public void call(T t) {
155                 count.incrementAndGet();
156             }
157         };
158     }
159 
160     public static Action0 decrement0(final AtomicInteger count) {
161         return new Action0() {
162             @Override
163             public void call() {
164                 count.incrementAndGet();
165             }
166         };
167     }
168 
169     public static Action1<Long> addTo(final AtomicLong value) {
170         return new Action1<Long>() {
171 
172             @Override
173             public void call(Long t) {
174                 value.addAndGet(t);
175             }
176         };
177     }
178 
179     @SuppressWarnings("unchecked")
180     public static <T> Action1<T> println() {
181         return (Action1<T>) PrintlnHolder.instance;
182     }
183 
184     private static class PrintlnHolder {
185         static final Action1<Object> instance = new Action1<Object>() {
186             @Override
187             public void call(Object t) {
188                 System.out.println(t);
189             }
190         };
191     }
192 
193     public static <T> Action1<T> setToTrue1(final AtomicBoolean ref) {
194         return new Action1<T>() {
195 
196             @Override
197             public void call(T t) {
198                 ref.set(true);
199             }
200         };
201     }
202 
203     public static Action0 setToTrue0(final AtomicBoolean ref) {
204         return new Action0() {
205 
206             @Override
207             public void call() {
208                 ref.set(true);
209             }
210         };
211     }
212 
213     public static Action0 countDown(final CountDownLatch latch) {
214         return new Action0() {
215 
216             @Override
217             public void call() {
218                 latch.countDown();
219             }
220         };
221     }
222 
223     public static <T> Action1<T> countDown1(final CountDownLatch latch) {
224         return new Action1<T>() {
225 
226             @Override
227             public void call(T t) {
228                 latch.countDown();
229             }
230         };
231     }
232 
233     public static Action1<Throwable> printStackTrace1() {
234         return PrintStackTrace1Holder.instance;
235     }
236 
237     private static class PrintStackTrace1Holder {
238         static final Action1<Throwable> instance = new Action1<Throwable>() {
239 
240             @Override
241             public void call(Throwable t) {
242                 t.printStackTrace();
243             }
244         };
245     }
246 
247     public static Action0 throw0(final RuntimeException ex) {
248         return new Action0() {
249 
250             @Override
251             public void call() {
252                 throw ex;
253             }
254         };
255     }
256 
257     public static Action1<Closeable> close() {
258         return CloseHolder.INSTANCE;
259     }
260 
261     private static final class CloseHolder {
262 
263         final static Action1<Closeable> INSTANCE = new Action1<Closeable>() {
264 
265             @Override
266             public void call(Closeable c) {
267                 try {
268                     c.close();
269                 } catch (IOException e) {
270                     throw new RuntimeException(e);
271                 }
272             }
273         };
274     }
275 
276     public static <T> Action1<T> addTo(final List<T> list) {
277         return new Action1<T>() {
278 
279             @Override
280             public void call(T t) {
281                 list.add(t);
282             }
283         };
284     }
285 
286 }