View Javadoc
1   package com.github.davidmoten.rx;
2   
3   import java.util.Comparator;
4   import java.util.concurrent.Callable;
5   
6   import rx.Observable;
7   import rx.functions.Func0;
8   import rx.functions.Func1;
9   import rx.functions.Func2;
10  
11  public final class Functions {
12  
13      private Functions() {
14          // prevent instantiation
15      }
16  
17      public static <T> Func1<T, T> identity() {
18          return new Func1<T, T>() {
19              @Override
20              public T call(T t) {
21                  return t;
22              }
23          };
24      }
25  
26      public static <T> Func1<T, Boolean> alwaysTrue() {
27          return new Func1<T, Boolean>() {
28              @Override
29              public Boolean call(T t) {
30                  return true;
31              }
32          };
33      }
34  
35      public static <T> Func1<T, Boolean> alwaysFalse() {
36          return new Func1<T, Boolean>() {
37              @Override
38              public Boolean call(T t) {
39                  return false;
40              }
41          };
42      }
43  
44      public static <T, R> Func1<T, R> constant(final R r) {
45          return new Func1<T, R>() {
46              @Override
47              public R call(T t) {
48                  return r;
49              }
50          };
51      }
52  
53      public static <T> Func0<T> constant0(final T t) {
54          return new Func0<T>() {
55  
56              @Override
57              public T call() {
58                  return t;
59              }
60          };
61      }
62  
63      public static <T> Func1<T, Boolean> not(final Func1<T, Boolean> f) {
64          return new Func1<T, Boolean>() {
65              @Override
66              public Boolean call(T t) {
67                  return !f.call(t);
68              }
69          };
70      }
71  
72      public static <T> Func1<T, Observable<T>> just() {
73          return new Func1<T, Observable<T>>() {
74              @Override
75              public Observable<T> call(T t) {
76                  return Observable.just(t);
77              }
78          };
79      }
80  
81      /**
82       * Returns a Func2 that adds numbers. Useful for Observable.reduce but not
83       * particularly performant as it does instanceOf checks.
84       * 
85       * @param <T>
86       *            generic type of Number being added
87       * @return Func2 that adds numbers
88       */
89      public static <T extends Number> Func2<T, T, T> add() {
90          return new Func2<T, T, T>() {
91              @SuppressWarnings("unchecked")
92              @Override
93              public T call(T a, T b) {
94                  if (a instanceof Integer)
95                      return (T) (Number) (a.intValue() + b.intValue());
96                  else if (a instanceof Long)
97                      return (T) (Number) (a.longValue() + b.longValue());
98                  else if (a instanceof Double)
99                      return (T) (Number) (a.doubleValue() + b.doubleValue());
100                 else if (a instanceof Float)
101                     return (T) (Number) (a.floatValue() + b.floatValue());
102                 else if (a instanceof Byte)
103                     return (T) (Number) (a.byteValue() + b.byteValue());
104                 else if (a instanceof Short)
105                     return (T) (Number) (a.shortValue() + b.shortValue());
106                 else
107                     throw new RuntimeException("not implemented");
108             }
109         };
110     }
111 
112     public static <T extends Number> Func2<Statistics, T, Statistics> collectStats() {
113         return new Func2<Statistics, T, Statistics>() {
114 
115             @Override
116             public Statistics call(Statistics s, T t) {
117                 return s.add(t);
118             }
119         };
120     }
121 
122     public static <T> Func2<T, T, Integer> toFunc2(final Comparator<? super T> comparator) {
123         return new Func2<T, T, Integer>() {
124             @Override
125             public Integer call(T t1, T t2) {
126                 return comparator.compare(t1, t2);
127             }
128         };
129     }
130 
131     public static <T> Comparator<T> toComparator(final Func2<? super T, ? super T, Integer> f) {
132         return new Comparator<T>() {
133             @Override
134             public int compare(T a, T b) {
135                 return f.call(a, b);
136             }
137         };
138     }
139 
140     public static <T, R> Func2<T, R, Boolean> alwaysTrue2() {
141         return new Func2<T, R, Boolean>() {
142 
143             @Override
144             public Boolean call(T t1, R t2) {
145                 return true;
146             }
147         };
148     }
149 
150     public static <T, R> Func2<T, R, Boolean> alwaysFalse2() {
151         return new Func2<T, R, Boolean>() {
152 
153             @Override
154             public Boolean call(T t1, R t2) {
155                 return false;
156             }
157         };
158     }
159 
160     public static <T> Func1<T, Boolean> isNull() {
161         return new Func1<T, Boolean>() {
162 
163             @Override
164             public Boolean call(T t) {
165                 return t == null;
166             }
167         };
168     }
169 
170     public static <T> Func1<T, Boolean> isNotNull() {
171         return new Func1<T, Boolean>() {
172 
173             @Override
174             public Boolean call(T t) {
175                 return t != null;
176             }
177         };
178     }
179 
180     public static <T> Func0<T> toFunc0(final Callable<T> f) {
181         return new Func0<T>() {
182 
183             @Override
184             public T call() {
185                 try {
186                     return f.call();
187                 } catch (Exception e) {
188                     throw new RuntimeException(e);
189                 }
190             }
191         };
192     }
193 
194 }